Skip to content

Commit

Permalink
Merge pull request #855 from sveltejs/shared-init
Browse files Browse the repository at this point in the history
extract some shared init logic
  • Loading branch information
Rich-Harris authored Sep 17, 2017
2 parents 9d8f2c4 + 33dbc18 commit f394cac
Show file tree
Hide file tree
Showing 43 changed files with 480 additions and 633 deletions.
11 changes: 1 addition & 10 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default function dom(
${options.dev && `this._debugName = '${debugName}';`}
${options.dev && !generator.customElement &&
`if (!options || (!options.target && !options._root)) throw new Error("'target' is a required option");`}
this.options = options;
@init(this, options);
${generator.usesRefs && `this.refs = {};`}
this._state = ${templateProperties.data
? `@assign(@template.data(), options.data)`
Expand All @@ -169,17 +169,8 @@ export default function dom(
${generator.bindingGroups.length &&
`this._bindingGroups = [${Array(generator.bindingGroups.length).fill('[]').join(', ')}];`}
this._observers = {
pre: Object.create(null),
post: Object.create(null)
};
this._handlers = Object.create(null);
${templateProperties.ondestroy && `this._handlers.destroy = [@template.ondestroy]`}
this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;
${generator.slots.size && `this._slotted = options.slots || {};`}
${generator.customElement ?
Expand Down
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function keyed(
const last = block.getUniqueName(`${each_block}_last`);
const expected = block.getUniqueName(`${each_block}_expected`);

block.addVariable(lookup, `Object.create(null)`);
block.addVariable(lookup, `@blankObject()`);
block.addVariable(head);
block.addVariable(last);

Expand Down
22 changes: 18 additions & 4 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export * from './dom.js';
export * from './transitions.js';
export * from './utils.js';

export function blankObject() {
return Object.create(null);
}

export function destroy(detach) {
this.destroy = noop;
this.fire('destroy');
Expand Down Expand Up @@ -46,10 +50,6 @@ export function dispatchObservers(component, group, changed, newState, oldState)
}
}

export function get(key) {
return key ? this._state[key] : this._state;
}

export function fire(eventName, data) {
var handlers =
eventName in this._handlers && this._handlers[eventName].slice();
Expand All @@ -60,6 +60,20 @@ export function fire(eventName, data) {
}
}

export function get(key) {
return key ? this._state[key] : this._state;
}

export function init(component, options) {
component.options = options;

component._observers = { pre: blankObject(), post: blankObject() };
component._handlers = blankObject();
component._root = options._root || component;
component._yield = options._yield;
component._bind = options._bind;
}

export function observe(key, callback, options) {
var group = options && options.defer
? this._observers.post
Expand Down
37 changes: 20 additions & 17 deletions test/js/samples/collapses-text-around-comments/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function setAttribute(node, attribute, value) {
node.setAttribute(attribute, value);
}

function blankObject() {
return Object.create(null);
}

function destroy(detach) {
this.destroy = noop;
this.fire('destroy');
Expand Down Expand Up @@ -72,10 +76,6 @@ function dispatchObservers(component, group, changed, newState, oldState) {
}
}

function get(key) {
return key ? this._state[key] : this._state;
}

function fire(eventName, data) {
var handlers =
eventName in this._handlers && this._handlers[eventName].slice();
Expand All @@ -86,6 +86,20 @@ function fire(eventName, data) {
}
}

function get(key) {
return key ? this._state[key] : this._state;
}

function init(component, options) {
component.options = options;

component._observers = { pre: blankObject(), post: blankObject() };
component._handlers = blankObject();
component._root = options._root || component;
component._yield = options._yield;
component._bind = options._bind;
}

function observe(key, callback, options) {
var group = options && options.defer
? this._observers.post
Expand Down Expand Up @@ -175,7 +189,7 @@ var proto = {
_unmount: _unmount
};

/* generated by Svelte v1.39.2 */
/* generated by Svelte v1.39.3 */

var template = (function() {
return {
Expand Down Expand Up @@ -230,20 +244,9 @@ function create_main_fragment(state, component) {
}

function SvelteComponent(options) {
this.options = options;
init(this, options);
this._state = assign(template.data(), options.data);

this._observers = {
pre: Object.create(null),
post: Object.create(null)
};

this._handlers = Object.create(null);

this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;

if (!document.getElementById("svelte-3590263702-style")) add_css();

this._fragment = create_main_fragment(this._state, this);
Expand Down
17 changes: 3 additions & 14 deletions test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* generated by Svelte v1.39.2 */
/* generated by Svelte v1.39.3 */

import { appendNode, assign, createElement, createText, detachNode, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";

var template = (function() {
return {
Expand Down Expand Up @@ -55,20 +55,9 @@ function create_main_fragment(state, component) {
}

function SvelteComponent(options) {
this.options = options;
init(this, options);
this._state = assign(template.data(), options.data);

this._observers = {
pre: Object.create(null),
post: Object.create(null)
};

this._handlers = Object.create(null);

this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;

if (!document.getElementById("svelte-3590263702-style")) add_css();

this._fragment = create_main_fragment(this._state, this);
Expand Down
37 changes: 20 additions & 17 deletions test/js/samples/computed-collapsed-if/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function assign(target) {
return target;
}

function blankObject() {
return Object.create(null);
}

function destroy(detach) {
this.destroy = noop;
this.fire('destroy');
Expand Down Expand Up @@ -48,10 +52,6 @@ function dispatchObservers(component, group, changed, newState, oldState) {
}
}

function get(key) {
return key ? this._state[key] : this._state;
}

function fire(eventName, data) {
var handlers =
eventName in this._handlers && this._handlers[eventName].slice();
Expand All @@ -62,6 +62,20 @@ function fire(eventName, data) {
}
}

function get(key) {
return key ? this._state[key] : this._state;
}

function init(component, options) {
component.options = options;

component._observers = { pre: blankObject(), post: blankObject() };
component._handlers = blankObject();
component._root = options._root || component;
component._yield = options._yield;
component._bind = options._bind;
}

function observe(key, callback, options) {
var group = options && options.defer
? this._observers.post
Expand Down Expand Up @@ -151,7 +165,7 @@ var proto = {
_unmount: _unmount
};

/* generated by Svelte v1.39.2 */
/* generated by Svelte v1.39.3 */

var template = (function() {
return {
Expand All @@ -178,21 +192,10 @@ function create_main_fragment(state, component) {
}

function SvelteComponent(options) {
this.options = options;
init(this, options);
this._state = options.data || {};
this._recompute({}, this._state, {}, true);

this._observers = {
pre: Object.create(null),
post: Object.create(null)
};

this._handlers = Object.create(null);

this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;

this._fragment = create_main_fragment(this._state, this);

if (options.target) {
Expand Down
17 changes: 3 additions & 14 deletions test/js/samples/computed-collapsed-if/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* generated by Svelte v1.39.2 */
/* generated by Svelte v1.39.3 */

import { assign, differs, noop, proto } from "svelte/shared.js";
import { assign, differs, init, noop, proto } from "svelte/shared.js";

var template = (function() {
return {
Expand All @@ -27,21 +27,10 @@ function create_main_fragment(state, component) {
}

function SvelteComponent(options) {
this.options = options;
init(this, options);
this._state = options.data || {};
this._recompute({}, this._state, {}, true);

this._observers = {
pre: Object.create(null),
post: Object.create(null)
};

this._handlers = Object.create(null);

this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;

this._fragment = create_main_fragment(this._state, this);

if (options.target) {
Expand Down
37 changes: 20 additions & 17 deletions test/js/samples/css-media-query/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ function setAttribute(node, attribute, value) {
node.setAttribute(attribute, value);
}

function blankObject() {
return Object.create(null);
}

function destroy(detach) {
this.destroy = noop;
this.fire('destroy');
Expand Down Expand Up @@ -68,10 +72,6 @@ function dispatchObservers(component, group, changed, newState, oldState) {
}
}

function get(key) {
return key ? this._state[key] : this._state;
}

function fire(eventName, data) {
var handlers =
eventName in this._handlers && this._handlers[eventName].slice();
Expand All @@ -82,6 +82,20 @@ function fire(eventName, data) {
}
}

function get(key) {
return key ? this._state[key] : this._state;
}

function init(component, options) {
component.options = options;

component._observers = { pre: blankObject(), post: blankObject() };
component._handlers = blankObject();
component._root = options._root || component;
component._yield = options._yield;
component._bind = options._bind;
}

function observe(key, callback, options) {
var group = options && options.defer
? this._observers.post
Expand Down Expand Up @@ -171,7 +185,7 @@ var proto = {
_unmount: _unmount
};

/* generated by Svelte v1.39.2 */
/* generated by Svelte v1.39.3 */

function encapsulateStyles(node) {
setAttribute(node, "svelte-2363328337", "");
Expand Down Expand Up @@ -212,20 +226,9 @@ function create_main_fragment(state, component) {
}

function SvelteComponent(options) {
this.options = options;
init(this, options);
this._state = options.data || {};

this._observers = {
pre: Object.create(null),
post: Object.create(null)
};

this._handlers = Object.create(null);

this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;

if (!document.getElementById("svelte-2363328337-style")) add_css();

this._fragment = create_main_fragment(this._state, this);
Expand Down
17 changes: 3 additions & 14 deletions test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* generated by Svelte v1.39.2 */
/* generated by Svelte v1.39.3 */

import { appendNode, assign, createElement, detachNode, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";

function encapsulateStyles(node) {
setAttribute(node, "svelte-2363328337", "");
Expand Down Expand Up @@ -41,20 +41,9 @@ function create_main_fragment(state, component) {
}

function SvelteComponent(options) {
this.options = options;
init(this, options);
this._state = options.data || {};

this._observers = {
pre: Object.create(null),
post: Object.create(null)
};

this._handlers = Object.create(null);

this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;

if (!document.getElementById("svelte-2363328337-style")) add_css();

this._fragment = create_main_fragment(this._state, this);
Expand Down
Loading

0 comments on commit f394cac

Please sign in to comment.