fix(utils): getSynchronize concurrency

This commit is contained in:
Marek Polak
2023-06-22 11:21:46 +02:00
committed by martin
parent 5d6db0d312
commit a8074a5f65
2 changed files with 15 additions and 2 deletions

View File

@@ -14,12 +14,15 @@ export const getSynchronize = () => {
let lock: Promise<any> | undefined;
return <T>(action: () => T | Promise<T>): Promise<T> => {
lock = (lock ?? Promise.resolve())
const newLock = (lock ?? Promise.resolve())
.catch(() => {})
.then(action)
.finally(() => {
lock = undefined;
if (lock === newLock) {
lock = undefined;
}
});
lock = newLock;
return lock;
};
};

View File

@@ -52,4 +52,14 @@ describe('getSynchronize', () => {
synchronize(() => sequence(['c', 7])),
]);
});
it('nested', done => {
synchronize(() =>
sequence(['a', 3]).then(() => {
// 'c' registers after 'a' ended and while 'b' is running
delay(2).then(() => synchronize(() => sequence(['c', 3])));
}),
);
synchronize(() => sequence(['b', 8]).then(done));
});
});