make _recompute a class method

pull/1359/head
Rich Harris 8 years ago
parent b606dd62bb
commit 56f00b8e77

@ -260,6 +260,14 @@ export default function dom(
`}
`;
const classMethods = [];
if (computations.length) {
classMethods.push(deindent`
_recompute(changed, state) {
${computationBuilder}
}`);
}
if (generator.customElement) {
const props = generator.props || Array.from(generator.expectedProperties);
@ -277,34 +285,35 @@ export default function dom(
}
${props.map(prop => deindent`
get ${prop}() {
return this.get().${prop};
}
get ${prop}() {
return this.get().${prop};
}
set ${prop}(value) {
this.set({ ${prop}: value });
}
`).join('\n\n')}
set ${prop}(value) {
this.set({ ${prop}: value });
}`).join('\n\n')}
${classMethods.length && classMethods.join('\n\n')}
${generator.slots.size && deindent`
connectedCallback() {
Object.keys(this._slotted).forEach(key => {
this.appendChild(this._slotted[key]);
});
}`}
connectedCallback() {
Object.keys(this._slotted).forEach(key => {
this.appendChild(this._slotted[key]);
});
}`}
attributeChangedCallback(attr, oldValue, newValue) {
this.set({ [attr]: newValue });
}
${(generator.hasComponents || generator.hasComplexBindings || templateProperties.oncreate || generator.hasIntroTransitions) && deindent`
connectedCallback() {
${generator.hasComponents && `this._lock = true;`}
${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`}
${(generator.hasComponents || templateProperties.oncreate) && `@callAll(this._oncreate);`}
${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`}
${generator.hasComponents && `this._lock = false;`}
}
connectedCallback() {
${generator.hasComponents && `this._lock = true;`}
${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`}
${(generator.hasComponents || templateProperties.oncreate) && `@callAll(this._oncreate);`}
${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`}
${generator.hasComponents && `this._lock = false;`}
}
`}
}
@ -337,6 +346,8 @@ export default function dom(
super(options);
${constructorBody}
}
${classMethods.length && classMethods.join('\n\n')}
}
${templateProperties.methods && `@assign(${name}.prototype, %methods);`}
@ -355,12 +366,6 @@ export default function dom(
};
`}
${computations.length ? deindent`
${name}.prototype._recompute = function _recompute(changed, state) {
${computationBuilder}
}
` : (!sharedPath && `${name}.prototype._recompute = @noop;`)}
${templateProperties.setup && `%setup(${name});`}
${templateProperties.preload && `${name}.preload = %preload;`}

@ -163,13 +163,13 @@ class SvelteComponent extends Component {
this._mount(options.target, options.anchor);
}
}
}
SvelteComponent.prototype._recompute = function _recompute(changed, state) {
if (changed.x) {
if (this._differs(state.a, (state.a = a(state)))) changed.a = true;
if (this._differs(state.b, (state.b = b(state)))) changed.b = true;
_recompute(changed, state) {
if (changed.x) {
if (this._differs(state.a, (state.a = a(state)))) changed.a = true;
if (this._differs(state.b, (state.b = b(state)))) changed.b = true;
}
}
};
}
export default SvelteComponent;

@ -37,12 +37,12 @@ class SvelteComponent extends Component {
this._mount(options.target, options.anchor);
}
}
}
SvelteComponent.prototype._recompute = function _recompute(changed, state) {
if (changed.x) {
if (this._differs(state.a, (state.a = a(state)))) changed.a = true;
if (this._differs(state.b, (state.b = b(state)))) changed.b = true;
_recompute(changed, state) {
if (changed.x) {
if (this._differs(state.a, (state.a = a(state)))) changed.a = true;
if (this._differs(state.b, (state.b = b(state)))) changed.b = true;
}
}
}
export default SvelteComponent;

@ -229,16 +229,16 @@ class SvelteComponent extends ComponentDev {
this._mount(options.target, options.anchor);
}
}
_recompute(changed, state) {
if (changed.foo) {
if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true;
}
}
}
SvelteComponent.prototype._checkReadOnly = function _checkReadOnly(newState) {
if ('bar' in newState && !this._updatingReadonlyProperty) throw new Error("<SvelteComponent>: Cannot set read-only property 'bar'");
};
SvelteComponent.prototype._recompute = function _recompute(changed, state) {
if (changed.foo) {
if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true;
}
};
export default SvelteComponent;

@ -57,15 +57,15 @@ class SvelteComponent extends ComponentDev {
this._mount(options.target, options.anchor);
}
}
_recompute(changed, state) {
if (changed.foo) {
if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true;
}
}
}
SvelteComponent.prototype._checkReadOnly = function _checkReadOnly(newState) {
if ('bar' in newState && !this._updatingReadonlyProperty) throw new Error("<SvelteComponent>: Cannot set read-only property 'bar'");
};
SvelteComponent.prototype._recompute = function _recompute(changed, state) {
if (changed.foo) {
if (this._differs(state.bar, (state.bar = bar(state)))) changed.bar = true;
}
}
export default SvelteComponent;
Loading…
Cancel
Save