Skip to content

Commit

Permalink
Merge pull request #755 from sveltejs/gh-744
Browse files Browse the repository at this point in the history
handle set after destroy, and move destroy into shared helpers
  • Loading branch information
Rich-Harris authored Aug 9, 2017
2 parents 8dd23b8 + fd3792b commit 8b9380f
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 344 deletions.
19 changes: 3 additions & 16 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}
}`;

Expand Down Expand Up @@ -207,11 +207,11 @@ export default function dom(
};
this._handlers = Object.create( null );
${templateProperties.ondestroy && `this._handlers.destroy = [@template.ondestroy]`}
this._root = options._root || this;
this._yield = options._yield;
this._destroyed = false;
${generator.stylesheet.hasStyles &&
options.css !== false &&
`if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`}
Expand Down Expand Up @@ -263,19 +263,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} );`}
`);

Expand Down
27 changes: 25 additions & 2 deletions src/shared/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { assign } from './utils.js';
import { noop } from './utils.js';
export * from './dom.js';
export * from './transitions.js';
export * from './utils.js';

export function destroy(detach) {
this.destroy = this.set = noop;
this.fire('destroy');

if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;

this._state = {};
}

export function destroyDev(detach) {
destroy.call(this, detach);
this.destroy = function() {
console.warn('Component was already destroyed');
};
}

export function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
Expand Down Expand Up @@ -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
};
28 changes: 14 additions & 14 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,17 @@ function setAttribute(node, attribute, value) {
node.setAttribute(attribute, value);
}

function destroy(detach) {
this.destroy = this.set = noop;
this.fire('destroy');

if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;

this._state = {};
}

function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
Expand Down Expand Up @@ -128,11 +139,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 () {
Expand Down Expand Up @@ -201,7 +214,6 @@ function SvelteComponent ( options ) {
this._root = options._root || this;
this._yield = options._yield;

this._destroyed = false;
if ( !document.getElementById( 'svelte-3590263702-style' ) ) add_css();

this._fragment = create_main_fragment( this._state, this );
Expand All @@ -222,16 +234,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 changes: 0 additions & 13 deletions test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function SvelteComponent ( options ) {
this._root = options._root || this;
this._yield = options._yield;

this._destroyed = false;
if ( !document.getElementById( 'svelte-3590263702-style' ) ) add_css();

this._fragment = create_main_fragment( this._state, this );
Expand All @@ -87,16 +86,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;
29 changes: 14 additions & 15 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,17 @@ function assign(target) {
return target;
}

function destroy(detach) {
this.destroy = this.set = noop;
this.fire('destroy');

if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;

this._state = {};
}

function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
Expand Down Expand Up @@ -104,11 +115,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 ) {
Expand Down Expand Up @@ -155,8 +168,6 @@ function SvelteComponent ( options ) {
this._root = options._root || this;
this._yield = options._yield;

this._destroyed = false;

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

if ( options.target ) {
Expand All @@ -175,16 +186,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;
14 changes: 0 additions & 14 deletions test/js/samples/computed-collapsed-if/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ function SvelteComponent ( options ) {
this._root = options._root || this;
this._yield = options._yield;

this._destroyed = false;

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

if ( options.target ) {
Expand All @@ -64,16 +62,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;
28 changes: 14 additions & 14 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,17 @@ function setAttribute(node, attribute, value) {
node.setAttribute(attribute, value);
}

function destroy(detach) {
this.destroy = this.set = noop;
this.fire('destroy');

if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = null;

this._state = {};
}

function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
Expand Down Expand Up @@ -124,11 +135,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 ) {
Expand Down Expand Up @@ -181,7 +194,6 @@ function SvelteComponent ( options ) {
this._root = options._root || this;
this._yield = options._yield;

this._destroyed = false;
if ( !document.getElementById( 'svelte-2363328337-style' ) ) add_css();

this._fragment = create_main_fragment( this._state, this );
Expand All @@ -201,16 +213,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 changes: 0 additions & 13 deletions test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function SvelteComponent ( options ) {
this._root = options._root || this;
this._yield = options._yield;

this._destroyed = false;
if ( !document.getElementById( 'svelte-2363328337-style' ) ) add_css();

this._fragment = create_main_fragment( this._state, this );
Expand All @@ -70,16 +69,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;
Loading

0 comments on commit 8b9380f

Please sign in to comment.