From eb864af10cff9f318d03d6f703c7909819876409 Mon Sep 17 00:00:00 2001 From: Yuichiro Yamashita Date: Sun, 5 Feb 2023 23:33:04 +0900 Subject: [PATCH] add test and fix code --- src/runtime/store/index.ts | 4 ++-- test/store/index.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) 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', () => {