Skip to content

Commit

Permalink
only call subscriber once for writable with callback - fixes #3022
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jun 25, 2019
1 parent 35001b3 commit 65eb5bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/runtime/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writa
}
if (subscribers.length === 0) {
stop();
stop = null;
}
};
}
Expand Down
16 changes: 16 additions & 0 deletions test/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ describe('store', () => {
store.update(obj => obj);
assert.equal(called, 3);
});

it('only calls subscriber once initially, including on resubscriptions', () => {
let num = 0;
const store = writable(num, set => set(num += 1));

let count1 = 0;
let count2 = 0;

store.subscribe(() => count1 += 1)();
assert.equal(count1, 1);

const unsubscribe = store.subscribe(() => count2 += 1);
assert.equal(count2, 1);

unsubscribe();
});
});

describe('readable', () => {
Expand Down

0 comments on commit 65eb5bb

Please sign in to comment.