Skip to content

Commit

Permalink
Merge pull request #1677 from sveltejs/gh-1660
Browse files Browse the repository at this point in the history
Allow non-existent dynamic components to be destroyed
  • Loading branch information
Rich-Harris authored Aug 23, 2018
2 parents 3778431 + 3785a6d commit ba7a6c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/compile/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export default class Component extends Node {

if (this.compiler.options.nestedTransitions) {
block.builders.outro.addLine(
`${name}._fragment.o(#outrocallback);`
`if (${name}) ${name}._fragment.o(#outrocallback);`
);
}
}
Expand Down
23 changes: 17 additions & 6 deletions test/cli/samples/amd/expected/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ define("test", function() { "use strict";

function init(component, options) {
component._handlers = blankObject();
component._slots = blankObject();
component._bind = options._bind;

component.options = options;
component.root = options.root || component;
component.store = options.store || component.root.store;

if (!options.root) {
component._beforecreate = [];
component._oncreate = [];
component._aftercreate = [];
}
}

function assign(tar, src) {
Expand Down Expand Up @@ -125,11 +132,7 @@ define("test", function() { "use strict";
function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
this.root._lock = true;
callAll(this.root._beforecreate);
callAll(this.root._oncreate);
callAll(this.root._aftercreate);
this.root._lock = false;
flush(this.root);
}

function _set(newState) {
Expand Down Expand Up @@ -165,8 +168,16 @@ define("test", function() { "use strict";
return Object.create(null);
}

function flush(component) {
component._lock = true;
callAll(component._beforecreate);
callAll(component._oncreate);
callAll(component._aftercreate);
component._lock = false;
}

function callAll(fns) {
while (fns && fns.length) fns.shift()();
}
return Main;
});
});
13 changes: 13 additions & 0 deletions test/runtime/samples/dynamic-component-destroy-null/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
data: {
x: true
},

nestedTransitions: true,

test(assert, component) {
component.set({
x: false
});
}
};
3 changes: 3 additions & 0 deletions test/runtime/samples/dynamic-component-destroy-null/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#if x}
<svelte:component this={null}/>
{/if}

0 comments on commit ba7a6c9

Please sign in to comment.