Skip to content

Commit d741000

Browse files
committed
set auto-subscription to undefined when update store to falsy value
1 parent 791533e commit d741000

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/compiler/compile/render_dom/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ export default function dom(
434434
const subscribe = `$$subscribe_${name}`;
435435
const i = renderer.context_lookup.get($name).index;
436436

437-
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe(${name}, $$value => $$invalidate(${i}, ${$name} = $$value)), ${name})`;
437+
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe_dynamic_store(${name}, $$value => $$invalidate(${i}, ${$name} = $$value)), ${name})`;
438438
}
439439

440440
return b`let ${$name};`;

src/runtime/internal/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ export function subscribe(store, ...callbacks) {
7272
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
7373
}
7474

75+
export function subscribe_dynamic_store(store, callback) {
76+
if (store == null) {
77+
callback(undefined);
78+
return noop;
79+
}
80+
const unsub = store.subscribe(callback);
81+
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
82+
}
83+
7584
export function get_store_value<T>(store: Readable<T>): T {
7685
let value;
7786
subscribe(store, _ => value = _)();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { writable } from 'svelte/store';
2+
3+
export default {
4+
html: '31',
5+
async test({ assert, component, target, window }) {
6+
component.store = undefined;
7+
8+
assert.htmlEqual(target.innerHTML, 'undefined');
9+
10+
component.store = writable(42);
11+
assert.htmlEqual(target.innerHTML, '42');
12+
}
13+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
export let store = writable(31);
4+
</script>
5+
6+
{$store}

0 commit comments

Comments
 (0)