set auto-subscription to undefined when update store to falsy value

pull/7693/head
tanhauhau 4 years ago
parent 1afcfd2b5f
commit e1a7831711

@ -434,7 +434,7 @@ export default function dom(
const subscribe = `$$subscribe_${name}`;
const i = renderer.context_lookup.get($name).index;
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe(${name}, $$value => $$invalidate(${i}, ${$name} = $$value)), ${name})`;
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe_dynamic_store(${name}, $$value => $$invalidate(${i}, ${$name} = $$value)), ${name})`;
}
return b`let ${$name};`;

@ -72,6 +72,15 @@ export function subscribe(store, ...callbacks) {
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}
export function subscribe_dynamic_store(store, callback) {
if (store == null) {
callback(undefined);
return noop;
}
const unsub = store.subscribe(callback);
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
}
export function get_store_value<T>(store: Readable<T>): T {
let value;
subscribe(store, _ => value = _)();

@ -0,0 +1,13 @@
import { writable } from 'svelte/store';
export default {
html: '31',
async test({ assert, component, target, window }) {
component.store = undefined;
assert.htmlEqual(target.innerHTML, 'undefined');
component.store = writable(42);
assert.htmlEqual(target.innerHTML, '42');
}
};

@ -0,0 +1,6 @@
<script>
import { writable } from 'svelte/store';
export let store = writable(31);
</script>
{$store}
Loading…
Cancel
Save