Merge pull request #3107 from sveltejs/gh-3022

only call subscriber once for writable with callback
pull/3112/head
Rich Harris 5 years ago committed by GitHub
commit 423428f488
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -93,6 +93,7 @@ export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writa
}
if (subscribers.length === 0) {
stop();
stop = null;
}
};
}

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

Loading…
Cancel
Save