Build a Reactivity Engine
Interactive lab

45 min

Observable State

Implement createSignal with get, set, and subscribe — the foundation of push-based reactivity.

~/playground — 01-observable-state · edit engine.js

Live
export function createSignal(initial) {
  // TODO: closure state
  let value = initial
  const subscribers = new Set()

  return {
    get() {
      // TODO: return current value
    },
    set(next) {
      // TODO: update value and notify subscribers synchronously
    },
    subscribe(fn) {
      // TODO: add fn and return unsubscribe
    },
  }
}

Preview goal: Clicking +1 in the preview updates the count.

Checkpoints

  • Subscribers fire when set is called
  • All Sandpack tests pass