0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

fix: don't throw when calling writable() unsubscribe twice (#8186)

Fixes one case of #4765

---------

Co-authored-by: Ben Bucksch <bbucksch@jw.org>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Yuichiro Yamashita <xydybaseball@gmail.com>
This commit is contained in:
Ben Bucksch 2023-02-21 13:52:39 +01:00 committed by GitHub
parent 85f882f23d
commit f6ef6a9349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -98,7 +98,7 @@ export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writ
return () => {
subscribers.delete(subscriber);
if (subscribers.size === 0) {
if (subscribers.size === 0 && stop) {
stop();
stop = null;
}

View File

@ -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', () => {