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) {
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 {
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
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());
this.builders.destroy.addBlockAtStart(this.builders.detachRaw.toString());
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()) {
properties.addBlock(`d: @noop`);
} else {
properties.addBlock(deindent`
${dev ? 'd: function destroy' : 'd'}() {
${dev ? 'd: function destroy' : 'd'}(detach) {
${this.builders.destroy}
}
`);

@ -124,7 +124,7 @@ export default function dom(
? `@proto`
: deindent`
{
${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_unmount', '_differs']
${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_differs']
.map(n => `${n}: @${n}`)
.join(',\n')}
}`;
@ -304,10 +304,6 @@ export default function dom(
@assign(${name}.prototype, {
_mount(target, 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`
${info}.block.d();
${info}.block.d(${parentNode ? '' : 'detach'});
${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(false);`);
block.builders.destroy.addLine(`if (${name}) ${name}.destroy(${parentNode ? '' : 'detach'});`);
} else {
const expression = this.name === 'svelte:self'
? 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`
${name}.destroy(false);
${name}.destroy(${parentNode ? '' : 'detach'});
${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}.${mountOrIntro}(${initialMountNode}, ${anchor});
} else if (${each_block_else}) {
${each_block_else}.u();
${each_block_else}.d();
${each_block_else}.d(1);
${each_block_else} = null;
}
`);
@ -222,8 +221,7 @@ export default class EachBlock extends Node {
block.builders.update.addBlock(deindent`
if (${this.each_block_value}.${length}) {
if (${each_block_else}) {
${each_block_else}.u();
${each_block_else}.d();
${each_block_else}.d(1);
${each_block_else} = null;
}
} 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`
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});
`);
if (!parentNode) {
block.builders.unmount.addBlock(deindent`
for (#i = 0; #i < ${blocks}.length; #i += 1) ${blocks}[#i].u();
`);
}
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) {
if (${iterations}[i]) {
${iterations}[i].o(function() {
${iterations}[i].u();
${iterations}[i].d();
${iterations}[i].d(1);
${iterations}[i] = null;
});
}
@ -435,8 +422,7 @@ export default class EachBlock extends Node {
`
: deindent`
for (; #i < ${iterations}.length; #i += 1) {
${iterations}[#i].u();
${iterations}[#i].d();
${iterations}[#i].d(1);
}
${iterations}.length = ${this.each_block_value}.${length};
`;
@ -456,13 +442,7 @@ export default class EachBlock extends Node {
`);
}
block.builders.unmount.addBlock(deindent`
for (var #i = 0; #i < ${iterations}.length; #i += 1) {
${iterations}[#i].u();
}
`);
block.builders.destroy.addBlock(`@destroyEach(${iterations});`);
block.builders.destroy.addBlock(`@destroyEach(${iterations}, detach);`);
}
remount(name: string) {

@ -291,14 +291,14 @@ export default class Element extends Node {
);
if (initialMountNode === 'document.head') {
block.builders.unmount.addLine(`@detachNode(${name});`);
block.builders.destroy.addLine(`@detachNode(${name});`);
}
} else {
block.builders.mount.addLine(`@insertNode(${name}, #target, anchor);`);
// TODO we eventually need to consider what happens to elements
// 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?
@ -503,7 +503,7 @@ export default class Element extends Node {
`${resize_listener} = @addResizeListener(${this.var}, ${handler});`
);
block.builders.unmount.addLine(
block.builders.destroy.addLine(
`${resize_listener}.cancel();`
);
} else {

@ -192,13 +192,11 @@ export default class IfBlock extends Node {
const changeBlock = deindent`
${hasElse
? deindent`
${name}.u();
${name}.d();
${name}.d(1);
`
: deindent`
if (${name}) {
${name}.u();
${name}.d();
${name}.d(1);
}`}
${name} = ${current_block_type_and}${current_block_type}(#component, ctx);
${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();`);
block.builders.destroy.addLine(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`);
}
// 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`
${name}.o(function() {
${if_blocks}[ ${previous_block_index} ].u();
${if_blocks}[ ${previous_block_index} ].d();
${if_blocks}[ ${previous_block_index} ].d(1);
${if_blocks}[ ${previous_block_index} ] = null;
});
`;
@ -343,8 +338,7 @@ export default class IfBlock extends Node {
block.builders.destroy.addLine(deindent`
${if_current_block_type_index}{
${if_blocks}[${current_block_type_index}].u();
${if_blocks}[${current_block_type_index}].d();
${if_blocks}[${current_block_type_index}].d(${parentNode ? '' : 'detach'});
}
`);
}
@ -413,14 +407,12 @@ export default class IfBlock extends Node {
const exit = branch.hasOutroMethod
? deindent`
${name}.o(function() {
${name}.u();
${name}.d();
${name}.d(1);
${name} = null;
});
`
: deindent`
${name}.u();
${name}.d();
${name}.d(1);
${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();`);
block.builders.destroy.addLine(`${if_name}${name}.d(${parentNode ? '' : 'detach'});`);
}
getBranches(

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

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

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

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

Loading…
Cancel
Save