Skip to content

Commit

Permalink
fix: wait for routine shutdown to complete
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jun 5, 2023
1 parent cfc75ca commit e4cec10
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,35 @@ it('stops retrying persistent routine if teardown is called', async () => {

expect(onChange.callCount).toBe(firstCallCount);
});

it('does not begin the new routine until the interrupted routine has completed', async () => {
const trigger = {
...defaultTrigger,
interruptible: true,
persistent: true,
retry: {
maxTimeout: 100,
retries: 1,
},
};

const onChange = sinon.stub(trigger, 'onChange');

onChange.resolves(async () => {
await wait(100);
});

const subscription = await subscribe(trigger);

void subscription.trigger([]);

await wait(10);

void subscription.trigger([]);

await wait(10);

subscription.activeTask?.abortController?.abort();

expect(onChange.callCount).toBe(1);
});
6 changes: 6 additions & 0 deletions src/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ export const subscribe = (trigger: Trigger): Subscription => {

outerActiveTask.abortController.abort();

const abortedTaskPromise = outerActiveTask.promise;

outerActiveTask = null;

// Do not start a new task until the previous task has been
// aborted and the shutdown routine has run to completion.
await abortedTaskPromise;
} else {
if (trigger.persistent) {
log.warn(
Expand Down

0 comments on commit e4cec10

Please sign in to comment.