From 65eb5bb34066d01eefaafc6b54e81aa8e1b3df06 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Tue, 25 Jun 2019 07:58:19 -0400 Subject: [PATCH] only call subscriber once for writable with callback - fixes #3022 --- src/runtime/store/index.ts | 1 + test/store/index.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index 184212473559..6e55d010ce08 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -93,6 +93,7 @@ export function writable(value: T, start: StartStopNotifier = noop): Writa } if (subscribers.length === 0) { stop(); + stop = null; } }; } diff --git a/test/store/index.ts b/test/store/index.ts index bb999f122037..bbfe7099b4f4 100644 --- a/test/store/index.ts +++ b/test/store/index.ts @@ -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', () => {