do unmount work in destroy method

pull/1421/head
Rich Harris 7 years ago
parent 21bc182fe7
commit 82c9217e53

@ -111,10 +111,10 @@ export default class Block {
if (parentNode) { if (parentNode) {
this.builders.mount.addLine(`@appendNode(${name}, ${parentNode});`); this.builders.mount.addLine(`@appendNode(${name}, ${parentNode});`);
if (parentNode === 'document.head') this.builders.unmount.addLine(`@detachNode(${name});`); if (parentNode === 'document.head') this.builders.destroy.addLine(`@detachNode(${name});`);
} else { } else {
this.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`); this.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`);
this.builders.unmount.addLine(`@detachNode(${name});`); this.builders.destroy.addConditional('detach', `@detachNode(${name});`);
} }
} }
@ -162,7 +162,7 @@ export default class Block {
} }
// minor hack we need to ensure that any {{{triples}}} are detached first // minor hack we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString()); this.builders.destroy.addBlockAtStart(this.builders.detachRaw.toString());
const properties = new CodeBuilder(); const properties = new CodeBuilder();
@ -280,21 +280,11 @@ export default class Block {
} }
} }
if (this.builders.unmount.isEmpty()) {
properties.addBlock(`u: @noop,`);
} else {
properties.addBlock(deindent`
${dev ? 'u: function unmount' : 'u'}() {
${this.builders.unmount}
},
`);
}
if (this.builders.destroy.isEmpty()) { if (this.builders.destroy.isEmpty()) {
properties.addBlock(`d: @noop`); properties.addBlock(`d: @noop`);
} else { } else {
properties.addBlock(deindent` properties.addBlock(deindent`
${dev ? 'd: function destroy' : 'd'}() { ${dev ? 'd: function destroy' : 'd'}(detach) {
${this.builders.destroy} ${this.builders.destroy}
} }
`); `);

@ -124,7 +124,7 @@ export default function dom(
? `@proto` ? `@proto`
: deindent` : deindent`
{ {
${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_unmount', '_differs'] ${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_differs']
.map(n => `${n}: @${n}`) .map(n => `${n}: @${n}`)
.join(',\n')} .join(',\n')}
}`; }`;
@ -304,10 +304,6 @@ export default function dom(
@assign(${name}.prototype, { @assign(${name}.prototype, {
_mount(target, anchor) { _mount(target, anchor) {
target.insertBefore(this, anchor); target.insertBefore(this, anchor);
},
_unmount() {
this.parentNode.removeChild(this);
} }
}); });

@ -167,12 +167,8 @@ export default class AwaitBlock extends Node {
`); `);
} }
block.builders.unmount.addBlock(deindent`
${info}.block.u();
`);
block.builders.destroy.addBlock(deindent` block.builders.destroy.addBlock(deindent`
${info}.block.d(); ${info}.block.d(${parentNode ? '' : 'detach'});
${info} = null; ${info} = null;
`); `);

@ -430,9 +430,7 @@ export default class Component extends Node {
`); `);
} }
if (!parentNode) block.builders.unmount.addLine(`if (${name}) ${name}._unmount();`); block.builders.destroy.addLine(`if (${name}) ${name}.destroy(${parentNode ? '' : 'detach'});`);
block.builders.destroy.addLine(`if (${name}) ${name}.destroy(false);`);
} else { } else {
const expression = this.name === 'svelte:self' const expression = this.name === 'svelte:self'
? compiler.name ? compiler.name
@ -477,10 +475,8 @@ export default class Component extends Node {
`); `);
} }
if (!parentNode) block.builders.unmount.addLine(`${name}._unmount();`);
block.builders.destroy.addLine(deindent` block.builders.destroy.addLine(deindent`
${name}.destroy(false); ${name}.destroy(${parentNode ? '' : 'detach'});
${this.ref && `if (#component.refs.${this.ref} === ${name}) #component.refs.${this.ref} = null;`} ${this.ref && `if (#component.refs.${this.ref} === ${name}) #component.refs.${this.ref} = null;`}
`); `);
} }

