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

Share more init work #1670

Merged
merged 2 commits into from
Aug 23, 2018
Merged
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
42 changes: 11 additions & 31 deletions src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,6 @@ export default function dom(
`if (!document.getElementById("${compiler.stylesheet.id}-style")) @add_css();`)
}

${(hasInitHooks || compiler.hasComponents || target.hasComplexBindings || target.hasIntroTransitions) && deindent`
if (!options.root) {
this._oncreate = [];
${(compiler.hasComponents || target.hasComplexBindings) && `this._beforecreate = [];`}
${(compiler.hasComponents || target.hasIntroTransitions) && `this._aftercreate = [];`}
}
`}

${compiler.slots.size && `this.slots = {};`}

${templateProperties.onstate && `%onstate.call(this, { changed: @assignTrue({}, this._state), current: this._state });`}

this._fragment = @create_main_fragment(this, this._state);
Expand All @@ -241,24 +231,18 @@ export default function dom(
` : deindent`
if (options.target) {
${compiler.options.hydratable
? deindent`
var nodes = @children(options.target);
options.hydrate ? this._fragment.l(nodes) : this._fragment.c();
nodes.forEach(@detachNode);
` :
deindent`
${options.dev && `if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the \`hydratable: true\` option");`}
this._fragment.c();
`}
? deindent`
var nodes = @children(options.target);
options.hydrate ? this._fragment.l(nodes) : this._fragment.c();
nodes.forEach(@detachNode);` :
deindent`
${options.dev &&
`if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the \`hydratable: true\` option");`}
this._fragment.c();`}
this._mount(options.target, options.anchor);

${(compiler.hasComponents || target.hasComplexBindings || hasInitHooks || target.hasIntroTransitions) && deindent`
${compiler.hasComponents && `this._lock = true;`}
${(compiler.hasComponents || target.hasComplexBindings) && `@callAll(this._beforecreate);`}
${(compiler.hasComponents || hasInitHooks) && `@callAll(this._oncreate);`}
${(compiler.hasComponents || target.hasIntroTransitions) && `@callAll(this._aftercreate);`}
${compiler.hasComponents && `this._lock = false;`}
`}
${(compiler.hasComponents || target.hasComplexBindings || hasInitHooks || target.hasIntroTransitions) &&
`@flush(this);`}
}
`}

Expand Down Expand Up @@ -302,11 +286,7 @@ export default function dom(

${(compiler.hasComponents || target.hasComplexBindings || templateProperties.oncreate || target.hasIntroTransitions) && deindent`
connectedCallback() {
${compiler.hasComponents && `this._lock = true;`}
${(compiler.hasComponents || target.hasComplexBindings) && `@callAll(this._beforecreate);`}
${(compiler.hasComponents || templateProperties.oncreate) && `@callAll(this._oncreate);`}
${(compiler.hasComponents || target.hasIntroTransitions) && `@callAll(this._aftercreate);`}
${compiler.hasComponents && `this._lock = false;`}
@flush(this);
}
`}
}
Expand Down
21 changes: 16 additions & 5 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,32 @@ export function fire(eventName, data) {
}
}

export function flush(component) {
component._lock = true;
callAll(component._beforecreate);
callAll(component._oncreate);
callAll(component._aftercreate);
component._lock = false;
}

export function get() {
return this._state;
}

export function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;

component.options = options;
component.root = options.root || component;
component.store = options.store || component.root.store;

if (!options.root) {
component._beforecreate = [];
component._oncreate = [];
component._aftercreate = [];
}
}

export function on(eventName, handler) {
Expand All @@ -89,11 +104,7 @@ export function run(fn) {
export function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
this.root._lock = true;
callAll(this.root._beforecreate);
callAll(this.root._oncreate);
callAll(this.root._aftercreate);
this.root._lock = false;
flush(this.root);
}

export function _set(newState) {
Expand Down
21 changes: 16 additions & 5 deletions test/cli/samples/basic/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ function detachNode(node) {

function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;

component.options = options;
component.root = options.root || component;
component.store = options.store || component.root.store;

if (!options.root) {
component._beforecreate = [];
component._oncreate = [];
component._aftercreate = [];
}
}

function assign(tar, src) {
Expand Down Expand Up @@ -125,11 +132,7 @@ function on(eventName, handler) {
function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
this.root._lock = true;
callAll(this.root._beforecreate);
callAll(this.root._oncreate);
callAll(this.root._aftercreate);
this.root._lock = false;
flush(this.root);
}

function _set(newState) {
Expand Down Expand Up @@ -165,6 +168,14 @@ function blankObject() {
return Object.create(null);
}

function flush(component) {
component._lock = true;
callAll(component._beforecreate);
callAll(component._oncreate);
callAll(component._aftercreate);
component._lock = false;
}

function callAll(fns) {
while (fns && fns.length) fns.shift()();
}
Expand Down
21 changes: 16 additions & 5 deletions test/cli/samples/custom-element/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ function detachNode(node) {

function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;

component.options = options;
component.root = options.root || component;
component.store = options.store || component.root.store;

if (!options.root) {
component._beforecreate = [];
component._oncreate = [];
component._aftercreate = [];
}
}

function assign(tar, src) {
Expand Down Expand Up @@ -146,11 +153,7 @@ function on(eventName, handler) {
function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
this.root._lock = true;
callAll(this.root._beforecreate);
callAll(this.root._oncreate);
callAll(this.root._aftercreate);
this.root._lock = false;
flush(this.root);
}

function _set(newState) {
Expand Down Expand Up @@ -186,6 +189,14 @@ function blankObject() {
return Object.create(null);
}

function flush(component) {
component._lock = true;
callAll(component._beforecreate);
callAll(component._oncreate);
callAll(component._aftercreate);
component._lock = false;
}

function callAll(fns) {
while (fns && fns.length) fns.shift()();
}
Expand Down
21 changes: 16 additions & 5 deletions test/cli/samples/dev/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ function detachNode(node) {

function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;

component.options = options;
component.root = options.root || component;
component.store = options.store || component.root.store;

if (!options.root) {
component._beforecreate = [];
component._oncreate = [];
component._aftercreate = [];
}
}

function assign(tar, src) {
Expand Down Expand Up @@ -200,11 +207,15 @@ function destroy(detach) {
function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
this.root._lock = true;
callAll(this.root._beforecreate);
callAll(this.root._oncreate);
callAll(this.root._aftercreate);
this.root._lock = false;
flush(this.root);
}

function flush(component) {
component._lock = true;
callAll(component._beforecreate);
callAll(component._oncreate);
callAll(component._aftercreate);
component._lock = false;
}

function callAll(fns) {
Expand Down
37 changes: 19 additions & 18 deletions test/cli/samples/dir-sourcemap/expected/Main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cli/samples/dir-sourcemap/expected/Main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions test/cli/samples/dir-sourcemap/expected/Widget.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cli/samples/dir-sourcemap/expected/Widget.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading