handle set after destroy, and move destroy into shared helpers

pull/755/head
Rich Harris 8 years ago
parent 8dd23b81df
commit 86fb0e4ced

@ -174,8 +174,8 @@ export default function dom(
? `@proto `
: deindent`
{
${['get', 'fire', 'observe', 'on', 'set']
.map(n => `${n}: @${n}`)
${['destroy', 'get', 'fire', 'observe', 'on', 'set', 'teardown']
.map(n => `${n}: @${n === 'teardown' ? 'destroy' : n}`)
.join(',\n')}
}`;
@ -207,6 +207,7 @@ export default function dom(
};
this._handlers = Object.create( null );
${templateProperties.ondestroy && `this._handlers.destroy = [@template.ondestroy.bind(this)]`}
this._root = options._root || this;
this._yield = options._yield;
@ -263,19 +264,6 @@ export default function dom(
${_set}
};
${name}.prototype.teardown = ${name}.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return${options.dev && ` console.warn( 'Component was already destroyed' )`};
this.fire( 'destroy' );
${templateProperties.ondestroy && `@template.ondestroy.call( this );`}
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
${templateProperties.setup && `@template.setup( ${name} );`}
`);

@ -3,6 +3,24 @@ export * from './dom.js';
export * from './transitions.js';
export * from './utils.js';
export function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
export function destroyDev(detach) {
if (this._destroyed) console.warn('Component was already destroyed');
destroy.call(this, detach);
}
export function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -105,6 +123,7 @@ export function onDev(eventName, handler) {
}
export function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -119,17 +138,21 @@ export function callAll(fns) {
}
export var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
export var protoDev = {
destroy: destroyDev,
get: get,
fire: fire,
observe: observeDev,
on: onDev,
set: set
set: set,
teardown: destroyDev
};

@ -37,6 +37,19 @@ function setAttribute(node, attribute, value) {
node.setAttribute(attribute, value);
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -114,6 +127,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -128,11 +142,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
var template = (function () {
@ -222,16 +238,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -87,16 +87,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -13,6 +13,19 @@ function assign(target) {
return target;
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -90,6 +103,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -104,11 +118,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
function recompute ( state, newState, oldState, isInitial ) {
@ -175,16 +191,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -64,16 +64,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -33,6 +33,19 @@ function setAttribute(node, attribute, value) {
node.setAttribute(attribute, value);
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -110,6 +123,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -124,11 +138,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
function encapsulateStyles ( node ) {
@ -201,16 +217,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -70,16 +70,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -46,6 +46,19 @@ function createText(data) {
return document.createTextNode(data);
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -123,6 +136,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -137,11 +151,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
function create_main_fragment ( state, component ) {
@ -321,16 +337,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -177,16 +177,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -31,6 +31,19 @@ function createText(data) {
return document.createTextNode(data);
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -108,6 +121,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -122,11 +136,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
var template = (function () {
@ -209,16 +225,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -80,16 +80,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -37,6 +37,19 @@ function createComment() {
return document.createComment('');
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -114,6 +127,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -128,11 +142,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
function create_main_fragment ( state, component ) {
@ -256,16 +272,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -121,16 +121,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -37,6 +37,19 @@ function createComment() {
return document.createComment('');
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -114,6 +127,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -128,11 +142,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
function create_main_fragment ( state, component ) {
@ -232,16 +248,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -97,16 +97,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -25,6 +25,19 @@ function createText(data) {
return document.createTextNode(data);
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -102,6 +115,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -116,11 +130,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
var template = (function () {
@ -215,16 +231,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -94,16 +94,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -13,6 +13,19 @@ function assign(target) {
return target;
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -90,6 +103,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -104,11 +118,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
var template = (function () {
@ -142,6 +158,7 @@ function SvelteComponent ( options ) {
};
this._handlers = Object.create( null );
this._handlers.destroy = [template.ondestroy.bind(this)];
this._root = options._root || this;
this._yield = options._yield;
@ -177,17 +194,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
template.ondestroy.call( this );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -31,6 +31,7 @@ function SvelteComponent ( options ) {
};
this._handlers = Object.create( null );
this._handlers.destroy = [template.ondestroy.bind(this)]
this._root = options._root || this;
this._yield = options._yield;
@ -66,17 +67,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
template.ondestroy.call( this );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -13,6 +13,19 @@ function assign(target) {
return target;
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -90,6 +103,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -104,11 +118,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
var template = (function () {
@ -176,18 +192,6 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
template.setup( SvelteComponent );
export default SvelteComponent;

@ -65,18 +65,6 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
template.setup( SvelteComponent );
export default SvelteComponent;
export default SvelteComponent;

@ -37,6 +37,19 @@ function createComment() {
return document.createComment('');
}
function destroy(detach) {
if (!this._destroyed) {
this.fire('destroy');
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
}
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
@ -114,6 +127,7 @@ function on(eventName, handler) {
}
function set(newState) {
if (this._destroyed) return;
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
@ -128,11 +142,13 @@ function callAll(fns) {
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set
set: set,
teardown: destroy
};
function create_main_fragment ( state, component ) {
@ -416,16 +432,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -281,16 +281,4 @@ SvelteComponent.prototype._set = function _set ( newState ) {
dispatchObservers( this, this._observers.post, newState, oldState );
};
SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) {
if ( this._destroyed ) return;
this.fire( 'destroy' );
if ( detach !== false ) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;
this._state = {};
this._destroyed = true;
};
export default SvelteComponent;

@ -1,7 +1,7 @@
export default {
test ( assert, component ) {
assert.deepEqual( component.events, [ 'render' ]);
test(assert, component) {
assert.deepEqual(component.events, ['create']);
component.destroy();
assert.deepEqual( component.events, [ 'render', 'teardown' ]);
assert.deepEqual(component.events, ['create', 'destroy']);
}
};

@ -2,12 +2,12 @@
<script>
export default {
oncreate () {
this.events = [ 'render' ];
oncreate() {
this.events = ['create'];
},
ondestroy () {
this.events.push( 'teardown' );
ondestroy() {
this.events.push('destroy');
}
};
</script>
</script>

@ -0,0 +1,10 @@
export default {
data: {
x: 1
},
test(assert, component) {
component.destroy();
component.set({ x: 2 });
}
};
Loading…
Cancel
Save