diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 9ae999d669..337d23d2ed 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -218,16 +218,6 @@ export default function dom( `if (!document.getElementById("${generator.stylesheet.id}-style")) @add_css();`) } - ${hasInitHooks && deindent` - var self = this; - var _oncreate = function() { - var changed = { ${expectedProperties.map(p => `${p}: 1`).join(', ')} }; - ${templateProperties.onstate && `%onstate.call(self, { changed: changed, current: self._state });`} - ${templateProperties.oncreate && `%oncreate.call(self);`} - self.fire("update", { changed: changed, current: self._state }); - }; - `} - ${(hasInitHooks || generator.hasComponents || generator.hasComplexBindings || generator.hasIntroTransitions) && deindent` if (!options.root) { this._oncreate = []; @@ -241,7 +231,11 @@ export default function dom( this._fragment = @create_main_fragment(this, this._state); ${hasInitHooks && deindent` - this.root._oncreate.push(_oncreate); + this.root._oncreate.push(() => { + ${templateProperties.onstate && `%onstate.call(this, { changed: @assignTrue({}, this._state), current: this._state });`} + ${templateProperties.oncreate && `%oncreate.call(this);`} + this.fire("update", { changed: @assignTrue({}, this._state), current: this._state }); + }); `} ${generator.customElement ? deindent` diff --git a/src/shared/utils.js b/src/shared/utils.js index c9a0f83449..45260d49bc 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -4,3 +4,8 @@ export function assign(tar, src) { for (var k in src) tar[k] = src[k]; return tar; } + +export function assignTrue(tar, src) { + for (var k in src) tar[k] = 1; + return tar; +} \ No newline at end of file diff --git a/test/js/samples/deconflict-globals/expected-bundle.js b/test/js/samples/deconflict-globals/expected-bundle.js index 5f02ccb9f3..2db12d46fb 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -5,6 +5,11 @@ function assign(tar, src) { return tar; } +function assignTrue(tar, src) { + for (var k in src) tar[k] = 1; + return tar; +} + function blankObject() { return Object.create(null); } @@ -151,20 +156,16 @@ function SvelteComponent(options) { init(this, options); this._state = assign(data_1(), options.data); - var self = this; - var _oncreate = function() { - var changed = { }; - oncreate.call(self); - self.fire("update", { changed: changed, current: self._state }); - }; - if (!options.root) { this._oncreate = []; } this._fragment = create_main_fragment(this, this._state); - this.root._oncreate.push(_oncreate); + this.root._oncreate.push(() => { + oncreate.call(this); + this.fire("update", { changed: assignTrue({}, this._state), current: this._state }); + }); if (options.target) { this._fragment.c(); diff --git a/test/js/samples/deconflict-globals/expected.js b/test/js/samples/deconflict-globals/expected.js index 91192f4cca..8b91d1550a 100644 --- a/test/js/samples/deconflict-globals/expected.js +++ b/test/js/samples/deconflict-globals/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, callAll, init, noop, proto } from "svelte/shared.js"; +import { assign, assignTrue, callAll, init, noop, proto } from "svelte/shared.js"; function data_1() { return { @@ -30,20 +30,16 @@ function SvelteComponent(options) { init(this, options); this._state = assign(data_1(), options.data); - var self = this; - var _oncreate = function() { - var changed = { }; - oncreate.call(self); - self.fire("update", { changed: changed, current: self._state }); - }; - if (!options.root) { this._oncreate = []; } this._fragment = create_main_fragment(this, this._state); - this.root._oncreate.push(_oncreate); + this.root._oncreate.push(() => { + oncreate.call(this); + this.fire("update", { changed: assignTrue({}, this._state), current: this._state }); + }); if (options.target) { this._fragment.c(); diff --git a/test/runtime/samples/onstate-no-template/_config.js b/test/runtime/samples/onstate-no-template/_config.js new file mode 100644 index 0000000000..87d7c599ce --- /dev/null +++ b/test/runtime/samples/onstate-no-template/_config.js @@ -0,0 +1,11 @@ +export default { + 'skip-ssr': true, + + data: { + foo: 'woo!' + }, + + test(assert, component) { + assert.deepEqual(component.changed, { foo: 1 }); + } +}; diff --git a/test/runtime/samples/onstate-no-template/main.html b/test/runtime/samples/onstate-no-template/main.html new file mode 100644 index 0000000000..a3c59f99be --- /dev/null +++ b/test/runtime/samples/onstate-no-template/main.html @@ -0,0 +1,7 @@ +