Closed ribx closed 3 years ago
Hi @ribx ,
The documentation above the function explains the functionality:
/**
* Creates a mutual exclude function with the following property:
*
* ```js
* const mutex = createMutex()
* mutex(() => {
* // This function is immediately executed
* mutex(() => {
* // This function is not executed, as the mutex is already active.
* })
* })
* ```
*
* @return {mutex} A mutual exclude function
* @public
*/
This helper function was originally created when async functions didn't exist yet (back in 2014). It was an homage to mutexes that obviously don't need to exist in JavaScript. This "mutex" implementation is helpful when you want to prevent recursion. I.e.
editor.on(delta => {
mutex(() => {
ytext.applyDelta(delta)
})
})
ytext.observe(event => {
mutex(() => {
editor.applyDelta(event.delta)
})
})
This is a typical editor binding. With mutexes, you can prevent recursion. But nowadays I prefer to use the transaction-origin to determine if a change should be ignored.
The function behaves exactly as specified. You don't need to use it. In any case, I won't change the behavior of lib0/mutex
because there are several packages depending on it.
Describe the bug Short Background: I am evaluating at the moment, wether we should switch to yjs from automerge in our software
Looking at the mutex code
const mutex = createMutex()
mutex(() => mutex(() => console.log('test')))