From 962f04f4bf6db0da84c27620a6de7a82c4b15c42 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 29 Jul 2017 20:33:38 -0400 Subject: [PATCH 1/2] dont throw error if component is destroyed twice (closes #643) --- src/generators/dom/index.ts | 5 +++-- .../collapses-text-around-comments/expected-bundle.js | 5 +++-- test/js/samples/collapses-text-around-comments/expected.js | 5 +++-- test/js/samples/computed-collapsed-if/expected-bundle.js | 5 +++-- test/js/samples/computed-collapsed-if/expected.js | 5 +++-- test/js/samples/css-media-query/expected-bundle.js | 5 +++-- test/js/samples/css-media-query/expected.js | 5 +++-- test/js/samples/each-block-changed-check/expected-bundle.js | 5 +++-- test/js/samples/each-block-changed-check/expected.js | 5 +++-- test/js/samples/event-handlers-custom/expected-bundle.js | 5 +++-- test/js/samples/event-handlers-custom/expected.js | 5 +++-- test/js/samples/if-block-no-update/expected-bundle.js | 5 +++-- test/js/samples/if-block-no-update/expected.js | 5 +++-- test/js/samples/if-block-simple/expected-bundle.js | 5 +++-- test/js/samples/if-block-simple/expected.js | 5 +++-- test/js/samples/non-imported-component/expected-bundle.js | 5 +++-- test/js/samples/non-imported-component/expected.js | 5 +++-- .../onrender-onteardown-rewritten/expected-bundle.js | 5 +++-- test/js/samples/onrender-onteardown-rewritten/expected.js | 5 +++-- test/js/samples/use-elements-as-anchors/expected-bundle.js | 5 +++-- test/js/samples/use-elements-as-anchors/expected.js | 5 +++-- test/runtime/samples/destroy-twice/_config.js | 6 ++++++ test/runtime/samples/destroy-twice/main.html | 1 + 23 files changed, 70 insertions(+), 42 deletions(-) create mode 100644 test/runtime/samples/destroy-twice/_config.js create mode 100644 test/runtime/samples/destroy-twice/main.html diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 073471e5b0..33ac0a33c4 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -211,7 +211,7 @@ export default function dom( this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; ${generator.stylesheet.hasStyles && options.css !== false && `if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`} @@ -264,6 +264,7 @@ export default function dom( }; ${name}.prototype.teardown = ${name}.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); ${templateProperties.ondestroy && `@template.ondestroy.call( this );`} @@ -272,7 +273,7 @@ export default function dom( this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; `); diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index b290fd4f00..c9fa1ef590 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -201,7 +201,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; if ( !document.getElementById( 'svelte-3590263702-style' ) ) add_css(); this._fragment = create_main_fragment( this._state, this ); @@ -223,6 +223,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -230,7 +231,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index e02f2581d9..a5dc4d5d58 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -66,7 +66,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; if ( !document.getElementById( 'svelte-3590263702-style' ) ) add_css(); this._fragment = create_main_fragment( this._state, this ); @@ -88,6 +88,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -95,7 +96,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index 547cb5e958..35825f465d 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -155,7 +155,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -176,6 +176,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -183,7 +184,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index e16d023bfb..bb2b420c84 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -44,7 +44,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -65,6 +65,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -72,7 +73,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 831bbd2b23..ebbfffd1ec 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -181,7 +181,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; if ( !document.getElementById( 'svelte-2363328337-style' ) ) add_css(); this._fragment = create_main_fragment( this._state, this ); @@ -202,6 +202,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -209,7 +210,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index 960b737412..d9af1bfe54 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -50,7 +50,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; if ( !document.getElementById( 'svelte-2363328337-style' ) ) add_css(); this._fragment = create_main_fragment( this._state, this ); @@ -71,6 +71,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -78,7 +79,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 47d43b2aea..9afe02d871 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -301,7 +301,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -322,6 +322,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -329,7 +330,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index 4fa7680a77..6072df4896 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -157,7 +157,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -178,6 +178,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -185,7 +186,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index c916b10078..fa20cbc9ef 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -190,7 +190,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -210,6 +210,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -217,7 +218,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 882bc08e85..abbfef0f91 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -61,7 +61,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -81,6 +81,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -88,7 +89,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index 39ea6622f5..51895c4d5b 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -236,7 +236,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -257,6 +257,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -264,7 +265,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index fdedd6e4a7..2d8972ad5e 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -101,7 +101,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -122,6 +122,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -129,7 +130,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 382c2fd8d3..2ce85c33e6 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -212,7 +212,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -233,6 +233,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -240,7 +241,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index 548938d59c..efd23dc55d 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -77,7 +77,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -98,6 +98,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -105,7 +106,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 91a8b8c292..c37d2f042a 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -182,7 +182,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; if ( !options._root ) { this._oncreate = []; @@ -216,6 +216,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -223,7 +224,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index 787e62c54b..195fb2b7b1 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -61,7 +61,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; if ( !options._root ) { this._oncreate = []; @@ -95,6 +95,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -102,7 +103,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 25fae9ba8c..eb9a6cdd4f 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -146,7 +146,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; var oncreate = template.oncreate.bind( this ); @@ -178,6 +178,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); template.ondestroy.call( this ); @@ -186,7 +187,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 1a1c10f7d6..e35b716595 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -35,7 +35,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; var oncreate = template.oncreate.bind( this ); @@ -67,6 +67,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); template.ondestroy.call( this ); @@ -75,7 +76,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index 32cd0203c2..b969e80971 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -396,7 +396,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -417,6 +417,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -424,7 +425,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index 524372861e..046bf7fcbc 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -261,7 +261,7 @@ function SvelteComponent ( options ) { this._root = options._root || this; this._yield = options._yield; - this._torndown = false; + this._destroyed = false; this._fragment = create_main_fragment( this._state, this ); @@ -282,6 +282,7 @@ SvelteComponent.prototype._set = function _set ( newState ) { }; SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = function destroy ( detach ) { + if ( this._destroyed ) return; this.fire( 'destroy' ); if ( detach !== false ) this._fragment.unmount(); @@ -289,7 +290,7 @@ SvelteComponent.prototype.teardown = SvelteComponent.prototype.destroy = functio this._fragment = null; this._state = {}; - this._torndown = true; + this._destroyed = true; }; export default SvelteComponent; \ No newline at end of file diff --git a/test/runtime/samples/destroy-twice/_config.js b/test/runtime/samples/destroy-twice/_config.js new file mode 100644 index 0000000000..1adc3e3d00 --- /dev/null +++ b/test/runtime/samples/destroy-twice/_config.js @@ -0,0 +1,6 @@ +export default { + test(assert, component) { + component.destroy(); + component.destroy(); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/destroy-twice/main.html b/test/runtime/samples/destroy-twice/main.html new file mode 100644 index 0000000000..78e7012b82 --- /dev/null +++ b/test/runtime/samples/destroy-twice/main.html @@ -0,0 +1 @@ +
\ No newline at end of file From e2d63d1b34e92bcdb05c1f03de149989602003b5 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 30 Jul 2017 00:09:35 -0400 Subject: [PATCH 2/2] add dev mode warning for double destroy --- src/generators/dom/index.ts | 2 +- .../dev-warning-destroy-twice/_config.js | 21 +++++++++++++++++++ .../dev-warning-destroy-twice/main.html | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/dev-warning-destroy-twice/_config.js create mode 100644 test/runtime/samples/dev-warning-destroy-twice/main.html diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 33ac0a33c4..8c3f287b8d 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -264,7 +264,7 @@ export default function dom( }; ${name}.prototype.teardown = ${name}.prototype.destroy = function destroy ( detach ) { - if ( this._destroyed ) return; + if ( this._destroyed ) return${options.dev && ` console.warn( 'Component was already destroyed' )`}; this.fire( 'destroy' ); ${templateProperties.ondestroy && `@template.ondestroy.call( this );`} diff --git a/test/runtime/samples/dev-warning-destroy-twice/_config.js b/test/runtime/samples/dev-warning-destroy-twice/_config.js new file mode 100644 index 0000000000..94c07ed6a7 --- /dev/null +++ b/test/runtime/samples/dev-warning-destroy-twice/_config.js @@ -0,0 +1,21 @@ +export default { + dev: true, + + test(assert, component) { + const warn = console.warn; // eslint-disable-line no-console + + const warnings = []; + console.warn = warning => { // eslint-disable-line no-console + warnings.push(warning); + }; + + component.destroy(); + component.destroy(); + + assert.deepEqual(warnings, [ + `Component was already destroyed` + ]); + + console.warn = warn; // eslint-disable-line no-console + } +}; \ No newline at end of file diff --git a/test/runtime/samples/dev-warning-destroy-twice/main.html b/test/runtime/samples/dev-warning-destroy-twice/main.html new file mode 100644 index 0000000000..78e7012b82 --- /dev/null +++ b/test/runtime/samples/dev-warning-destroy-twice/main.html @@ -0,0 +1 @@ +
\ No newline at end of file