yjs/lib/mutex.js
2018-11-27 14:59:24 +01:00

32 lines
591 B
JavaScript

/**
* Creates a mutual exclude function with the following property:
*
* @example
* const mutex = createMutex()
* mutex(() => {
* // This function is immediately executed
* mutex(() => {
* // This function is not executed, as the mutex is already active.
* })
* })
*
* @return {Function} A mutual exclude function
* @public
*/
export const createMutex = () => {
let token = true
return (f, g) => {
if (token) {
token = false
try {
f()
} finally {
token = true
}
} else if (g !== undefined) {
g()
}
}
}