@ -213,8 +213,7 @@ export default class EachBlock extends Node {
${each_block_else}.c(); ${each_block_else}.c();
${each_block_else}.${mountOrIntro}(${initialMountNode}, ${anchor}); ${each_block_else}.${mountOrIntro}(${initialMountNode}, ${anchor});
} else if (${each_block_else}) { } else if (${each_block_else}) {
${each_block_else}.u(); ${each_block_else}.d(1);
${each_block_else}.d();
${each_block_else} = null; ${each_block_else} = null;
} }
`); `);
@ -222,8 +221,7 @@ export default class EachBlock extends Node {
block.builders.update.addBlock(deindent` block.builders.update.addBlock(deindent`
if (${this.each_block_value}.${length}) { if (${this.each_block_value}.${length}) {
if (${each_block_else}) { if (${each_block_else}) {
${each_block_else}.u(); ${each_block_else}.d(1);
${each_block_else}.d();
${each_block_else} = null; ${each_block_else} = null;
} }
} else if (!${each_block_else}) { } else if (!${each_block_else}) {
@ -234,12 +232,8 @@ export default class EachBlock extends Node {
`); `);
} }
block.builders.unmount.addLine(
`if (${each_block_else}) ${each_block_else}.u()`
);
block.builders.destroy.addBlock(deindent` block.builders.destroy.addBlock(deindent`
if (${each_block_else}) ${each_block_else}.d(); if (${each_block_else}) ${each_block_else}.d(${parentNode ? '' : 'detach'});
`); `);
} }
@ -322,14 +316,8 @@ export default class EachBlock extends Node {
${blocks} = @updateKeyedEach(${blocks}, #component, changed, ${get_key}, ${dynamic ? '1' : '0'}, ctx, ${this.each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", ${anchor}, ${this.get_each_context}); ${blocks} = @updateKeyedEach(${blocks}, #component, changed, ${get_key}, ${dynamic ? '1' : '0'}, ctx, ${this.each_block_value}, ${lookup}, ${updateMountNode}, ${String(this.block.hasOutroMethod)}, ${create_each_block}, "${mountOrIntro}", ${anchor}, ${this.get_each_context});
`); `);
if (!parentNode) {
block.builders.unmount.addBlock(deindent`
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].u();
`);
}
block.builders.destroy.addBlock(deindent` block.builders.destroy.addBlock(deindent`
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].d(); for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].d(${parentNode ? '' : 'detach'});
`); `);
} }
@ -424,8 +412,7 @@ export default class EachBlock extends Node {
function ${outro}(i) { function ${outro}(i) {
if (${iterations}[i]) { if (${iterations}[i]) {
${iterations}[i].o(function() { ${iterations}[i].o(function() {
${iterations}[i].u(); ${iterations}[i].d(1);
${iterations}[i].d();
${iterations}[i] = null; ${iterations}[i] = null;
}); });
} }
@ -435,8 +422,7 @@ export default class EachBlock extends Node {
` `
: deindent` : deindent`
for (; #i < ${iterations}.length; #i += 1) { for (; #i < ${iterations}.length; #i += 1) {
${iterations}[#i].u(); ${iterations}[#i].d(1);
${iterations}[#i].d();
} }
${iterations}.length = ${this.each_block_value}.${length}; ${iterations}.length = ${this.each_block_value}.${length};
`; `;
@ -456,13 +442,7 @@ export default class EachBlock extends Node {
`); `);
} }
block.builders.unmount.addBlock(deindent` block.builders.destroy.addBlock(`@destroyEach(${iterations}, detach);`);
for (var #i = 0; #i < ${iterations}.length; #i += 1) {
${iterations}[#i].u();
}
`);
block.builders.destroy.addBlock(`@destroyEach(${iterations});`);
} }
remount(name: string) { remount(name: string) {

@ -291,14 +291,14 @@ export default class Element extends Node {
); );
if (initialMountNode === 'document.head') { if (initialMountNode === 'document.head') {
block.builders.unmount.addLine(`@detachNode(${name});`); block.builders.destroy.addLine(`@detachNode(${name});`);
} }
} else { } else {
block.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`); block.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`);
// TODO we eventually need to consider what happens to elements // TODO we eventually need to consider what happens to elements
// that belong to the same outgroup as an outroing element... // that belong to the same outgroup as an outroing element...
block.builders.unmount.addLine(`@detachNode(${name});`); block.builders.destroy.addConditional('detach', `@detachNode(${name});`);
} }
// TODO move this into a class as well? // TODO move this into a class as well?
@ -503,7 +503,7 @@ export default class Element extends Node {
`${resize_listener} = @addResizeListener(${this.var}, ${handler});` `${resize_listener} = @addResizeListener(${this.var}, ${handler});`
); );
block.builders.unmount.addLine( block.builders.destroy.addLine(
`${resize_listener}.cancel();` `${resize_listener}.cancel();`
); );
} else { } else {

@ -192,13 +192,11 @@ export default class IfBlock extends Node {
const changeBlock = deindent` const changeBlock = deindent`
${hasElse ${hasElse
? deindent` ? deindent`
${name}.u(); ${name}.d(1);
${name}.d();
` `
: deindent` : deindent`
if (${name}) { if (${name}) {
${name}.u(); ${name}.d(1);
${name}.d();
}`} }`}
${name} = ${current_block_type_and}${current_block_type}(#component, ctx); ${name} = ${current_block_type_and}${current_block_type}(#component, ctx);
${if_name}${name}.c(); ${if_name}${name}.c();
@ -221,9 +219,7 @@ export default class IfBlock extends Node {
`); `);
} }
block.builders.unmount.addLine(`${if_name}${name}.u();`); block.builders.destroy.addLine(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`);
block.builders.destroy.addLine(`${if_name}${name}.d();`);
} }
// if any of the siblings have outros, we need to keep references to the blocks // if any of the siblings have outros, we need to keep references to the blocks
@ -288,8 +284,7 @@ export default class IfBlock extends Node {
const destroyOldBlock = deindent` const destroyOldBlock = deindent`
${name}.o(function() { ${name}.o(function() {
${if_blocks}[ ${previous_block_index} ].u(); ${if_blocks}[ ${previous_block_index} ].d(1);
${if_blocks}[ ${previous_block_index} ].d();
${if_blocks}[ ${previous_block_index} ] = null; ${if_blocks}[ ${previous_block_index} ] = null;
}); });
`; `;
@ -343,8 +338,7 @@ export default class IfBlock extends Node {
block.builders.destroy.addLine(deindent` block.builders.destroy.addLine(deindent`
${if_current_block_type_index}{ ${if_current_block_type_index}{
${if_blocks}[${current_block_type_index}].u(); ${if_blocks}[${current_block_type_index}].d(${parentNode ? '' : 'detach'});
${if_blocks}[${current_block_type_index}].d();
} }
`); `);
} }
@ -413,14 +407,12 @@ export default class IfBlock extends Node {
const exit = branch.hasOutroMethod const exit = branch.hasOutroMethod
? deindent` ? deindent`
${name}.o(function() { ${name}.o(function() {
${name}.u(); ${name}.d(1);
${name}.d();
${name} = null; ${name} = null;
}); });
` `
: deindent` : deindent`
${name}.u(); ${name}.d(1);
${name}.d();
${name} = null; ${name} = null;
`; `;
@ -432,9 +424,7 @@ export default class IfBlock extends Node {
} }
`); `);
block.builders.unmount.addLine(`${if_name}${name}.u();`); block.builders.destroy.addLine(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`);
block.builders.destroy.addLine(`${if_name}${name}.d();`);
} }
getBranches( getBranches(

@ -76,7 +76,7 @@ export default class RawMustacheTag extends Tag {
} }
block.builders.mount.addLine(insert(init)); block.builders.mount.addLine(insert(init));
block.builders.detachRaw.addBlock(detach); block.builders.detachRaw.addConditional('detach', detach);
if (needsAnchorAfter && anchorBefore !== 'null') { if (needsAnchorAfter && anchorBefore !== 'null') {
// ...otherwise it should go afterwards // ...otherwise it should go afterwards

@ -15,14 +15,12 @@ export function handlePromise(promise, info) {
if (info.blocks) { if (info.blocks) {
info.blocks.forEach((block, i) => { info.blocks.forEach((block, i) => {
if (i !== index && block) block.o(() => { if (i !== index && block) block.o(() => {
block.u(); block.d(1);
block.d();
info.blocks[i] = null; info.blocks[i] = null;
}); });
}); });
} else { } else {
info.block.u(); info.block.d(1);
info.block.d();
} }
block.c(); block.c();

@ -47,9 +47,9 @@ export function reinsertBefore(after, target) {
while (parent.firstChild !== after) target.appendChild(parent.firstChild); while (parent.firstChild !== after) target.appendChild(parent.firstChild);
} }
export function destroyEach(iterations) { export function destroyEach(iterations, detach) {
for (var i = 0; i < iterations.length; i += 1) { for (var i = 0; i < iterations.length; i += 1) {
if (iterations[i]) iterations[i].d(); if (iterations[i]) iterations[i].d(detach);
} }
} }

@ -17,8 +17,7 @@ export function destroy(detach) {
this.fire('destroy'); this.fire('destroy');
this.set = noop; this.set = noop;
if (detach !== false) this._fragment.u(); this._fragment.d(detach !== false);
this._fragment.d();
this._fragment = null; this._fragment = null;
this._state = {}; this._state = {};
} }
@ -133,10 +132,6 @@ export function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null); this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
} }
export function _unmount() {
if (this._fragment) this._fragment.u();
}
export var PENDING = {}; export var PENDING = {};
export var SUCCESS = {}; export var SUCCESS = {};
export var FAILURE = {}; export var FAILURE = {};
@ -154,7 +149,6 @@ export var proto = {
_recompute: noop, _recompute: noop,
_set, _set,
_mount, _mount,
_unmount,
_differs _differs
}; };
@ -167,6 +161,5 @@ export var protoDev = {
_recompute: noop, _recompute: noop,
_set, _set,
_mount, _mount,
_unmount,
_differs _differs
}; };

@ -1,6 +1,5 @@
export function destroyBlock(block, lookup) { export function destroyBlock(block, lookup) {
block.u(); block.d(1);
block.d();
lookup[block.key] = null; lookup[block.key] = null;
} }

Loading…
Cancel
Save