mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-02 21:45:14 +01:00
fix(utils): getSynchronize concurrency
This commit is contained in:
@@ -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;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user