Skip to content

Function: guardContextWrites()

API / @xmachines/play-actor / guardContextWrites

function guardContextWrites(store): StateStore;

Defined in: packages/play-actor/src/context-projection.ts:151

Wraps a StateStore, and the wrapper refuses each write under /context.

A provider applies it to the store that it gives to the bindings and to the action handlers ($bindState, setState, and a chained set). The provider keeps the store without the wrapper, and it refreshes the projection through that store. The subtree is therefore read-only to the spec, and not to the machinery.

A write with a value that is identical to the current value passes in silence. A handler in the style of setState reads the complete snapshot, changes it, and writes the whole object back. The context key that goes through that round trip without a change is no attempt of a mutation. Only a write that changes the subtree throws.

Each read passes through without a change. The wrapper delegates every member explicitly, and it does not use a spread: a store from a consumer can be an instance of a class, and the methods of that instance are on the prototype. Those methods do not survive { ...store }, and the first render then fails with store.getSnapshot is not a function. The code builds the wrapper one time for each resolved store. Therefore the identity of the delegating getSnapshot and of the delegating subscribe stays stable for a consumer in the style of useSyncExternalStore.

Parameters

ParameterTypeDescription
storeStateStoreThe store below the wrapper.

Returns

StateStore

A store with a guard on set and on update.