From 00fb9789243aa70f18f9b944ea5c40fc8334a462 Mon Sep 17 00:00:00 2001 From: pk Date: Sun, 22 Apr 2018 11:16:56 +0200 Subject: [PATCH 1/3] Failing test for #1356 --- test/runtime/samples/onstate-no-template/_config.js | 11 +++++++++++ test/runtime/samples/onstate-no-template/main.html | 7 +++++++ 2 files changed, 18 insertions(+) create mode 100644 test/runtime/samples/onstate-no-template/_config.js create mode 100644 test/runtime/samples/onstate-no-template/main.html 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..fa1db69c96 --- /dev/null +++ b/test/runtime/samples/onstate-no-template/main.html @@ -0,0 +1,7 @@ + From 991bd9dce85fd8f72ca3ef89caba31a82aae5338 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 22 Apr 2018 10:36:30 -0400 Subject: [PATCH 2/3] correctly report changed properties in initial state/update events - fixes #1356 --- src/generators/dom/index.ts | 16 +++++---------- src/shared/utils.js | 8 ++++++++ .../deconflict-globals/expected-bundle.js | 20 +++++++++++-------- .../js/samples/deconflict-globals/expected.js | 14 +++++-------- .../samples/onstate-no-template/main.html | 2 +- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 9ae999d669..0a4b48b92f 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: @makePropertyMap(this._state), current: this._state });`} + ${templateProperties.oncreate && `%oncreate.call(this);`} + this.fire("update", { changed: @makePropertyMap(this._state), current: this._state }); + }); `} ${generator.customElement ? deindent` diff --git a/src/shared/utils.js b/src/shared/utils.js index c9a0f83449..be01c7a131 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -4,3 +4,11 @@ export function assign(tar, src) { for (var k in src) tar[k] = src[k]; return tar; } + +export function makePropertyMap(obj) { + const map = {}; + for (const key in obj) { + map[key] = 1; + } + return map; +} \ 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 214eed9578..09143c55bc 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -5,6 +5,14 @@ function assign(tar, src) { return tar; } +function makePropertyMap(obj) { + const map = {}; + for (const key in obj) { + map[key] = 1; + } + return map; +} + function blankObject() { return Object.create(null); } @@ -150,20 +158,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: makePropertyMap(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..b4b7633dcb 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, callAll, init, makePropertyMap, 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: makePropertyMap(this._state), current: this._state }); + }); if (options.target) { this._fragment.c(); diff --git a/test/runtime/samples/onstate-no-template/main.html b/test/runtime/samples/onstate-no-template/main.html index fa1db69c96..a3c59f99be 100644 --- a/test/runtime/samples/onstate-no-template/main.html +++ b/test/runtime/samples/onstate-no-template/main.html @@ -1,7 +1,7 @@ From 8643b7ac887f465fdaa34561e6cb706381fb8ddf Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 22 Apr 2018 10:47:37 -0400 Subject: [PATCH 3/3] tweak --- src/generators/dom/index.ts | 4 ++-- src/shared/utils.js | 9 +++------ test/js/samples/deconflict-globals/expected-bundle.js | 11 ++++------- test/js/samples/deconflict-globals/expected.js | 4 ++-- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 0a4b48b92f..337d23d2ed 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -232,9 +232,9 @@ export default function dom( ${hasInitHooks && deindent` this.root._oncreate.push(() => { - ${templateProperties.onstate && `%onstate.call(this, { changed: @makePropertyMap(this._state), current: this._state });`} + ${templateProperties.onstate && `%onstate.call(this, { changed: @assignTrue({}, this._state), current: this._state });`} ${templateProperties.oncreate && `%oncreate.call(this);`} - this.fire("update", { changed: @makePropertyMap(this._state), current: this._state }); + this.fire("update", { changed: @assignTrue({}, this._state), current: this._state }); }); `} diff --git a/src/shared/utils.js b/src/shared/utils.js index be01c7a131..45260d49bc 100644 --- a/src/shared/utils.js +++ b/src/shared/utils.js @@ -5,10 +5,7 @@ export function assign(tar, src) { return tar; } -export function makePropertyMap(obj) { - const map = {}; - for (const key in obj) { - map[key] = 1; - } - return map; +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 09143c55bc..ab0773f324 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -5,12 +5,9 @@ function assign(tar, src) { return tar; } -function makePropertyMap(obj) { - const map = {}; - for (const key in obj) { - map[key] = 1; - } - return map; +function assignTrue(tar, src) { + for (var k in src) tar[k] = 1; + return tar; } function blankObject() { @@ -166,7 +163,7 @@ function SvelteComponent(options) { this.root._oncreate.push(() => { oncreate.call(this); - this.fire("update", { changed: makePropertyMap(this._state), current: this._state }); + this.fire("update", { changed: assignTrue({}, this._state), current: this._state }); }); if (options.target) { diff --git a/test/js/samples/deconflict-globals/expected.js b/test/js/samples/deconflict-globals/expected.js index b4b7633dcb..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, makePropertyMap, noop, proto } from "svelte/shared.js"; +import { assign, assignTrue, callAll, init, noop, proto } from "svelte/shared.js"; function data_1() { return { @@ -38,7 +38,7 @@ function SvelteComponent(options) { this.root._oncreate.push(() => { oncreate.call(this); - this.fire("update", { changed: makePropertyMap(this._state), current: this._state }); + this.fire("update", { changed: assignTrue({}, this._state), current: this._state }); }); if (options.target) {