Skip to content

Commit e4cec10

Browse files
committed
fix: wait for routine shutdown to complete
1 parent cfc75ca commit e4cec10

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/subscribe.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,35 @@ it('stops retrying persistent routine if teardown is called', async () => {
358358

359359
expect(onChange.callCount).toBe(firstCallCount);
360360
});
361+
362+
it('does not begin the new routine until the interrupted routine has completed', async () => {
363+
const trigger = {
364+
...defaultTrigger,
365+
interruptible: true,
366+
persistent: true,
367+
retry: {
368+
maxTimeout: 100,
369+
retries: 1,
370+
},
371+
};
372+
373+
const onChange = sinon.stub(trigger, 'onChange');
374+
375+
onChange.resolves(async () => {
376+
await wait(100);
377+
});
378+
379+
const subscription = await subscribe(trigger);
380+
381+
void subscription.trigger([]);
382+
383+
await wait(10);
384+
385+
void subscription.trigger([]);
386+
387+
await wait(10);
388+
389+
subscription.activeTask?.abortController?.abort();
390+
391+
expect(onChange.callCount).toBe(1);
392+
});

src/subscribe.ts

+6
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,13 @@ export const subscribe = (trigger: Trigger): Subscription => {
227227

228228
outerActiveTask.abortController.abort();
229229

230+
const abortedTaskPromise = outerActiveTask.promise;
231+
230232
outerActiveTask = null;
233+
234+
// Do not start a new task until the previous task has been
235+
// aborted and the shutdown routine has run to completion.
236+
await abortedTaskPromise;
231237
} else {
232238
if (trigger.persistent) {
233239
log.warn(

0 commit comments

Comments
 (0)