-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46b2e77
commit 6af23ba
Showing
9 changed files
with
141 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/compiler/compile/render_dom/wrappers/shared/bind_this.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import flatten_reference from '../../../utils/flatten_reference'; | ||
import deindent from '../../../utils/deindent'; | ||
import Component from '../../../Component'; | ||
import Block from '../../Block'; | ||
import Binding from '../../../nodes/Binding'; | ||
|
||
export default function bind_this(component: Component, block: Block, binding: Binding, variable: string) { | ||
const fn = component.get_unique_name(`${variable}_binding`); | ||
|
||
component.add_var({ | ||
name: fn, | ||
internal: true, | ||
referenced: true | ||
}); | ||
|
||
let lhs; | ||
let object; | ||
|
||
if (binding.is_contextual && binding.expression.node.type === 'Identifier') { | ||
// bind:x={y} — we can't just do `y = x`, we need to | ||
// to `array[index] = x; | ||
const { name } = binding.expression.node; | ||
const { snippet } = block.bindings.get(name); | ||
lhs = snippet; | ||
|
||
// TODO we need to invalidate... something | ||
} else { | ||
object = flatten_reference(binding.expression.node).name; | ||
lhs = component.source.slice(binding.expression.node.start, binding.expression.node.end).trim(); | ||
} | ||
|
||
const contextual_dependencies = Array.from(binding.expression.contextual_dependencies); | ||
|
||
if (contextual_dependencies.length) { | ||
component.partly_hoisted.push(deindent` | ||
function ${fn}(${['$$value', ...contextual_dependencies].join(', ')}) { | ||
if (${lhs} === $$value) return; | ||
${lhs} = $$value; | ||
${object && component.invalidate(object)} | ||
} | ||
`); | ||
|
||
const args = []; | ||
for (const arg of contextual_dependencies) { | ||
args.push(arg); | ||
block.add_variable(arg, `ctx.${arg}`); | ||
} | ||
|
||
const assign = block.get_unique_name(`assign_${variable}`); | ||
const unassign = block.get_unique_name(`unassign_${variable}`); | ||
|
||
block.builders.init.add_block(deindent` | ||
const ${assign} = () => ctx.${fn}(${[variable].concat(args).join(', ')}); | ||
const ${unassign} = () => ctx.${fn}(${['null'].concat(args).join(', ')}); | ||
`); | ||
|
||
const condition = Array.from(contextual_dependencies).map(name => `${name} !== ctx.${name}`).join(' || '); | ||
|
||
block.builders.update.add_line(deindent` | ||
if (${condition}) { | ||
${unassign}(); | ||
${args.map(a => `${a} = ctx.${a}`).join(', ')}; | ||
@add_binding_callback(${assign}); | ||
}` | ||
); | ||
|
||
block.builders.destroy.add_line(`${unassign}();`); | ||
return `@add_binding_callback(${assign});`; | ||
} | ||
|
||
component.partly_hoisted.push(deindent` | ||
function ${fn}($$value) { | ||
${lhs} = $$value; | ||
${object && component.invalidate(object)} | ||
} | ||
`); | ||
|
||
block.builders.destroy.add_line(`ctx.${fn}(null);`); | ||
return `@add_binding_callback(() => ctx.${fn}(${variable}));`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test/runtime/samples/binding-this-each-block-property-component/Foo.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
export function isFoo() { | ||
return true; | ||
} | ||
</script> | ||
|
||
<p><slot></slot></p> |
12 changes: 12 additions & 0 deletions
12
test/runtime/samples/binding-this-each-block-property-component/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default { | ||
html: ``, | ||
|
||
async test({ assert, component, target }) { | ||
component.visible = true; | ||
assert.htmlEqual(target.innerHTML, ` | ||
<p>a</p> | ||
`); | ||
|
||
assert.ok(component.items[0].ref.isFoo()); | ||
} | ||
}; |
13 changes: 13 additions & 0 deletions
13
test/runtime/samples/binding-this-each-block-property-component/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
import Foo from './Foo.svelte'; | ||
export let visible = false; | ||
export let items = [{ value: 'a', ref: null }]; | ||
</script> | ||
|
||
{#if visible} | ||
{#each items as item} | ||
<Foo bind:this={item.ref}>{item.value}</Foo> | ||
{/each} | ||
{/if} |
12 changes: 12 additions & 0 deletions
12
test/runtime/samples/binding-this-each-block-property/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default { | ||
html: ``, | ||
|
||
async test({ assert, component, target }) { | ||
component.visible = true; | ||
assert.htmlEqual(target.innerHTML, ` | ||
<div>a</div> | ||
`); | ||
|
||
assert.equal(component.items[0].ref, target.querySelector('div')); | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
test/runtime/samples/binding-this-each-block-property/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
export let visible = false; | ||
export let items = [{ value: 'a', ref: null }]; | ||
</script> | ||
|
||
{#if visible} | ||
{#each items as item} | ||
<div bind:this={item.ref}>{item.value}</div> | ||
{/each} | ||
{/if} |