applyPatchMessage
@rotorsoft/act-root / act-http/src/sse / applyPatchMessage
Function: applyPatchMessage()
applyPatchMessage<
S>(msg,cached):ApplyResult<S>
Defined in: libs/act-http/src/sse/apply-patch.ts:40
Apply a version-keyed patch message to the client's cached state.
Version logicโ
- All patches older than cached โ "stale" (client already ahead)
- Gap between cached version and first patch โ "behind" (client missed versions, must resync)
- Contiguous from cached version โ apply in order
- No baseline (fresh client) โ the genesis patch (version 0) folds onto init state; a first patch at version โฅ 1 is "behind" (can't build from init)
- Overlay frame (PatchMessage._overlay) at the current version โ
merged on top of cached state,
_vunchanged (presence / computed fields reach caught-up clients instead of being dropped as stale)
Usage (React Query)โ
onData: (msg) => {
const cached = utils.get_state.get_data({ streamId });
const result = applyPatchMessage(msg, cached);
if (result.ok) {
utils.get_state.setData({ streamId }, result.state);
} else if (result.reason === "behind") {
utils.get_state.invalidate({ streamId }); // trigger full refetch
}
// "stale" โ no-op, client already has newer state
}
Type Parametersโ
Sโ
S extends BroadcastState
Parametersโ
msgโ
PatchMessage<S>
cachedโ
S | null | undefined
Returnsโ
ApplyResult<S>