# 7060 MINBuild an External Store
# 70
60 MIN
Overview
Build the external store behind the 069-live-dashboard UI, implementing subscribe/getSnapshot, five-second polling, stale-response protection, and pause/resume in store.ts.
Solution preview
Here's the result you'll build by completing this challenge.
Requirements
- Implement
store.tssocreateMetricsStore(fetcher)returns an object withsubscribe,getSnapshot,start,refresh,pause, andresume;fetcheris optional and defaults tofetchMetrics. - The initial snapshot is
{ metrics: [], updatedAt: null, status: "idle", error: null }. getSnapshotreturns the same object reference until the state changes.subscribe(listener)registers a listener and returns a function that removes it.- Subscribers are notified after every state change.
start()fetches immediately, then every five seconds, and returns a cleanup function that stops polling.- Calling
start()while polling is already active does not create a second interval or start another fetch. - While any request (poll or manual refresh) is in flight and the store is not paused, the status is
loading. - When the store is not paused,
refresh()ends inlivewith the new metrics andupdatedAtand clearserror, or inerrorwith the message. - When a request fails, the last successful metrics remain in the snapshot.
- After an error, the next poll retries the request.
- When two requests overlap, only the response from the latest request is applied.
pause()stops polling, sets the status topaused, and ignores an in-flight response.resume()fetches immediately, restarts polling, and does nothing if the store is not paused.- A manual
refresh()while paused updates the data and keeps the statuspausedwhen it succeeds. - The
dashboardStoresingleton is exported and used by the providedApp.tsx.
Notes
- This is the other half of
069-live-dashboard: there the store was hidden and you built the UI; here you build the store thatApp.tsxconsumes. Readtypes.ts(the contract), thenApp.tsx(how it is used), thendata.ts(fetchMetrics,POLL_INTERVAL, and the test helpers). createMetricsStoreaccepts an optional fetcher so the tests can inject deterministic responses; the default isfetchMetrics.- The state lives in a closure outside React. Every update must create a new snapshot object;
getSnapshotis compared withObject.is, and returning a fresh object on every call causes an infinite render loop. - The store is DOM-agnostic: it never reads
document.hidden; the providedApp.tsxcallspause()andresume()when the tab visibility changes. - React StrictMode mounts effects twice in development, so the provided
App.tsxcan callstart()twice. Makestart()idempotent: one interval and one immediate fetch at a time. - Two clocks run in parallel: the polling interval and the request latency. A sequence number lets you discard a response that is no longer the latest, so a slow poll cannot overwrite a newer manual refresh.
- Pausing is not only about clearing the interval; invalidate the request already in flight so its response cannot flip the status back to
live. - A listener can unsubscribe while others remain; a
Set(iterated withforEach) makes that safe. refresh()while paused is a deliberate exception: when it succeeds, it updates the data but the status stayspauseduntilresume().- APIs:
useSyncExternalStore,setInterval,clearInterval.
Tests
- exports the dashboardStore singleton
- renders the provided dashboard with the store
- starts with an idle snapshot and a stable reference
- notifies subscribers when the state changes
- stops notifying after unsubscribing
- fetches immediately when started
- shows a loading status while a request is in flight
- polls every five seconds
- starts only one interval and one fetch when start is called twice
- stops polling after stop is called
- keeps the last metrics when a request fails
- recovers on the next poll after an error
- discards a stale response that arrives after a newer one
- pauses polling and marks the dashboard as paused
- ignores an in-flight response that arrives after pausing
- resumes with an immediate fetch and restarts polling
- does nothing when resumed while already running
- stays paused when refreshed manually while paused
- keeps the snapshot reference stable between updates