Skip to content

Commit 0a76f66

Browse files
committed
add updating guard to binding callback
1 parent d19bcef commit 0a76f66

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -359,18 +359,25 @@ export default class InlineComponentWrapper extends Wrapper {
359359
args.push(renderer.reference(name));
360360
});
361361

362-
363362
block.chunks.init.push(b`
364363
function ${id}(#value) {
365-
${callee}.call(null, #value, ${args});
364+
if (!${updating}) {
365+
${updating} = true;
366+
${callee}.call(null, #value, ${args});
367+
@add_flush_callback(() => ${updating} = false);
368+
}
366369
}
367370
`);
368371

369372
block.maintain_context = true; // TODO put this somewhere more logical
370373
} else {
371374
block.chunks.init.push(b`
372375
function ${id}(#value) {
373-
${callee}.call(null, #value);
376+
if (!${updating}) {
377+
${updating} = true;
378+
${callee}.call(null, #value);
379+
@add_flush_callback(() => ${updating} = false);
380+
}
374381
}
375382
`);
376383
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let value = '';
3+
</script>
4+
5+
<input bind:value />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export default {
2+
html: `
3+
<input />
4+
<input />
5+
<div></div>
6+
`,
7+
8+
async test({ assert, component, target, window }) {
9+
let count = 0;
10+
component.callback = () => {
11+
count++;
12+
};
13+
14+
const [input1, input2] = target.querySelectorAll("input");
15+
16+
input1.value = "1";
17+
await input1.dispatchEvent(new window.Event("input"));
18+
19+
assert.htmlEqual(
20+
target.innerHTML,
21+
`
22+
<input />
23+
<input />
24+
<div>1</div>
25+
`
26+
);
27+
assert.equal(input1.value, "1");
28+
assert.equal(input2.value, "1");
29+
assert.equal(count, 1);
30+
31+
input2.value = "123";
32+
await input2.dispatchEvent(new window.Event("input"));
33+
34+
assert.htmlEqual(
35+
target.innerHTML,
36+
`
37+
<input />
38+
<input />
39+
<div>123</div>
40+
`
41+
);
42+
assert.equal(input1.value, "123");
43+
assert.equal(input2.value, "123");
44+
assert.equal(count, 2);
45+
46+
input1.value = "456";
47+
await input1.dispatchEvent(new window.Event("input"));
48+
49+
assert.htmlEqual(
50+
target.innerHTML,
51+
`
52+
<input />
53+
<input />
54+
<div>456</div>
55+
`
56+
);
57+
assert.equal(input1.value, "456");
58+
assert.equal(input2.value, "456");
59+
assert.equal(count, 3);
60+
},
61+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import { writable } from 'svelte/store';
3+
import Input from './Input.svelte';
4+
5+
let value = writable({ value: '' });
6+
7+
export let callback = () => {};
8+
9+
value.subscribe(() => {
10+
callback();
11+
})
12+
</script>
13+
14+
<input bind:value={$value.value} />
15+
16+
<Input bind:value={$value.value}/>
17+
18+
<div>{$value.value}</div>

0 commit comments

Comments
 (0)