fix: derived store restarting when unsubscribed from another store with a shared ancestor

pull/8368/head
Jon Rouleau 4 years ago
parent f9efb4d992
commit cb6624f6e2

@ -164,7 +164,7 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
const auto = fn.length < 2;
return readable(initial_value, (set) => {
let inited = false;
let started = false;
const values = [];
let pending = 0;
@ -188,7 +188,7 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
(value) => {
values[i] = value;
pending &= ~(1 << i);
if (inited) {
if (started) {
sync();
}
},
@ -197,12 +197,13 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
})
);
inited = true;
started = true;
sync();
return function stop() {
run_all(unsubscribers);
cleanup();
started = false;
};
});
}

@ -407,6 +407,25 @@ describe('store', () => {
const d = derived(fake_observable, _ => _);
assert.equal(get(d), 42);
});
it('doesn\'t restart when unsubscribed from another store with a shared ancestor', () => {
const a = writable(true);
let b_started = false;
const b = derived(a, (_, __) => {
b_started = true;
return () => {
assert.equal(b_started, true);
b_started = false;
};
});
const c = derived(a, ($a, set) => {
if ($a) return b.subscribe(set);
});
c.subscribe(() => { });
a.set(false);
assert.equal(b_started, false);
});
});
describe('get', () => {

Loading…
Cancel
Save