← All Exercises

Track Reactive Dependencies per Key

intermediatejavascript~25min

Background

Fine-grained reactivity re-runs effects only for keys they read. Recording dependencies explicitly explains why an effect does or does not re-run.

Problem

Simulate read/write operations on a reactive proxy and record which effects depend on each key plus the final values.

Input

JSON object: { operations: [{ type: "read"|"write", key, effect?, value? }] }

Output

JSON object: { dependencies: object, values: object }

Sample tests

Sample 1

Input: {"operations":[{"type":"read","key":"count","effect":"render"},{"type":"write","key":"count","value":2},{"type":"read","key":"name","effect":"render"}]}
Expected: {"dependencies":{"count":["render"],"name":["render"]},"values":{"count":2}}

Sample 2

Input: {"operations":[{"type":"read","key":"total","effect":"a"},{"type":"read","key":"total","effect":"a"}]}
Expected: {"dependencies":{"total":["a"]},"values":{}}

Your solution

Learning objective

Explain reactive re-renders from recorded reads and writes.