mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
/*!
|
|
* (C) Ionic http://ionicframework.com - MIT License
|
|
*/
|
|
/**
|
|
* Creates a lock controller.
|
|
*
|
|
* Claiming a lock means that nothing else can acquire the lock until it is released.
|
|
* This can momentarily prevent execution of code that needs to wait for the earlier code to finish.
|
|
* For example, this can be used to prevent multiple transitions from occurring at the same time.
|
|
*/
|
|
const createLockController = () => {
|
|
let waitPromise;
|
|
/**
|
|
* When lock() is called, the lock is claimed.
|
|
* Once a lock has been claimed, it cannot be claimed again until it is released.
|
|
* When this function gets resolved, the lock is released, allowing it to be claimed again.
|
|
*
|
|
* @example ```tsx
|
|
* const unlock = await this.lockController.lock();
|
|
* // do other stuff
|
|
* unlock();
|
|
* ```
|
|
*/
|
|
const lock = async () => {
|
|
const p = waitPromise;
|
|
let resolve;
|
|
waitPromise = new Promise((r) => (resolve = r));
|
|
if (p !== undefined) {
|
|
await p;
|
|
}
|
|
return resolve;
|
|
};
|
|
return {
|
|
lock,
|
|
};
|
|
};
|
|
|
|
export { createLockController as c };
|
|
|