Skip to content

Commit 00e0de7

Browse files
committed
[FIX] Prevent multiple deletes being called
1 parent 0fb25d5 commit 00e0de7

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

one-way-data-binding-library.mjs

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@ export default (bindings) => {
1717
matchedBy.forEach((match) => {
1818
// No binding exists for this key; bind and call create method
1919
if (!(key in bound)) {
20-
nextBound[key] = { methods: bindings[match](), data };
21-
if (nextBound[key].methods.create) {
22-
nextBound[key].methods.create(data, state);
20+
bound[key] = { methods: bindings[match](), data };
21+
if (bound[key].methods.create) {
22+
bound[key].methods.create(data, state);
2323
}
2424
}
2525
// Binding exists and data reference has changed; call update method
2626
else if (bound[key].methods.update && data !== bound[key].data) {
27-
nextBound[key] = { methods: bound[key].methods, data };
28-
nextBound[key].methods.update(data, state);
27+
bound[key].methods.update(data, state);
2928
}
29+
30+
nextBound[key] = { methods: bound[key].methods, data };
3031
});
3132
});
3233

3334
Object.keys(bound).forEach((key) => {
3435
if (!(key in nextBound) || nextBound[key] === undefined) {
36+
console.log("delete", key);
3537
bound[key].methods.delete?.(state);
3638
}
3739
});

one-way-data-binding-library.test.mjs

+19
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,23 @@ describe("one-way-data-binding-library", () => {
254254

255255
expect(runs).toBe(1);
256256
});
257+
258+
it("runs a single delete on popping a list", () => {
259+
let runs = 0;
260+
261+
const foo = () => ({
262+
delete: () => runs++,
263+
});
264+
265+
const run = bind({
266+
"state.foo[*]": foo,
267+
});
268+
269+
run(() => ({ state: { foo: [1, 2, 3] } }));
270+
run((state) => {
271+
state.state.foo.pop();
272+
});
273+
274+
expect(runs).toBe(1);
275+
});
257276
});

0 commit comments

Comments
 (0)