Description
This feature introduces fundamental enhancements to the Neo.mjs core reactivity system, enabling automatic and dynamic dependency tracking for
Neo.core.Effect instances and improving the flexibility of effect functions. These changes are crucial for building more robust and efficient reactive
components and data bindings.
Motivation
To achieve truly automatic and dynamic reactivity, the Neo.core.Effect system needs a reliable mechanism to:
- Automatically discover its dependencies: Instead of manually declaring what data an effect depends on, the system should infer dependencies by
observing which reactive properties are accessed during the effect's execution.
- Dynamically update dependencies: The set of dependencies an effect relies on can change during its lifetime (e.g., due to conditional logic within
the effect's function). The system must be able to clear old dependencies and establish new ones on demand.
Changes Made
src/core/Config.mjs - get() method modification:
- The
get() method of Neo.core.Config has been enhanced to integrate with the Neo.core.EffectManager.
- When an
Effect is actively running (as determined by EffectManager.getActiveEffect()), any Config instance whose value is accessed via
config.get() will automatically register itself as a dependency of that active Effect.
- This change enables the
Effect system to automatically track which Config instances its function relies on, replacing the need for manual
dependency declaration or static analysis (like regex parsing).
src/core/Effect.mjs - fn property as getter/setter:
- The
fn property of Neo.core.Effect has been converted into a getter/setter.
- Assigning a new function to
effect.fn (e.g., effect.fn = newFunction;) now automatically triggers effect.run().
- This ensures that whenever the effect's logic changes, its
run() method is immediately invoked. The run() method, in turn, clears all previous
dependencies and re-establishes new ones based on the newly assigned function's execution. This is vital for effects whose dependency set changes
dynamically.
test/siesta/tests/core/Effect.mjs - Comprehensive Test Suite:
- A dedicated test file has been created/updated to thoroughly validate the behavior of
Neo.core.Effect and its interaction with Neo.core.Config
and Neo.core.EffectManager.
- The tests cover:
- Basic dependency tracking and re-execution on dependency change.
- The dynamic re-tracking of dependencies when
effect.fn is reassigned, ensuring old dependencies are cleaned up and new ones are established.
- The correct functioning of
EffectManager's stack for managing active effects.
Benefits
- True Automatic Reactivity: Eliminates manual dependency management, making reactive programming more intuitive and less error-prone.
- Dynamic Dependency Sets: Effects can now adapt to changing conditions within their functions, automatically tracking and reacting to new data
sources.
- Improved Code Clarity: The core reactivity logic is encapsulated within
Effect and Config, leading to cleaner and more declarative component
code.
- Foundation for Advanced Features: These enhancements lay critical groundwork for more sophisticated reactive patterns, such as computed properties
and highly optimized data flows.
Description
This feature introduces fundamental enhancements to the Neo.mjs core reactivity system, enabling automatic and dynamic dependency tracking for
Neo.core.Effectinstances and improving the flexibility of effect functions. These changes are crucial for building more robust and efficient reactive components and data bindings.Motivation
To achieve truly automatic and dynamic reactivity, the
Neo.core.Effectsystem needs a reliable mechanism to:Changes Made
src/core/Config.mjs-get()method modification:get()method ofNeo.core.Confighas been enhanced to integrate with theNeo.core.EffectManager.Effectis actively running (as determined byEffectManager.getActiveEffect()), anyConfiginstance whose value is accessed viaconfig.get()will automatically register itself as a dependency of that activeEffect.Effectsystem to automatically track whichConfiginstances its function relies on, replacing the need for manual dependency declaration or static analysis (like regex parsing).src/core/Effect.mjs-fnproperty as getter/setter:fnproperty ofNeo.core.Effecthas been converted into a getter/setter.effect.fn(e.g.,effect.fn = newFunction;) now automatically triggerseffect.run().run()method is immediately invoked. Therun()method, in turn, clears all previous dependencies and re-establishes new ones based on the newly assigned function's execution. This is vital for effects whose dependency set changes dynamically.test/siesta/tests/core/Effect.mjs- Comprehensive Test Suite:Neo.core.Effectand its interaction withNeo.core.ConfigandNeo.core.EffectManager.effect.fnis reassigned, ensuring old dependencies are cleaned up and new ones are established.EffectManager's stack for managing active effects.Benefits
EffectandConfig, leading to cleaner and more declarative component code.