Proposal: Realtime Refetch
Preview of event sources, refetch triggers and conditional polling for @blac/async, replacing hand-written socket watchers, coalescing flags and focus listeners.
Problem
In realtime apps, a pushed socket event mostly means “something changed, refetch”. The data itself is rarely in the payload, and the wire has no sequence numbers, so after a reconnect nobody can tell what was missed. Recovery must refetch too. resource has no way to say any of this, so each container hand-rolls it:
- Watcher plumbing. A class subscribes to the socket, filters events, and also fires its callback on
reconnected,authenticatedandresubscribed. - Coalescing flags. An
isRefreshing/pendingRefreshpair keeps overlapping refreshes from stacking up. In one production codebase there are about 51 hand-written debounce or coalesce guards. - Fallback polling. While the socket is down, a
setIntervalpolls (45s), and a state subscription starts and stops it. - Page-level triggers.
onlineandvisibilitychangelisteners, and on mobile an app-resume hook (9 uses in one app), each re-signal a refetch.
resource().reload() is not the answer to any of these. It aborts the call in flight, which is right for a user-initiated reload but wrong for a burst of events: a steady stream would keep cancelling the request that is about to land.
Proposed API
Source adapter
A source turns an app’s socket into two subscriptions. It is written once and registered once:
interface RealtimeSource<E = unknown> { /** Called for every pushed event. */ subscribe(cb: (event: E) => void): () => void; /** Called when events may have been missed: reconnect, re-auth, re-observe. */ onRecover(cb: () => void): () => void;}
configureAsync({ sources: [orderEvents] });Per-resource refetchOn
resource(this, 'order', fn, { auto: true, refetchOn: { event: (e) => e.type === 'order.updated' && e.orderId === this.args.orderId, recover: true, // the source's onRecover focus: true, // tab becomes visible online: true, // browser comes back online },});eventis a predicate. Returningtruetriggers a refetch. Omit it to ignore events.recover,focusandonlineare plain switches.- Triggers are pluggable:
configureAsync({ triggers: { resume } })registers a custom trigger (for example a native app-resume event), enabled per resource withrefetchOn: { resume: true }.focusandonlineship built in.
Coalescing
Every trigger goes through one rule: one load in flight, plus at most one trailing rerun.
- Idle: the trigger starts a load.
- A load is in flight: the trigger sets a “rerun” flag and returns. When the load settles, one more load runs and the flag clears.
Unlike reload, nothing is aborted. This is safe because refetches are GETs, so the trailing load always reads state at least as new as the last event.
Inactive hosts
Same rule as invalidate today. A trigger that arrives while the host is inactive marks the resource stale, and the next activation loads it. Nothing is fetched for a screen nobody is looking at.
refetchInterval
refetchInterval: (state) => (socket.connected ? false : 45_000),A function of the host state returning milliseconds or false. The interval runs only while the host is active, goes through the same coalescing, and is re-evaluated after each state change. Returning false stops it. This gives “poll only while the socket is unhealthy” without a state subscription and a timer field, provided the connection status is readable from state (see open questions).
Use
const orderEvents: RealtimeSource<OrderEvent> = { subscribe: (cb) => socket.on('event', cb), onRecover: (cb) => socket.onAny(['reconnected', 'authenticated', 'resubscribed'], cb),};
configureAsync({ sources: [orderEvents] });Before and after
Cubit: an order detail screen
Adapted from a production container and simplified.
class OrderDetailCubit extends Cubit<OrderDetailState> { private isRefreshing = false; private pendingRefresh = false; private watcher = new EventWatcher(); private fallbackTimer?: ReturnType<typeof setInterval>;
startWatching = (orderId: string) => { this.watcher.start( (e) => e.subjects?.some((s) => s.type === 'order' && s.id === orderId) ?? false, this.refresh, ); this.syncFallbackPoll(); this.unsubscribeSocket = socket.subscribe(() => this.syncFallbackPoll()); window.addEventListener('online', this.refresh); document.addEventListener('visibilitychange', this.onVisibility); };
stopWatching = () => { this.watcher.stop(); this.unsubscribeSocket?.(); clearInterval(this.fallbackTimer); window.removeEventListener('online', this.refresh); document.removeEventListener('visibilitychange', this.onVisibility); };
private onVisibility = () => { if (document.visibilityState === 'visible') this.refresh(); };
private syncFallbackPoll() { const down = socket.state.status !== 'authenticated'; if (!down) { clearInterval(this.fallbackTimer); this.fallbackTimer = undefined; } else if (!this.fallbackTimer) { this.fallbackTimer = setInterval(this.refresh, 45_000); } }
private refresh = async () => { const orderId = this.state.order?.id; if (!orderId) return; if (this.isRefreshing) { this.pendingRefresh = true; return; } this.isRefreshing = true; try { await this.loadOrder(orderId, { background: true }); } catch (e) { reportError(e); } finally { this.isRefreshing = false; if (this.pendingRefresh) { this.pendingRefresh = false; void this.refresh(); } } };}class OrderDetailCubit extends Cubit< { order: AsyncState<Order> }, { orderId: string }> { constructor() { super({ order: idle() }); }
load = resource( this, 'order', (signal, { orderId }) => api.getOrder(orderId, { signal }), { auto: true, refetchOn: { event: (e) => e.subjects.some( (s) => s.type === 'order' && s.id === this.args.orderId, ), recover: true, focus: true, online: true, }, refetchInterval: () => socket.state.status === 'authenticated' ? false : 45_000, }, );}What goes away: the watcher class, both coalescing flags, the fallback timer and its state subscription, the two page listeners with their cleanup, and startWatching / stopWatching (activation and deactivation do that now).
Behavior differences to review:
- Background refresh. The “before” passes
background: trueso the UI shows no spinner. A resource refetch keepsdataand setsstatus: 'loading', so the view must show a spinner only whendataisundefined. Is that enough, or does a silent refetch need its own flag? - Errors. The “before” reports to
reportErroronly. A failed refetch now also setsstatus: 'error'on the slot, while keeping the olddata. A view that renderserroras a blocking state would flash it during a flaky poll. refetchIntervalreadssocket.state. It is not host state, so nothing re-evaluates it when the socket recovers. See open questions.- Chained reloads. The real container also reloads a sibling timeline container after each refresh. That moves to the sibling’s own
refetchOn, or to awatchon the order slot.
Test
it('runs one trailing refresh for events during a refresh', async () => { const done = cubit.refresh(); cubit.refresh(); cubit.refresh(); await done; expect(loadOrder).toHaveBeenCalledTimes(2);});it('refetches on matching events only', async () => { const source = fakeSource(); configureAsync({ sources: [source] }); const cubit = await activate(OrderDetailCubit, { orderId: 'o1' }); source.emit({ subjects: [{ type: 'order', id: 'o2' }] }); source.emit({ subjects: [{ type: 'order', id: 'o1' }] }); await waitForState(cubit, (s) => s.order.status === 'success'); expect(api.getOrder).toHaveBeenCalledTimes(2); // activation + one match});fakeSource would live in @blac/async/testing. The “before” test is a typical shape, not copied from a real suite.
Library change
config.ts:sourcesandtriggersinAsyncConfig, withfocusandonlinebuilt in.resource.ts:refetchOnandrefetchInterval. Subscriptions are created in the existingonActivatedcallback and torn down with its signal. The coalescer is a rerun flag aroundrun, about 15 lines.- Tests: coalescing (one in flight, one trailing), non-matching events ignored, stale-on-inactive then load on activation, recovery refetch, interval start and stop, unsubscribe on deactivation.
- Docs: a “Realtime” section in
guide/async-resources.md.
Effort: about 1–1.5 days.
Open questions
- Event typing per source.
configureAsync({ sources })erasesE. Options: module augmentation of one app-wide event type, a generic onresource, or a typed helperrefetchOn: sourceOf(orderEvents, { event }). Which fits apps with several sockets? - Debounce in
event? A burst of events already coalesces to two loads. Is adebounce: msoption still needed, or is thatdebouncedfrom activation timers? staleTime. Triggers bypass it by default, since “something changed” should not be ignored because the last load was recent.focusandonlineare the exception: they could respectstaleTimeso that tab switching does not refetch every time. Should that be the default for those two?- Reactive intervals.
refetchInterval(state)only sees host state. To react to connection status, the host must mirror it into state, or the option takes a signal. Which is simpler to document? - Several resources, one event. Each resource subscribes separately, so one event can start several loads. That matches today, and
invalidatetags remain the way to share a trigger. Is that enough?