Skip to content
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

HMR Hooks (Discussion) #3126

Closed
wants to merge 6 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
29 changes: 28 additions & 1 deletion src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default function dom(
const body = [];

const not_equal = component.component_options.immutable ? `@not_equal` : `@safe_not_equal`;
let dev_props_check;
let dev_props_check; let inject_state; let capture_state;

props.forEach(x => {
const variable = component.var_lookup.get(x.name);
Expand Down Expand Up @@ -151,6 +151,29 @@ export default function dom(
}`)}
`;
}

capture_state = (uses_props || writable_props.length > 0) ? deindent`
() => {
return { ${component.vars.filter(prop => prop.writable).map(prop => prop.name).join(", ")} };
}
` : deindent`
() => {
return {}
}
`;

inject_state = (uses_props || writable_props.length > 0) ? deindent`
${$$props} => {
${uses_props && component.invalidate('$$props', `$$props = @assign(@assign({}, $$props), $$new_props)`)}
${component.vars.filter(prop => prop.writable).map(prop => deindent`
if ('${prop.name}' in $$props) ${component.invalidate(prop.name, `${prop.name} = ${$$props}.${prop.name}`)};
`)}
}
` : deindent`
${$$props} => {
return
}
`;
}

// instrument assignments
Expand Down Expand Up @@ -430,6 +453,10 @@ export default function dom(

${set && `$$self.$set = ${set};`}

${capture_state && `$$self.$capture_state = ${capture_state};`}

${inject_state && `$$self.$inject_state = ${inject_state};`}

${injected.length && `let ${injected.join(', ')};`}

${reactive_declarations.length > 0 && deindent`
Expand Down
5 changes: 5 additions & 0 deletions test/js/samples/capture-inject-dev-only/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
options: {
dev: false
}
};
79 changes: 79 additions & 0 deletions test/js/samples/capture-inject-dev-only/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
append,
detach,
element,
init,
insert,
listen,
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

function create_fragment(ctx) {
var p, t0, t1, input, dispose;

return {
c() {
p = element("p");
t0 = text(ctx.foo);
t1 = space();
input = element("input");
dispose = listen(input, "input", ctx.input_input_handler);
},

m(target, anchor) {
insert(target, p, anchor);
append(p, t0);
insert(target, t1, anchor);
insert(target, input, anchor);

input.value = ctx.foo;
},

p(changed, ctx) {
if (changed.foo) {
set_data(t0, ctx.foo);
}

if (changed.foo && (input.value !== ctx.foo)) input.value = ctx.foo;
},

i: noop,
o: noop,

d(detaching) {
if (detaching) {
detach(p);
detach(t1);
detach(input);
}

dispose();
}
};
}

function instance($$self, $$props, $$invalidate) {
let foo = "bar";

function input_input_handler() {
foo = this.value;
$$invalidate('foo', foo);
}

return { foo, input_input_handler };
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, []);
}
}

export default Component;
5 changes: 5 additions & 0 deletions test/js/samples/capture-inject-dev-only/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let foo = "bar";
</script>
<p>{foo}</p>
<input bind:value={foo}>
8 changes: 8 additions & 0 deletions test/js/samples/debug-empty/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ function instance($$self, $$props, $$invalidate) {
if ('name' in $$props) $$invalidate('name', name = $$props.name);
};

$$self.$capture_state = () => {
return { name };
};

$$self.$inject_state = $$props => {
if ('name' in $$props) $$invalidate('name', name = $$props.name);
};

return { name };
}

Expand Down
11 changes: 11 additions & 0 deletions test/js/samples/debug-foo-bar-baz-things/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ function instance($$self, $$props, $$invalidate) {
if ('baz' in $$props) $$invalidate('baz', baz = $$props.baz);
};

$$self.$capture_state = () => {
return { things, foo, bar, baz };
};

$$self.$inject_state = $$props => {
if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
if ('baz' in $$props) $$invalidate('baz', baz = $$props.baz);
};

return { things, foo, bar, baz };
}

Expand Down
9 changes: 9 additions & 0 deletions test/js/samples/debug-foo/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ function instance($$self, $$props, $$invalidate) {
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
};

$$self.$capture_state = () => {
return { things, foo };
};

$$self.$inject_state = $$props => {
if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
};

return { things, foo };
}

Expand Down
8 changes: 8 additions & 0 deletions test/js/samples/debug-hoisted/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ let kobzol = 5;
function instance($$self) {
let obj = { x: 5 };

$$self.$capture_state = () => {
return {}
};

$$self.$inject_state = $$props => {
return
};

return { obj };
}

Expand Down
9 changes: 9 additions & 0 deletions test/js/samples/dev-warning-missing-data-computed/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ function instance($$self, $$props, $$invalidate) {
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
};

$$self.$capture_state = () => {
return { foo, bar };
};

$$self.$inject_state = $$props => {
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
};

$$self.$$.update = ($$dirty = { foo: 1 }) => {
if ($$dirty.foo) { $$invalidate('bar', bar = foo * 2); }
};
Expand Down