Skip to content

Commit 54a129c

Browse files
dasZGFzRich-Harris
authored andcommitted
continue inlining $$invalidate calls (#3548)
* finish inlining $$invalidate * add failing test * add test for navigator online * add tests for store invalidate refactor * add test for reactive_store_subscriptions * remove failing test * update
1 parent 84b0e86 commit 54a129c

File tree

10 files changed

+254
-3
lines changed

10 files changed

+254
-3
lines changed

src/compiler/compile/render_dom/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export default function dom(
228228
return b`${`$$subscribe_${name}`}()`;
229229
}
230230

231-
const callback = x`$$value => { $$invalidate('${value}', ${value} = $$value) }`;
231+
const callback = x`$$value => $$invalidate('${value}', ${value} = $$value)`;
232232

233233
let insert = b`@component_subscribe($$self, ${name}, $${callback})`;
234234
if (component.compile_options.dev) {
@@ -305,7 +305,7 @@ export default function dom(
305305
})
306306
.map(({ name }) => b`
307307
${component.compile_options.dev && `@validate_store(${name.slice(1)}, '${name.slice(1)}');`}
308-
@component_subscribe($$self, ${name.slice(1)}, $$value => { ${name} = $$value; $$invalidate('${name}', ${name}); });
308+
@component_subscribe($$self, ${name.slice(1)}, $$value => $$invalidate('${name}', ${name} = $$value));
309309
`);
310310

311311
const resubscribable_reactive_store_unsubscribers = reactive_stores
@@ -356,7 +356,7 @@ export default function dom(
356356
if (store && store.reassigned) {
357357
const unsubscribe = `$$unsubscribe_${name}`;
358358
const subscribe = `$$subscribe_${name}`;
359-
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe(${name}, $$value => { ${$name} = $$value; $$invalidate('${$name}', ${$name}); }), ${name})`;
359+
return b`let ${$name}, ${unsubscribe} = @noop, ${subscribe} = () => (${unsubscribe}(), ${unsubscribe} = @subscribe(${name}, $$value => $$invalidate('${$name}', ${$name} = $$value)), ${name})`;
360360
}
361361

362362
return b`let ${$name};`;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
SvelteComponent,
3+
append,
4+
component_subscribe,
5+
detach,
6+
element,
7+
init,
8+
insert,
9+
noop,
10+
safe_not_equal,
11+
set_data,
12+
text
13+
} from "svelte/internal";
14+
15+
import { writable } from "svelte/store";
16+
17+
function create_fragment(ctx) {
18+
let h1;
19+
let t;
20+
21+
return {
22+
c() {
23+
h1 = element("h1");
24+
t = text(ctx.$foo);
25+
},
26+
m(target, anchor) {
27+
insert(target, h1, anchor);
28+
append(h1, t);
29+
},
30+
p(changed, ctx) {
31+
if (changed.$foo) set_data(t, ctx.$foo);
32+
},
33+
i: noop,
34+
o: noop,
35+
d(detaching) {
36+
if (detaching) detach(h1);
37+
}
38+
};
39+
}
40+
41+
function instance($$self, $$props, $$invalidate) {
42+
let $foo;
43+
const foo = writable(0);
44+
component_subscribe($$self, foo, $$value => $$invalidate("$foo", $foo = $$value));
45+
return { foo, $foo };
46+
}
47+
48+
class Component extends SvelteComponent {
49+
constructor(options) {
50+
super();
51+
init(this, options, instance, create_fragment, safe_not_equal, []);
52+
}
53+
}
54+
55+
export default Component;
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+
const foo = writable(0);
4+
</script>
5+
6+
<h1>{$foo}</h1>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
SvelteComponent,
3+
component_subscribe,
4+
init,
5+
noop,
6+
safe_not_equal,
7+
set_store_value
8+
} from "svelte/internal";
9+
10+
import { count } from "./store.js";
11+
12+
function create_fragment(ctx) {
13+
return {
14+
c: noop,
15+
m: noop,
16+
p: noop,
17+
i: noop,
18+
o: noop,
19+
d: noop
20+
};
21+
}
22+
23+
function instance($$self, $$props, $$invalidate) {
24+
let $count;
25+
component_subscribe($$self, count, $$value => $$invalidate("$count", $count = $$value));
26+
27+
function increment() {
28+
set_store_value(count, $count++, $count);
29+
}
30+
31+
return { increment };
32+
}
33+
34+
class Component extends SvelteComponent {
35+
constructor(options) {
36+
super();
37+
init(this, options, instance, create_fragment, safe_not_equal, ["increment"]);
38+
}
39+
40+
get increment() {
41+
return this.$$.ctx.increment;
42+
}
43+
}
44+
45+
export default Component;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { count } from './store.js';
3+
4+
export function increment() {
5+
$count++;
6+
}
7+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { writable } from '../../../../store';
2+
3+
export const count = writable(0);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import {
2+
SvelteComponent,
3+
append,
4+
detach,
5+
element,
6+
init,
7+
insert,
8+
listen,
9+
noop,
10+
safe_not_equal,
11+
set_data,
12+
space,
13+
subscribe,
14+
text
15+
} from "svelte/internal";
16+
17+
import { writable } from "svelte/store";
18+
19+
function create_fragment(ctx) {
20+
let h1;
21+
let t0;
22+
let t1;
23+
let button;
24+
let dispose;
25+
26+
return {
27+
c() {
28+
h1 = element("h1");
29+
t0 = text(ctx.$foo);
30+
t1 = space();
31+
button = element("button");
32+
button.textContent = "reset";
33+
dispose = listen(button, "click", ctx.click_handler);
34+
},
35+
m(target, anchor) {
36+
insert(target, h1, anchor);
37+
append(h1, t0);
38+
insert(target, t1, anchor);
39+
insert(target, button, anchor);
40+
},
41+
p(changed, ctx) {
42+
if (changed.$foo) set_data(t0, ctx.$foo);
43+
},
44+
i: noop,
45+
o: noop,
46+
d(detaching) {
47+
if (detaching) detach(h1);
48+
if (detaching) detach(t1);
49+
if (detaching) detach(button);
50+
dispose();
51+
}
52+
};
53+
}
54+
55+
function instance($$self, $$props, $$invalidate) {
56+
let $foo,
57+
$$unsubscribe_foo = noop,
58+
$$subscribe_foo = () => ($$unsubscribe_foo(), $$unsubscribe_foo = subscribe(foo, $$value => $$invalidate("$foo", $foo = $$value)), foo);
59+
60+
$$self.$$.on_destroy.push(() => $$unsubscribe_foo());
61+
let foo = writable(0);
62+
$$subscribe_foo();
63+
const click_handler = () => $$subscribe_foo($$invalidate("foo", foo = writable(0)));
64+
return { foo, $foo, click_handler };
65+
}
66+
67+
class Component extends SvelteComponent {
68+
constructor(options) {
69+
super();
70+
init(this, options, instance, create_fragment, safe_not_equal, []);
71+
}
72+
}
73+
74+
export default Component;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
let foo = writable(0);
4+
</script>
5+
6+
<h1>{$foo}</h1>
7+
<button on:click="{() => foo = writable(0)}">reset</button>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
SvelteComponent,
3+
add_render_callback,
4+
init,
5+
listen,
6+
noop,
7+
run_all,
8+
safe_not_equal
9+
} from "svelte/internal";
10+
11+
function create_fragment(ctx) {
12+
let dispose;
13+
add_render_callback(ctx.onlinestatuschanged);
14+
15+
return {
16+
c() {
17+
dispose = [
18+
listen(window, "online", ctx.onlinestatuschanged),
19+
listen(window, "offline", ctx.onlinestatuschanged)
20+
];
21+
},
22+
m: noop,
23+
p: noop,
24+
i: noop,
25+
o: noop,
26+
d(detaching) {
27+
run_all(dispose);
28+
}
29+
};
30+
}
31+
32+
function instance($$self, $$props, $$invalidate) {
33+
let online;
34+
35+
function onlinestatuschanged() {
36+
$$invalidate("online", online = navigator.onLine);
37+
}
38+
39+
return { online, onlinestatuschanged };
40+
}
41+
42+
class Component extends SvelteComponent {
43+
constructor(options) {
44+
super();
45+
init(this, options, instance, create_fragment, safe_not_equal, []);
46+
}
47+
}
48+
49+
export default Component;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let online;
3+
</script>
4+
5+
<svelte:window bind:online={online}/>

0 commit comments

Comments
 (0)