Skip to content

Optimize core.Observable#afterSetListeners to perform a diff-based update #7091

@tobiu

Description

@tobiu

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:

  1. Identifying and Removing Listeners: Iterate through oldValue. If a specific listener configuration does not exist in newValue, it should be removed via un().
  2. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions