Skip to content

Commit 6f60ed8

Browse files
committed
Merge branch 'hmr' of https://github.com/Axelen123/svelte into Axelen123-hmr
2 parents 3f33d35 + 1ca0b23 commit 6f60ed8

File tree

9 files changed

+162
-1
lines changed

9 files changed

+162
-1
lines changed

src/compiler/compile/render_dom/index.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default function dom(
9393
const body = [];
9494

9595
const not_equal = component.component_options.immutable ? `@not_equal` : `@safe_not_equal`;
96-
let dev_props_check;
96+
let dev_props_check; let inject_state; let capture_state;
9797

9898
props.forEach(x => {
9999
const variable = component.var_lookup.get(x.name);
@@ -150,6 +150,29 @@ export default function dom(
150150
}`)}
151151
`;
152152
}
153+
154+
capture_state = (uses_props || writable_props.length > 0) ? deindent`
155+
() => {
156+
return { ${component.vars.filter(prop => prop.writable).map(prop => prop.name).join(", ")} };
157+
}
158+
` : deindent`
159+
() => {
160+
return {}
161+
}
162+
`;
163+
164+
inject_state = (uses_props || writable_props.length > 0) ? deindent`
165+
${$$props} => {
166+
${uses_props && component.invalidate('$$props', `$$props = @assign(@assign({}, $$props), $$new_props)`)}
167+
${component.vars.filter(prop => prop.writable).map(prop => deindent`
168+
if ('${prop.name}' in $$props) ${component.invalidate(prop.name, `${prop.name} = ${$$props}.${prop.name}`)};
169+
`)}
170+
}
171+
` : deindent`
172+
${$$props} => {
173+
return
174+
}
175+
`;
153176
}
154177

155178
// instrument assignments
@@ -351,6 +374,10 @@ export default function dom(
351374
352375
${set && `$$self.$set = ${set};`}
353376
377+
${capture_state && `$$self.$capture_state = ${capture_state};`}
378+
379+
${inject_state && `$$self.$inject_state = ${inject_state};`}
380+
354381
${injected.length && `let ${injected.join(', ')};`}
355382
356383
${reactive_declarations.length > 0 && deindent`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
options: {
3+
dev: false
4+
}
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* generated by Svelte vX.Y.Z */
2+
import {
3+
SvelteComponent,
4+
append,
5+
detach,
6+
element,
7+
init,
8+
insert,
9+
listen,
10+
noop,
11+
safe_not_equal,
12+
set_data,
13+
space,
14+
text
15+
} from "svelte/internal";
16+
17+
function create_fragment(ctx) {
18+
var p, t0, t1, input, dispose;
19+
20+
return {
21+
c() {
22+
p = element("p");
23+
t0 = text(ctx.foo);
24+
t1 = space();
25+
input = element("input");
26+
dispose = listen(input, "input", ctx.input_input_handler);
27+
},
28+
29+
m(target, anchor) {
30+
insert(target, p, anchor);
31+
append(p, t0);
32+
insert(target, t1, anchor);
33+
insert(target, input, anchor);
34+
35+
input.value = ctx.foo;
36+
},
37+
38+
p(changed, ctx) {
39+
if (changed.foo) {
40+
set_data(t0, ctx.foo);
41+
}
42+
43+
if (changed.foo && (input.value !== ctx.foo)) input.value = ctx.foo;
44+
},
45+
46+
i: noop,
47+
o: noop,
48+
49+
d(detaching) {
50+
if (detaching) {
51+
detach(p);
52+
detach(t1);
53+
detach(input);
54+
}
55+
56+
dispose();
57+
}
58+
};
59+
}
60+
61+
function instance($$self, $$props, $$invalidate) {
62+
let foo = "bar";
63+
64+
function input_input_handler() {
65+
foo = this.value;
66+
$$invalidate('foo', foo);
67+
}
68+
69+
return { foo, input_input_handler };
70+
}
71+
72+
class Component extends SvelteComponent {
73+
constructor(options) {
74+
super();
75+
init(this, options, instance, create_fragment, safe_not_equal, []);
76+
}
77+
}
78+
79+
export default Component;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let foo = "bar";
3+
</script>
4+
<p>{foo}</p>
5+
<input bind:value={foo}>

test/js/samples/debug-empty/expected.js

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ function instance($$self, $$props, $$invalidate) {
7474
if ('name' in $$props) $$invalidate('name', name = $$props.name);
7575
};
7676

77+
$$self.$capture_state = () => {
78+
return { name };
79+
};
80+
81+
$$self.$inject_state = $$props => {
82+
if ('name' in $$props) $$invalidate('name', name = $$props.name);
83+
};
84+
7785
return { name };
7886
}
7987

test/js/samples/debug-foo-bar-baz-things/expected.js

+11
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ function instance($$self, $$props, $$invalidate) {
164164
if ('baz' in $$props) $$invalidate('baz', baz = $$props.baz);
165165
};
166166

167+
$$self.$capture_state = () => {
168+
return { things, foo, bar, baz };
169+
};
170+
171+
$$self.$inject_state = $$props => {
172+
if ('things' in $$props) $$invalidate('things', things = $$props.things);
173+
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
174+
if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
175+
if ('baz' in $$props) $$invalidate('baz', baz = $$props.baz);
176+
};
177+
167178
return { things, foo, bar, baz };
168179
}
169180

test/js/samples/debug-foo/expected.js

+9
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ function instance($$self, $$props, $$invalidate) {
162162
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
163163
};
164164

165+
$$self.$capture_state = () => {
166+
return { things, foo };
167+
};
168+
169+
$$self.$inject_state = $$props => {
170+
if ('things' in $$props) $$invalidate('things', things = $$props.things);
171+
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
172+
};
173+
165174
return { things, foo };
166175
}
167176

test/js/samples/debug-hoisted/expected.js

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ let kobzol = 5;
4343
function instance($$self) {
4444
let obj = { x: 5 };
4545

46+
$$self.$capture_state = () => {
47+
return {}
48+
};
49+
50+
$$self.$inject_state = $$props => {
51+
return
52+
};
53+
4654
return { obj };
4755
}
4856

test/js/samples/dev-warning-missing-data-computed/expected.js

+9
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ function instance($$self, $$props, $$invalidate) {
7474
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
7575
};
7676

77+
$$self.$capture_state = () => {
78+
return { foo, bar };
79+
};
80+
81+
$$self.$inject_state = $$props => {
82+
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
83+
if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
84+
};
85+
7786
$$self.$$.update = ($$dirty = { foo: 1 }) => {
7887
if ($$dirty.foo) { $$invalidate('bar', bar = foo * 2); }
7988
};

0 commit comments

Comments
 (0)