mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
client-ts: Port services/mutex
This commit is contained in:
parent
679e9eba77
commit
bece0aa784
@ -1,12 +1,13 @@
|
|||||||
export default class Mutex {
|
export default class Mutex {
|
||||||
|
private current: Promise<void>;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.current = Promise.resolve();
|
this.current = Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {Promise} */
|
|
||||||
lock() {
|
lock() {
|
||||||
let resolveFun;
|
let resolveFun: () => void;
|
||||||
const subPromise = new Promise(resolve => resolveFun = () => resolve());
|
const subPromise = new Promise<void>(resolve => resolveFun = () => resolve());
|
||||||
// Caller gets a promise that resolves when the current outstanding lock resolves
|
// Caller gets a promise that resolves when the current outstanding lock resolves
|
||||||
const newPromise = this.current.then(() => resolveFun);
|
const newPromise = this.current.then(() => resolveFun);
|
||||||
// Don't allow the next request until the new promise is done
|
// Don't allow the next request until the new promise is done
|
||||||
@ -15,7 +16,7 @@ export default class Mutex {
|
|||||||
return newPromise;
|
return newPromise;
|
||||||
};
|
};
|
||||||
|
|
||||||
async runExclusively(cb) {
|
async runExclusively(cb: () => Promise<void>) {
|
||||||
const unlock = await this.lock();
|
const unlock = await this.lock();
|
||||||
|
|
||||||
try {
|
try {
|
Loading…
x
Reference in New Issue
Block a user