Skip to content

Commit

Permalink
fix: ensure that new trigger awais abort completion
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jun 5, 2023
1 parent 3b7dec0 commit 977a7ca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/subscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,36 @@ it('does not begin the new routine until the interrupted routine has completed',

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

it('does not begin the new routine until the interrupted routine has completed (multiple-triggers)', 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([]);
void subscription.trigger([]);

await wait(10);

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

expect(onChange.callCount).toBe(1);
});
5 changes: 3 additions & 2 deletions src/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ export const subscribe = (trigger: Trigger): Subscription => {

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;

// eslint-disable-next-line require-atomic-updates
outerActiveTask = null;
} else {
if (trigger.persistent) {
log.warn(
Expand Down

0 comments on commit 977a7ca

Please sign in to comment.