Skip to main content
Version: Current

applyPatchMessage

@rotorsoft/act-root


@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, _v unchanged (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>