HardPro challengePythonJavaScriptTypeScript

Proxy Observable State

ProxyPatternsObjects

Implement createObservable(target) using Proxy that:

  • Intercepts property get and set operations.
  • Returns the actual value on get.
  • On set, updates the value AND calls registered listeners.

subscribe(prop, listener) — registers a listener called with
(newValue, oldValue) whenever prop changes.

solve(ops) drives the state and returns all listener calls.

Sample tests

Test #1Single set triggers listener
Input: [{"count":0},[{"prop":"count","type":"set","value":1}]]
Output: [{"prop":"count","newVal":1,"oldVal":0}]
Test #2Two sets produce two listener calls with correct old values
Input: [{"count":0},[{"prop":"count","type":"set","value":5},{"prop":"count","type":"set","value":10}]]
Output: [{"prop":"count","newVal":5,"oldVal":0},{"prop":"count","newVal":10,"oldVal":5}]
Test #3No ops → no listener calls
Input: [{"count":0},[]]
Output: []
Test #4Setting same value still fires listener
Input: [{"count":100},[{"prop":"count","type":"set","value":100}]]
Output: [{"prop":"count","newVal":100,"oldVal":100}]
Test #5Three sequential mutations
Input: [{"count":0},[{"prop":"count","type":"set","value":1},{"prop":"count","type":"set","value":2},{"prop":"count","type":"set","value":3}]]
Output: [{"prop":"count","newVal":1,"oldVal":0},{"prop":"count","newVal":2,"oldVal":1},{"prop":"count","newVal":3,"oldVal":2}]