Skip to content

[fix] set auto-subscription to undefined when update store to falsy value #7693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};`;
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +75 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ask]

This is a question but why did you create the new function?
Why adding callbacks.forEach(callback => callback(undefined)); to subscribe is not good?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry didn't understand the question.

You are saying using what statement instead of which function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already we have subscribe function.
And the difference between subscribe_dynamic_store and subscribe is only line number 77. (callback(undefined);)

So I thought that we can update subscribe without creating the new function subscribe_dynamic_store.
But I believe you have a reason why subscribe_dynamic_store is necessary. So I would like to know about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. I can't remember why I did that.
I know we need to call callback(undefined) so that when store changed to undefined, we should update $store to undefined too.

The subscribe was used when it assumes that the store variable is static, so does not need to call callback to set the $store to undefined.

But let me look at it again and see if I can reuse the code.


export function get_store_value<T>(store: Readable<T>): T {
let value;
subscribe(store, _ => value = _)();
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/capture-inject-state/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
safe_not_equal,
set_data_dev,
space,
subscribe,
subscribe_dynamic_store,
text,
validate_slots,
validate_store
Expand Down Expand Up @@ -102,7 +102,7 @@ function instance($$self, $$props, $$invalidate) {

let $prop,
$$unsubscribe_prop = noop,
$$subscribe_prop = () => ($$unsubscribe_prop(), $$unsubscribe_prop = subscribe(prop, $$value => $$invalidate(2, $prop = $$value)), prop);
$$subscribe_prop = () => ($$unsubscribe_prop(), $$unsubscribe_prop = subscribe_dynamic_store(prop, $$value => $$invalidate(2, $prop = $$value)), prop);

$$self.$$.on_destroy.push(() => $$unsubscribe_prop());
let { $$slots: slots = {}, $$scope } = $$props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
safe_not_equal,
set_data,
space,
subscribe,
subscribe_dynamic_store,
text
} from "svelte/internal";

Expand Down Expand Up @@ -62,7 +62,7 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let $foo,
$$unsubscribe_foo = noop,
$$subscribe_foo = () => ($$unsubscribe_foo(), $$unsubscribe_foo = subscribe(foo, $$value => $$invalidate(1, $foo = $$value)), foo);
$$subscribe_foo = () => ($$unsubscribe_foo(), $$unsubscribe_foo = subscribe_dynamic_store(foo, $$value => $$invalidate(1, $foo = $$value)), foo);

$$self.$$.on_destroy.push(() => $$unsubscribe_foo());
let foo = writable(0);
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/reactive-class-optimized/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
noop,
safe_not_equal,
space,
subscribe,
subscribe_dynamic_store,
toggle_class
} from "svelte/internal";

Expand Down Expand Up @@ -136,7 +136,7 @@ function instance($$self, $$props, $$invalidate) {

let $reactiveDeclaration,
$$unsubscribe_reactiveDeclaration = noop,
$$subscribe_reactiveDeclaration = () => ($$unsubscribe_reactiveDeclaration(), $$unsubscribe_reactiveDeclaration = subscribe(reactiveDeclaration, $$value => $$invalidate(3, $reactiveDeclaration = $$value)), reactiveDeclaration);
$$subscribe_reactiveDeclaration = () => ($$unsubscribe_reactiveDeclaration(), $$unsubscribe_reactiveDeclaration = subscribe_dynamic_store(reactiveDeclaration, $$value => $$invalidate(3, $reactiveDeclaration = $$value)), reactiveDeclaration);

component_subscribe($$self, reactiveStoreVal, $$value => $$invalidate(2, $reactiveStoreVal = $$value));
$$self.$$.on_destroy.push(() => $$unsubscribe_reactiveDeclaration());
Expand Down
13 changes: 13 additions & 0 deletions test/runtime/samples/store-resubscribe-d/_config.js
Original file line number Diff line number Diff line change
@@ -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');
}
};
6 changes: 6 additions & 0 deletions test/runtime/samples/store-resubscribe-d/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
import { writable } from 'svelte/store';
export let store = writable(31);
</script>

{$store}