()
| 6 | * For example, this can be used to prevent multiple transitions from occurring at the same time. |
| 7 | */ |
| 8 | export const createLockController = () => { |
| 9 | let waitPromise: Promise<void>; |
| 10 | |
| 11 | /** |
| 12 | * When lock() is called, the lock is claimed. |
| 13 | * Once a lock has been claimed, it cannot be claimed again until it is released. |
| 14 | * When this function gets resolved, the lock is released, allowing it to be claimed again. |
| 15 | * |
| 16 | * @example ```tsx |
| 17 | * const unlock = await this.lockController.lock(); |
| 18 | * // do other stuff |
| 19 | * unlock(); |
| 20 | * ``` |
| 21 | */ |
| 22 | const lock = async () => { |
| 23 | const p = waitPromise; |
| 24 | let resolve!: () => void; |
| 25 | waitPromise = new Promise((r) => (resolve = r)); |
| 26 | if (p !== undefined) { |
| 27 | await p; |
| 28 | } |
| 29 | return resolve; |
| 30 | }; |
| 31 | |
| 32 | return { |
| 33 | lock, |
| 34 | }; |
| 35 | }; |
no outgoing calls
no test coverage detected