WIP - start seeing if we can unify init and update

pull/3986/head
Richard Harris 6 years ago
parent 8125731deb
commit 3b5155e46c

@ -287,11 +287,18 @@ export default class Block {
}`; }`;
} }
if (this.chunks.mount.length === 0) { const needs_update_method = this.maintain_context || (this.chunks.update.length > 0 && this.has_update_method);
if (this.chunks.mount.length === 0 && this.chunks.update.length === 0) {
properties.mount = noop; properties.mount = noop;
} else { } else {
const dirty = this.renderer.context_overflow
? x`TODO` // this.renderer.dirty_expression
: this.parent ? x`-1` : x`[-1]`;
properties.mount = x`function #mount(#target, anchor) { properties.mount = x`function #mount(#target, anchor) {
${this.chunks.mount} ${this.chunks.mount}
${needs_update_method && b`this.p(#ctx, ${dirty});`}
}`; }`;
} }

@ -203,7 +203,7 @@ export default class EachBlockWrapper extends Wrapper {
const snippet = this.node.expression.manipulate(block); const snippet = this.node.expression.manipulate(block);
block.chunks.init.push(b`let ${this.vars.each_block_value} = ${snippet};`); block.chunks.init.push(b`let ${this.vars.each_block_value} = [];`);
// TODO which is better — Object.create(array) or array.slice()? // TODO which is better — Object.create(array) or array.slice()?
renderer.blocks.push(b` renderer.blocks.push(b`
@ -261,12 +261,12 @@ export default class EachBlockWrapper extends Wrapper {
block.chunks.init.push(b`let ${each_block_else} = null;`); block.chunks.init.push(b`let ${each_block_else} = null;`);
// TODO neaten this up... will end up with an empty line in the block // TODO neaten this up... will end up with an empty line in the block
block.chunks.init.push(b` // block.chunks.init.push(b`
if (!${this.vars.data_length}) { // if (!${this.vars.data_length}) {
${each_block_else} = ${this.else.block.name}(#ctx); // ${each_block_else} = ${this.else.block.name}(#ctx);
${each_block_else}.c(); // ${each_block_else}.c();
} // }
`); // `);
block.chunks.mount.push(b` block.chunks.mount.push(b`
if (${each_block_else}) { if (${each_block_else}) {
@ -336,7 +336,6 @@ export default class EachBlockWrapper extends Wrapper {
const { const {
create_each_block, create_each_block,
iterations, iterations,
data_length,
view_length view_length
} = this.vars; } = this.vars;
@ -360,12 +359,6 @@ export default class EachBlockWrapper extends Wrapper {
block.chunks.init.push(b` block.chunks.init.push(b`
const ${get_key} = #ctx => ${this.node.key.manipulate(block)}; const ${get_key} = #ctx => ${this.node.key.manipulate(block)};
for (let #i = 0; #i < ${data_length}; #i += 1) {
let child_ctx = ${this.vars.get_each_context}(#ctx, ${this.vars.each_block_value}, #i);
let key = ${get_key}(child_ctx);
${lookup}.set(key, ${iterations}[#i] = ${create_each_block}(key, child_ctx));
}
`); `);
block.chunks.create.push(b` block.chunks.create.push(b`
@ -450,10 +443,6 @@ export default class EachBlockWrapper extends Wrapper {
block.chunks.init.push(b` block.chunks.init.push(b`
let ${iterations} = []; let ${iterations} = [];
for (let #i = 0; #i < ${data_length}; #i += 1) {
${iterations}[#i] = ${create_each_block}(${this.vars.get_each_context}(#ctx, ${this.vars.each_block_value}, #i));
}
`); `);
block.chunks.create.push(b` block.chunks.create.push(b`

@ -500,7 +500,7 @@ export default class IfBlockWrapper extends Wrapper {
render_simple( render_simple(
block: Block, block: Block,
parent_node: Identifier, _parent_node: Identifier,
_parent_nodes: Identifier, _parent_nodes: Identifier,
dynamic, dynamic,
{ name, anchor, if_exists_condition, has_transitions }, { name, anchor, if_exists_condition, has_transitions },
@ -509,17 +509,7 @@ export default class IfBlockWrapper extends Wrapper {
const branch = this.branches[0]; const branch = this.branches[0];
if (branch.snippet) block.add_variable(branch.condition, branch.snippet); if (branch.snippet) block.add_variable(branch.condition, branch.snippet);
block.add_variable(name);
block.chunks.init.push(b`
let ${name} = ${branch.condition} && ${branch.block.name}(#ctx);
`);
const initial_mount_node = parent_node || '#target';
const anchor_node = parent_node ? 'null' : 'anchor';
block.chunks.mount.push(
b`if (${name}) ${name}.m(${initial_mount_node}, ${anchor_node});`
);
if (branch.dependencies.length > 0) { if (branch.dependencies.length > 0) {
const update_mount_node = this.get_update_mount_node(anchor); const update_mount_node = this.get_update_mount_node(anchor);

@ -139,13 +139,19 @@ export default class WindowWrapper extends Wrapper {
// special case... might need to abstract this out if we add more special cases // special case... might need to abstract this out if we add more special cases
if (bindings.scrollX || bindings.scrollY) { if (bindings.scrollX || bindings.scrollY) {
const condition = renderer.dirty([bindings.scrollX, bindings.scrollY].filter(Boolean));
const scrollX = bindings.scrollX ? renderer.reference(bindings.scrollX) : x`@_window.pageXOffset`; const scrollX = bindings.scrollX ? renderer.reference(bindings.scrollX) : x`@_window.pageXOffset`;
const scrollY = bindings.scrollY ? renderer.reference(bindings.scrollY) : x`@_window.pageYOffset`; const scrollY = bindings.scrollY ? renderer.reference(bindings.scrollY) : x`@_window.pageYOffset`;
let condition = x`
${renderer.dirty([bindings.scrollX, bindings.scrollY].filter(Boolean))} &&
!${scrolling}
`;
if (bindings.scrollX) condition = x`${condition} && !isNaN(${scrollX})`;
if (bindings.scrollY) condition = x`${condition} && !isNaN(${scrollY})`;
block.chunks.update.push(b` block.chunks.update.push(b`
if (${condition} && !${scrolling}) { if (${condition}) {
${scrolling} = true; ${scrolling} = true;
@_clearTimeout(${scrolling_timeout}); @_clearTimeout(${scrolling_timeout});
@_scrollTo(${scrollX}, ${scrollY}); @_scrollTo(${scrollX}, ${scrollY});

@ -31,12 +31,16 @@ export default class Tag extends Wrapper {
const dependencies = this.node.expression.dynamic_dependencies(); const dependencies = this.node.expression.dynamic_dependencies();
let snippet = this.node.expression.manipulate(block); let snippet = this.node.expression.manipulate(block);
const value = this.node.should_cache && block.get_unique_name(`${this.var.name}_value`); const value = block.get_unique_name(`${this.var.name}_value`);
const content = this.node.should_cache ? value : snippet; const content = this.node.should_cache ? value : snippet;
snippet = x`${snippet} + ""`; snippet = x`${snippet} + ""`;
if (this.node.should_cache) block.add_variable(value, snippet); // TODO may need to coerce snippet to string if (dependencies.length > 0) {
block.add_variable(value);
} else {
block.add_variable(value, snippet);
}
if (dependencies.length > 0) { if (dependencies.length > 0) {
let condition = block.renderer.dirty(dependencies); let condition = block.renderer.dirty(dependencies);
@ -54,6 +58,8 @@ export default class Tag extends Wrapper {
block.chunks.update.push(b`if (${condition}) ${update(content as Node)}`); block.chunks.update.push(b`if (${condition}) ${update(content as Node)}`);
} }
return { init: content }; return {
init: value
};
} }
} }

@ -26,7 +26,7 @@ process.on('unhandledRejection', err => {
unhandled_rejection = err; unhandled_rejection = err;
}); });
describe("runtime", () => { describe.only("runtime", () => {
before(() => { before(() => {
svelte = loadSvelte(false); svelte = loadSvelte(false);
svelte$ = loadSvelte(true); svelte$ = loadSvelte(true);

@ -3,7 +3,7 @@ import { env, useFakeTimers } from "../../../helpers";
let clock; let clock;
export default { export default {
before_test() { before_test() {
clock = useFakeTimers(); clock = useFakeTimers();
const window = env(); const window = env();

Loading…
Cancel
Save