Skip to content

@statesync/mobx


@statesync/mobx / MobXSnapshotApplierOptions

Type Alias: MobXSnapshotApplierOptions<State, Data> ​

ts
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 mode is 'patch' (or omitted), toState is expected to return Partial<State>.
  • When mode is 'replace', toState is expected to return the full State.

Type Parameters ​

Type ParameterDescription
State extends Record<string, unknown>The shape of the MobX observable's state object.
DataThe snapshot payload type received from the sync engine. Defaults to State when the snapshot data matches the store shape directly.

Type Declaration ​

ts
{
  mode?: "patch";
  omitKeys?: ReadonlyArray<keyof State>;
  pickKeys?: ReadonlyArray<keyof State>;
  runInAction?: (fn) => void;
  strict?: boolean;
  toState?: (data, ctx) => Partial<State>;
}

mode? ​

ts
optional mode: "patch";

The apply strategy. Defaults to 'patch'.

Default Value ​

'patch'

omitKeys? ​

ts
optional omitKeys: ReadonlyArray<keyof State>;

pickKeys? ​

ts
optional pickKeys: ReadonlyArray<keyof State>;

runInAction()? ​

ts
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 ​

ParameterType
fn() => void

Returns ​

void

Example ​

ts
import { runInAction } from 'mobx';
const applier = createMobXSnapshotApplier(store, { runInAction });

Default Value ​

undefined — mutations are applied without wrapping.

strict? ​

ts
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()? ​

ts
optional toState: (data, ctx) => Partial<State>;

Maps raw snapshot data to a state patch object.

Parameters ​

ParameterTypeDescription
dataDataThe raw snapshot payload from the sync engine.
ctx{ store: MobXStoreLike<State>; }Context object providing access to the target store.
ctx.storeMobXStoreLike<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>.

ts
{
  mode: "replace";
  omitKeys?: ReadonlyArray<keyof State>;
  pickKeys?: ReadonlyArray<keyof State>;
  runInAction?: (fn) => void;
  strict?: boolean;
  toState?: (data, ctx) => State;
}

mode ​

ts
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? ​

ts
optional omitKeys: ReadonlyArray<keyof State>;

pickKeys? ​

ts
optional pickKeys: ReadonlyArray<keyof State>;

runInAction()? ​

ts
optional runInAction: (fn) => void;

Parameters ​

ParameterType
fn() => void

Returns ​

void

See ​

MobXSnapshotApplierOptions

strict? ​

ts
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()? ​

ts
optional toState: (data, ctx) => State;

Maps raw snapshot data to the full next state.

Parameters ​

ParameterTypeDescription
dataDataThe raw snapshot payload from the sync engine.
ctx{ store: MobXStoreLike<State>; }Context object providing access to the target store.
ctx.storeMobXStoreLike<State>-

Returns ​

State

The full next state to apply onto the store.

Default Value ​

Identity cast — treats data as State.

Released under the MIT License.