With the recent shift in the frontend ecosystem—from Angular Signals to Vue Refs and Solid primitives—reactivity has become the fundamental driver of modern UI development.
Developers rely on these abstractions daily. Setting a signal updates the view. Computed values cache their results efficiently. But there is a distinct difference between knowing how to use a tool and understanding how it works.
ts-reactivity-engine is a zero-dependency reactivity engine built from scratch in TypeScript. It demonstrates the architectural decisions behind modern reactive systems by implementing the core primitives directly.
The implementation addresses three critical questions: Why is "glitch-free" execution essential? How does dependency tracking work without manual subscriptions? What is the real difference between eager and lazy evaluation?
The Engineering Challenge#
The goal was to create a scaffold that evolves from a naive implementation to a robust, architecture-aware engine, replicating the journey framework authors take by solving one bottleneck at a time.
The project is structured into four distinct levels of complexity.
Level 1: The Naive "Push" Model#
The simplest definition of reactivity is Dependency Tracking.
The basic signal and effect use a global context variable: when a signal is read, it registers who is listening. When the signal changes, it immediately "pushes" updates to all listeners.
The problem: While functional, this "eager" approach reveals why frameworks are complex.
- Memory leaks: Without careful management, effects subscribe to signals and never let go, creating "zombie listeners."
- Glitches: Updating multiple signals causes cascading, redundant re-renders—a performance killer in real applications.
Level 2: Safety and Batching#
To address the limitations of the first implementation, the engine introduces Cleanup and Batching.
The lifecycle becomes critical. Before an effect re-runs, it must "forget" its previous dependencies. This is crucial for dynamic branching—for example, condition ? A() : B(). If condition flips, the engine must stop listening to A immediately.
A global batching queue ensures that multiple synchronous state changes result in a single, consistent execution, preventing the "Diamond Problem."
Level 3: The "Push-Pull" Architecture#
The most significant architectural shift: modern frameworks don't just "push" updates. They use a Push-Pull hybrid.
- Push: When a signal changes, it marks its dependents as "dirty." It does not re-run them immediately.
- Pull: A computed value is re-evaluated only when it is actually read.
This laziness ensures that expensive computations for hidden UI components are never executed unnecessarily. It also naturally solves topological sorting issues, keeping the graph consistent without complex scheduling algorithms.
Level 4: Advanced Primitives#
Finally, the engine integrates features that make a reactivity system robust enough for complex scenarios.
Cycle Detection protects the browser stack from infinite loops. The Owner Pattern—similar to createRoot—manages the disposal of entire reactive trees, ensuring cleanup when components unmount or workflows complete.
Conclusion#
Building ts-reactivity-engine demonstrates that deconstructing reactive primitives opens up the "black box" of modern frameworks. When debugging complex race conditions or optimizing performance in an application, understanding the graph structures underneath provides a clear mental model.
The code and evolutionary steps are available in the repository below.
Explore ts-reactivity-engine on GitHub
Originally published on Medium.