From 5e41ecb34aedefea17c81a6ca284d75b0dc56502 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 26 Jan 2018 13:27:58 -0500 Subject: [PATCH] run oncreate functions in sensible order (#1135) --- src/generators/dom/index.ts | 12 ++++++------ src/shared/index.js | 2 +- .../samples/oncreate-sibling-order/Nested.html | 11 +++++++++++ .../samples/oncreate-sibling-order/_config.js | 13 +++++++++++++ .../samples/oncreate-sibling-order/main.html | 12 ++++++++++++ .../samples/oncreate-sibling-order/result.js | 1 + 6 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 test/runtime/samples/oncreate-sibling-order/Nested.html create mode 100644 test/runtime/samples/oncreate-sibling-order/_config.js create mode 100644 test/runtime/samples/oncreate-sibling-order/main.html create mode 100644 test/runtime/samples/oncreate-sibling-order/result.js diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index c124f05bb5..776cf1ab0b 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -245,20 +245,20 @@ export default function dom( ${(templateProperties.oncreate || generator.hasComponents || generator.hasComplexBindings || generator.hasIntroTransitions) && deindent` if (!options.root) { - this._oncreate = [${templateProperties.oncreate && `_oncreate`}]; + this._oncreate = []; ${(generator.hasComponents || generator.hasComplexBindings) && `this._beforecreate = [];`} ${(generator.hasComponents || generator.hasIntroTransitions) && `this._aftercreate = [];`} - } ${templateProperties.oncreate && deindent` - else { - this.root._oncreate.push(_oncreate); - } - `} + } `} ${generator.slots.size && `this.slots = {};`} this._fragment = @create_main_fragment(this._state, this); + ${(templateProperties.oncreate) && deindent` + this.root._oncreate.push(_oncreate); + `} + ${generator.customElement ? deindent` this._fragment.c(); this._fragment.${block.hasIntroMethod ? 'i' : 'm'}(this.shadowRoot, null); diff --git a/src/shared/index.js b/src/shared/index.js index 3adae010c2..ef160ebf07 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -177,7 +177,7 @@ export function setDev(newState) { } export function callAll(fns) { - while (fns && fns.length) fns.pop()(); + while (fns && fns.length) fns.shift()(); } export function _mount(target, anchor) { diff --git a/test/runtime/samples/oncreate-sibling-order/Nested.html b/test/runtime/samples/oncreate-sibling-order/Nested.html new file mode 100644 index 0000000000..03bd443a53 --- /dev/null +++ b/test/runtime/samples/oncreate-sibling-order/Nested.html @@ -0,0 +1,11 @@ +

{{name}}

+ + \ No newline at end of file diff --git a/test/runtime/samples/oncreate-sibling-order/_config.js b/test/runtime/samples/oncreate-sibling-order/_config.js new file mode 100644 index 0000000000..4e28940b74 --- /dev/null +++ b/test/runtime/samples/oncreate-sibling-order/_config.js @@ -0,0 +1,13 @@ +import result from './result.js'; + +export default { + test(assert) { + assert.deepEqual(result, [ + 'oncreate foo', + 'oncreate bar' + ]); + + result.pop(); + result.pop(); + } +}; diff --git a/test/runtime/samples/oncreate-sibling-order/main.html b/test/runtime/samples/oncreate-sibling-order/main.html new file mode 100644 index 0000000000..ba594477f0 --- /dev/null +++ b/test/runtime/samples/oncreate-sibling-order/main.html @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/test/runtime/samples/oncreate-sibling-order/result.js b/test/runtime/samples/oncreate-sibling-order/result.js new file mode 100644 index 0000000000..109fa8b38c --- /dev/null +++ b/test/runtime/samples/oncreate-sibling-order/result.js @@ -0,0 +1 @@ +export default []; \ No newline at end of file