Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Unmount nodes in destroy methods #1421

Merged
merged 3 commits into from
May 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions src/compile/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export default class Block {
intro: CodeBuilder;
update: CodeBuilder;
outro: CodeBuilder;
unmount: CodeBuilder;
detachRaw: CodeBuilder;
destroy: CodeBuilder;
};

Expand Down Expand Up @@ -73,8 +71,6 @@ export default class Block {
intro: new CodeBuilder(),
update: new CodeBuilder(),
outro: new CodeBuilder(),
unmount: new CodeBuilder(),
detachRaw: new CodeBuilder(),
destroy: new CodeBuilder(),
};

Expand Down Expand Up @@ -103,18 +99,19 @@ export default class Block {
name: string,
renderStatement: string,
claimStatement: string,
parentNode: string
parentNode: string,
noDetach?: boolean
) {
this.addVariable(name);
this.builders.create.addLine(`${name} = ${renderStatement};`);
this.builders.claim.addLine(`${name} = ${claimStatement || renderStatement};`);

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});`);
if (!noDetach) this.builders.destroy.addConditional('detach', `@detachNode(${name});`);
}
}

Expand Down Expand Up @@ -161,9 +158,6 @@ export default class Block {
this.builders.mount.addLine(`${this.autofocus}.focus();`);
}

// minor hack – we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());

const properties = new CodeBuilder();

let localKey;
Expand Down Expand Up @@ -280,21 +274,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}
}
`);
Expand Down
6 changes: 1 addition & 5 deletions src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')}
}`;
Expand Down Expand Up @@ -304,10 +304,6 @@ export default function dom(
@assign(${name}.prototype, {
_mount(target, anchor) {
target.insertBefore(this, anchor);
},

_unmount() {
this.parentNode.removeChild(this);
}
});

Expand Down
6 changes: 1 addition & 5 deletions src/compile/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
`);

Expand Down
8 changes: 2 additions & 6 deletions src/compile/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;`}
`);
}
Expand Down
34 changes: 7 additions & 27 deletions src/compile/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,15 @@ 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;
}
`);
} else {
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}) {
Expand All @@ -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'});
`);
}

Expand Down Expand Up @@ -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'});
`);
}

Expand Down Expand Up @@ -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;
});
}
Expand All @@ -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};
`;
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 8 additions & 18 deletions src/compile/nodes/IfBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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;
});
`;
Expand Down Expand Up @@ -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'});
}
`);
}
Expand Down Expand Up @@ -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;
`;

Expand All @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions src/compile/nodes/RawMustacheTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export default class RawMustacheTag extends Tag {
anchorBefore,
`@createElement('noscript')`,
parentNodes && `@createElement('noscript')`,
parentNode
parentNode,
true
);
}

Expand All @@ -76,7 +77,12 @@ export default class RawMustacheTag extends Tag {
}

block.builders.mount.addLine(insert(init));
block.builders.detachRaw.addBlock(detach);

if (!parentNode) {
block.builders.destroy.addConditional('detach', needsAnchorBefore
? `${detach}\n@detachNode(${anchorBefore});`
: detach);
}

if (needsAnchorAfter && anchorBefore !== 'null') {
// ...otherwise it should go afterwards
Expand Down
14 changes: 6 additions & 8 deletions src/compile/nodes/Slot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ export default class Slot extends Element {
if (needsAnchorAfter) block.addVariable(anchorAfter);

let mountBefore = block.builders.mount.toString();
let unmountBefore = block.builders.unmount.toString();
let destroyBefore = block.builders.destroy.toString();

block.builders.create.pushCondition(`!${content_name}`);
block.builders.hydrate.pushCondition(`!${content_name}`);
block.builders.mount.pushCondition(`!${content_name}`);
block.builders.update.pushCondition(`!${content_name}`);
block.builders.unmount.pushCondition(`!${content_name}`);
block.builders.destroy.pushCondition(`!${content_name}`);

this.children.forEach((child: Node) => {
Expand All @@ -72,7 +71,6 @@ export default class Slot extends Element {
block.builders.hydrate.popCondition();
block.builders.mount.popCondition();
block.builders.update.popCondition();
block.builders.unmount.popCondition();
block.builders.destroy.popCondition();

const mountLeadin = block.builders.mount.toString() !== mountBefore
Expand Down Expand Up @@ -101,30 +99,30 @@ export default class Slot extends Element {
// so that it can be reinserted later
// TODO so that this can work with public API, component._slotted should
// be all fragments, derived from options.slots. Not === options.slots
const unmountLeadin = block.builders.unmount.toString() !== unmountBefore
const unmountLeadin = block.builders.destroy.toString() !== destroyBefore
? `else`
: `if (${content_name})`;

if (anchorBefore === 'null' && anchorAfter === 'null') {
block.builders.unmount.addBlock(deindent`
block.builders.destroy.addBlock(deindent`
${unmountLeadin} {
@reinsertChildren(${parentNode}, ${content_name});
}
`);
} else if (anchorBefore === 'null') {
block.builders.unmount.addBlock(deindent`
block.builders.destroy.addBlock(deindent`
${unmountLeadin} {
@reinsertBefore(${anchorAfter}, ${content_name});
}
`);
} else if (anchorAfter === 'null') {
block.builders.unmount.addBlock(deindent`
block.builders.destroy.addBlock(deindent`
${unmountLeadin} {
@reinsertAfter(${anchorBefore}, ${content_name});
}
`);
} else {
block.builders.unmount.addBlock(deindent`
block.builders.destroy.addBlock(deindent`
${unmountLeadin} {
@reinsertBetween(${anchorBefore}, ${anchorAfter}, ${content_name});
@detachNode(${anchorBefore});
Expand Down
Loading