mirror of https://github.com/sveltejs/svelte
parent
8072f6ad77
commit
596c432f5e
@ -1,50 +0,0 @@
|
|||||||
import deindent from '../../../../utils/deindent';
|
|
||||||
import { DomGenerator } from '../../index';
|
|
||||||
import Block from '../../Block';
|
|
||||||
import { Node } from '../../../../interfaces';
|
|
||||||
import State from '../../State';
|
|
||||||
|
|
||||||
export default function visitTag(
|
|
||||||
generator: DomGenerator,
|
|
||||||
block: Block,
|
|
||||||
state: State,
|
|
||||||
node: Node,
|
|
||||||
name: string,
|
|
||||||
update: (value: string) => string
|
|
||||||
) {
|
|
||||||
const { indexes } = block.contextualise(node.expression);
|
|
||||||
const { dependencies, snippet } = node.metadata;
|
|
||||||
|
|
||||||
const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index));
|
|
||||||
|
|
||||||
const shouldCache = (
|
|
||||||
node.expression.type !== 'Identifier' ||
|
|
||||||
block.contexts.has(node.expression.name) ||
|
|
||||||
hasChangeableIndex
|
|
||||||
);
|
|
||||||
|
|
||||||
const value = shouldCache && block.getUniqueName(`${name}_value`);
|
|
||||||
const content = shouldCache ? value : snippet;
|
|
||||||
|
|
||||||
if (shouldCache) block.addVariable(value, snippet);
|
|
||||||
|
|
||||||
if (dependencies.length || hasChangeableIndex) {
|
|
||||||
const changedCheck = (
|
|
||||||
(block.hasOutroMethod ? `#outroing || ` : '') +
|
|
||||||
dependencies.map((dependency: string) => `changed.${dependency}`).join(' || ')
|
|
||||||
);
|
|
||||||
|
|
||||||
const updateCachedValue = `${value} !== (${value} = ${snippet})`;
|
|
||||||
|
|
||||||
const condition = shouldCache ?
|
|
||||||
(dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue) :
|
|
||||||
changedCheck;
|
|
||||||
|
|
||||||
block.builders.update.addConditional(
|
|
||||||
condition,
|
|
||||||
update(content)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { init: content };
|
|
||||||
}
|
|
@ -0,0 +1,47 @@
|
|||||||
|
import deindent from '../../utils/deindent';
|
||||||
|
import Node from './Node';
|
||||||
|
import Block from '../../dom/Block';
|
||||||
|
import State from '../../dom/State';
|
||||||
|
|
||||||
|
export default class Tag extends Node {
|
||||||
|
renameThisMethod(
|
||||||
|
block: Block,
|
||||||
|
update: ((value: string) => string)
|
||||||
|
) {
|
||||||
|
const { indexes } = block.contextualise(this.expression);
|
||||||
|
const { dependencies, snippet } = this.metadata;
|
||||||
|
|
||||||
|
const hasChangeableIndex = Array.from(indexes).some(index => block.changeableIndexes.get(index));
|
||||||
|
|
||||||
|
const shouldCache = (
|
||||||
|
this.expression.type !== 'Identifier' ||
|
||||||
|
block.contexts.has(this.expression.name) ||
|
||||||
|
hasChangeableIndex
|
||||||
|
);
|
||||||
|
|
||||||
|
const value = shouldCache && block.getUniqueName(`${this.var}_value`);
|
||||||
|
const content = shouldCache ? value : snippet;
|
||||||
|
|
||||||
|
if (shouldCache) block.addVariable(value, snippet);
|
||||||
|
|
||||||
|
if (dependencies.length || hasChangeableIndex) {
|
||||||
|
const changedCheck = (
|
||||||
|
(block.hasOutroMethod ? `#outroing || ` : '') +
|
||||||
|
dependencies.map((dependency: string) => `changed.${dependency}`).join(' || ')
|
||||||
|
);
|
||||||
|
|
||||||
|
const updateCachedValue = `${value} !== (${value} = ${snippet})`;
|
||||||
|
|
||||||
|
const condition = shouldCache ?
|
||||||
|
(dependencies.length ? `(${changedCheck}) && ${updateCachedValue}` : updateCachedValue) :
|
||||||
|
changedCheck;
|
||||||
|
|
||||||
|
block.builders.update.addConditional(
|
||||||
|
condition,
|
||||||
|
update(content)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { init: content };
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue