Skip to content

Commit 8430a1e

Browse files
committed
fix(state): Fix multi-binding overwrites and effect leaks in createBinding (#9305)
1 parent 1fa6961 commit 8430a1e

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

src/state/Provider.mjs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,28 +243,35 @@ class Provider extends Base {
243243
*/
244244
createBinding(componentId, configKey, formatter) {
245245
const
246-
me = this,
247-
effect = new Effect(() => {
248-
const component = Neo.get(componentId);
246+
me = this,
247+
mapKey = `${componentId}-${configKey}`,
248+
oldEffect = me.#bindingEffects.get(mapKey);
249249

250-
if (component && !component.isDestroyed) {
251-
const
252-
hierarchicalData = me.getHierarchyData(),
253-
newValue = Neo.isFunction(formatter) ? formatter.call(me, hierarchicalData) : hierarchicalData[formatter];
250+
if (oldEffect) {
251+
oldEffect.destroy()
252+
}
254253

255-
component._skipTwoWayPush = configKey;
256-
component[configKey] = newValue;
257-
delete component._skipTwoWayPush
258-
}
259-
});
254+
const effect = new Effect(() => {
255+
const component = Neo.get(componentId);
256+
257+
if (component && !component.isDestroyed) {
258+
const
259+
hierarchicalData = me.getHierarchyData(),
260+
newValue = Neo.isFunction(formatter) ? formatter.call(me, hierarchicalData) : hierarchicalData[formatter];
261+
262+
component._skipTwoWayPush = configKey;
263+
component[configKey] = newValue;
264+
delete component._skipTwoWayPush
265+
}
266+
});
260267

261-
me.#bindingEffects.set(componentId, effect);
268+
me.#bindingEffects.set(mapKey, effect);
262269

263270
// The effect observes the component's destruction to clean itself up.
264271
me.observeConfig(componentId, 'isDestroying', (value) => {
265272
if (value) {
266273
effect.destroy();
267-
me.#bindingEffects.delete(componentId)
274+
me.#bindingEffects.delete(mapKey)
268275
}
269276
});
270277

0 commit comments

Comments
 (0)