1. Motivation
The current implementation of afterSetListeners in Neo.core.Observable is functional but inefficient for runtime updates. When the listeners_ config is modified, the hook removes all previously declared listeners and re-adds all the new ones. This "brute-force" approach can cause unnecessary overhead, especially in dynamic applications where listener configurations might change.
The goal is to make this process more intelligent and performant.
2. Goal
Refactor the afterSetListeners(value, oldValue) method to perform a "diff" between the oldValue and newValue objects. The method should only apply the delta, rather than completely rebuilding the listener set.
This involves:
- Identifying and Removing Listeners: Iterate through
oldValue. If a specific listener configuration does not exist in newValue, it should be removed via un().
- Identifying and Adding Listeners: Iterate through
newValue. If a listener does not exist in oldValue, or if its definition has changed, it should be added via on().
This ensures that listeners added imperatively via on() remain unaffected and that only the necessary changes are made.
3. Acceptance Criteria
- The
afterSetListeners method in src/core/Observable.mjs is updated to implement the diffing logic.
- Runtime changes to the
listeners_ config correctly add, remove, and update event listeners without affecting listeners that were not part of the config.
- All existing tests for
Observable continue to pass.
- (Optional but recommended) New unit tests are created to specifically verify the diffing logic in various scenarios (add, remove, update).
1. Motivation
The current implementation of
afterSetListenersinNeo.core.Observableis functional but inefficient for runtime updates. When thelisteners_config is modified, the hook removes all previously declared listeners and re-adds all the new ones. This "brute-force" approach can cause unnecessary overhead, especially in dynamic applications where listener configurations might change.The goal is to make this process more intelligent and performant.
2. Goal
Refactor the
afterSetListeners(value, oldValue)method to perform a "diff" between theoldValueandnewValueobjects. The method should only apply the delta, rather than completely rebuilding the listener set.This involves:
oldValue. If a specific listener configuration does not exist innewValue, it should be removed viaun().newValue. If a listener does not exist inoldValue, or if its definition has changed, it should be added viaon().This ensures that listeners added imperatively via
on()remain unaffected and that only the necessary changes are made.3. Acceptance Criteria
afterSetListenersmethod insrc/core/Observable.mjsis updated to implement the diffing logic.listeners_config correctly add, remove, and update event listeners without affecting listeners that were not part of the config.Observablecontinue to pass.