Skip to content

Commit 977a7ca

Browse files
committed
fix: ensure that new trigger awais abort completion
1 parent 3b7dec0 commit 977a7ca

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/subscribe.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,36 @@ it('does not begin the new routine until the interrupted routine has completed',
390390

391391
expect(onChange.callCount).toBe(1);
392392
});
393+
394+
it('does not begin the new routine until the interrupted routine has completed (multiple-triggers)', async () => {
395+
const trigger = {
396+
...defaultTrigger,
397+
interruptible: true,
398+
persistent: true,
399+
retry: {
400+
maxTimeout: 100,
401+
retries: 1,
402+
},
403+
};
404+
405+
const onChange = sinon.stub(trigger, 'onChange');
406+
407+
onChange.resolves(async () => {
408+
await wait(100);
409+
});
410+
411+
const subscription = await subscribe(trigger);
412+
413+
void subscription.trigger([]);
414+
415+
await wait(10);
416+
417+
void subscription.trigger([]);
418+
void subscription.trigger([]);
419+
420+
await wait(10);
421+
422+
subscription.activeTask?.abortController?.abort();
423+
424+
expect(onChange.callCount).toBe(1);
425+
});

src/subscribe.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,12 @@ export const subscribe = (trigger: Trigger): Subscription => {
229229

230230
const abortedTaskPromise = outerActiveTask.promise;
231231

232-
outerActiveTask = null;
233-
234232
// Do not start a new task until the previous task has been
235233
// aborted and the shutdown routine has run to completion.
236234
await abortedTaskPromise;
235+
236+
// eslint-disable-next-line require-atomic-updates
237+
outerActiveTask = null;
237238
} else {
238239
if (trigger.persistent) {
239240
log.warn(

0 commit comments

Comments
 (0)