Fix the re-render counter for a Zustand port
Your task
An AI agent moved your React app's state from Redux to Zustand. The data is the same, but you want to check that components re-render as often as before. You model the store and count the re-renders a list of writes causes.
An assistant wrote count_rerenders(subscriptions, updates). It is in the editor. It has a bug. Find it and fix it.
Input and output:
subscriptions: one string per component,"Name:fields". The fields are*for the whole store, or field names joined by|.updates: the store writes in order, each"field=value".- Return one re-render count per subscription, in the same order.
Rules the tests check:
- Split a subscription at its first
:. With no:, the component reads nothing. The name is ignored. - Trim the part after the
:. If it is exactly*, the component reads the whole store. Otherwise split it on|, trim each field name and drop empty ones. - Split an update at its first
=and trim both sides. Skip an update with no=or with an empty field name. - The store starts empty. An update is a change when its field has never been written, or when the field's current value differs from the new value.
- An update that writes the value its field already holds changes nothing, and nobody re-renders.
- On a change, every whole-store component re-renders once. Any other component re-renders once if it reads that field, even when it lists the field twice.
Examples
count_rerenders(subscriptions: list[str], updates: list[str]) -> list[int]Example 1
Input
count_rerenders(["Cart:items|total", "Header:userName", "Debug:*"], ["items=2", "items=2", "userName=Ada"])Output
[1, 1, 2]Example 2
Input
count_rerenders(["Whole:*"], [])Output
[0]Example 3
Input
count_rerenders([], ["a=1"])Output
[]