Notes/src/services/sync_mutex.ts

22 lines
502 B
TypeScript
Raw Normal View History

/**
* Sync process can make data intermittently inconsistent. Processes which require strong data consistency
* (like consistency checks) can use this mutex to make sure sync isn't currently running.
*/
import { Mutex } from "async-mutex";
const instance = new Mutex();
2024-02-18 18:11:56 +02:00
async function doExclusively<T>(func: () => T) {
const releaseMutex = await instance.acquire();
try {
2020-07-01 21:33:52 +02:00
return await func();
2025-01-09 18:07:02 +02:00
} finally {
releaseMutex();
}
}
export default {
doExclusively
2020-06-20 12:31:38 +02:00
};