Skip to content

Commit

Permalink
fix(app-state): unsubscribing mid-emit could sometimes result in an e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
ersimont committed Nov 2, 2020
1 parent 21db328 commit c42585f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions projects/app-state/src/lib/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ describe('Store', () => {
store('counter').set(1);
expectSingleCallAndReset(spy, 1);
});

it('works when the last subscriber to a child store unsubscribes mid-emit (production bug)', () => {
store('counter')
.$.pipe(skip(1))
.subscribe(() => {
sub2.unsubscribe();
});
const sub2 = store('nested').$.subscribe();

expect(() => {
store('counter').set(1);
}).not.toThrowError();
});
});

describe('()', () => {
Expand Down
3 changes: 2 additions & 1 deletion projects/app-state/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export abstract class Store<T> extends CallableObject<GetSlice<T>> {
}
});
forOwn(this.activeChildren, (children) => {
for (const child of children) {
// `children` can be undefined if emitting from a previous key caused removed all subscribers to this key
for (const child of children || []) {
child.maybeEmit();
}
});
Expand Down

0 comments on commit c42585f

Please sign in to comment.