@statesync/mobx / MobXSnapshotApplierOptions
Type Alias: MobXSnapshotApplierOptions<State, Data>
type MobXSnapshotApplierOptions<State, Data> =
| {
mode?: "patch";
omitKeys?: ReadonlyArray<keyof State>;
pickKeys?: ReadonlyArray<keyof State>;
runInAction?: (fn) => void;
strict?: boolean;
toState?: (data, ctx) => Partial<State>;
}
| {
mode: "replace";
omitKeys?: ReadonlyArray<keyof State>;
pickKeys?: ReadonlyArray<keyof State>;
runInAction?: (fn) => void;
strict?: boolean;
toState?: (data, ctx) => State;
};Defined in: mobx.ts:60
Configuration options for createMobXSnapshotApplier.
This is a discriminated union on the mode field:
- When
modeis'patch'(or omitted),toStateis expected to returnPartial<State>. - When
modeis'replace',toStateis expected to return the fullState.
Type Parameters
| Type Parameter | Description |
|---|---|
State extends Record<string, unknown> | The shape of the MobX observable's state object. |
Data | The snapshot payload type received from the sync engine. Defaults to State when the snapshot data matches the store shape directly. |
Type Declaration
{
mode?: "patch";
omitKeys?: ReadonlyArray<keyof State>;
pickKeys?: ReadonlyArray<keyof State>;
runInAction?: (fn) => void;
strict?: boolean;
toState?: (data, ctx) => Partial<State>;
}mode?
optional mode: "patch";The apply strategy. Defaults to 'patch'.
Default Value
'patch'
omitKeys?
optional omitKeys: ReadonlyArray<keyof State>;pickKeys?
optional pickKeys: ReadonlyArray<keyof State>;runInAction()?
optional runInAction: (fn) => void;A function that wraps state mutations in a MobX action/transaction.
Pass runInAction from mobx here to ensure the adapter works correctly with enforceActions: 'always' or 'observed' (the recommended defaults). All property assignments happen inside a single call, so MobX batches updates and triggers reactions once.
When omitted, the adapter assigns keys directly on the observable. This works when enforceActions is 'never', but will throw at runtime if MobX strict mode is enabled and the store has active observers.
Parameters
| Parameter | Type |
|---|---|
fn | () => void |
Returns
void
Example
import { runInAction } from 'mobx';
const applier = createMobXSnapshotApplier(store, { runInAction });Default Value
undefined — mutations are applied without wrapping.
strict?
optional strict: boolean;When true, the applier throws if toState returns a non-plain-object value. When false, such values are silently ignored.
Default Value
true
toState()?
optional toState: (data, ctx) => Partial<State>;Maps raw snapshot data to a state patch object.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | Data | The raw snapshot payload from the sync engine. |
ctx | { store: MobXStoreLike<State>; } | Context object providing access to the target store. |
ctx.store | MobXStoreLike<State> | - |
Returns
Partial<State>
A partial state object whose keys will be assigned to the store.
Default Value
Identity cast — treats data as Partial<State>.
{
mode: "replace";
omitKeys?: ReadonlyArray<keyof State>;
pickKeys?: ReadonlyArray<keyof State>;
runInAction?: (fn) => void;
strict?: boolean;
toState?: (data, ctx) => State;
}mode
mode: "replace";Use 'replace' mode for a full top-level state swap on the observable. Allowed keys not present in the incoming snapshot are deleted; the store reference itself is never replaced.
omitKeys?
optional omitKeys: ReadonlyArray<keyof State>;pickKeys?
optional pickKeys: ReadonlyArray<keyof State>;runInAction()?
optional runInAction: (fn) => void;Parameters
| Parameter | Type |
|---|---|
fn | () => void |
Returns
void
See
MobXSnapshotApplierOptions
strict?
optional strict: boolean;When true, the applier throws if toState returns a non-plain-object value. When false, such values are silently ignored.
Default Value
true
toState()?
optional toState: (data, ctx) => State;Maps raw snapshot data to the full next state.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | Data | The raw snapshot payload from the sync engine. |
ctx | { store: MobXStoreLike<State>; } | Context object providing access to the target store. |
ctx.store | MobXStoreLike<State> | - |
Returns
State
The full next state to apply onto the store.
Default Value
Identity cast — treats data as State.
