Files
yjs/lib/mutualExclude.js

35 lines
732 B
JavaScript
Raw Normal View History

2018-03-23 04:35:52 +01:00
// TODO: rename mutex
/**
* Creates a mutual exclude function with the following property:
*
* @example
* const mutualExclude = createMutualExclude()
* mutualExclude(function () {
* // This function is immediately executed
* mutualExclude(function () {
* // This function is never executed, as it is called with the same
* // mutualExclude
* })
* })
*
* @return {Function} A mutual exclude function
* @public
*/
2018-01-10 00:17:26 +01:00
export function createMutualExclude () {
var token = true
2018-06-02 13:33:04 +02:00
return function mutualExclude (f, g) {
2018-01-10 00:17:26 +01:00
if (token) {
token = false
try {
f()
} catch (e) {
console.error(e)
}
token = true
2018-06-02 13:33:04 +02:00
} else if (g !== undefined) {
g()
2018-01-10 00:17:26 +01:00
}
}
}