diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index 8b89ac27de..16832f10d9 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -98,9 +98,9 @@ export function writable(value?: T, start: StartStopNotifier = noop): Writ return () => { subscribers.delete(subscriber); - if (subscribers.size === 0) { + if (subscribers.size === 0 && stop) { stop(); - stop = noop; + stop = null; } }; } diff --git a/test/store/index.ts b/test/store/index.ts index b6fc5940e1..389f29244b 100644 --- a/test/store/index.ts +++ b/test/store/index.ts @@ -88,6 +88,14 @@ describe('store', () => { unsubscribe(); }); + + it('no error even if unsubscribe calls twice', () => { + let num = 0; + const store = writable(num, set => set(num += 1)); + const unsubscribe = store.subscribe(() => { }); + unsubscribe(); + assert.doesNotThrow(() => unsubscribe()); + }); }); describe('readable', () => {