Skip to content

@statesync/mobx


@statesync/mobx / createMobXSnapshotApplier

Function: createMobXSnapshotApplier() ​

ts
function createMobXSnapshotApplier<State, Data>(store, options): SnapshotApplier<Data>;

Defined in: mobx.ts:264

Creates a SnapshotApplier that applies incoming snapshots into a MobX observable store by mutating it in place.

This is a framework adapter: it only focuses on how to apply a snapshot into a concrete MobX state container. It does not fetch snapshots and does not subscribe to invalidation events — those concerns belong to the sync engine (@statesync/core).

How state updates propagate:

  1. The sync engine receives an invalidation event and fetches a new snapshot from the server.
  2. The engine calls applier.apply(envelope) with the snapshot data.
  3. This adapter maps the snapshot data to a state patch (via toState), filters keys (via pickKeys / omitKeys), and mutates the MobX observable in place by assigning or deleting individual properties inside a single runInAction call (when provided).
  4. MobX's reactivity system automatically notifies all subscribers (autorun, reaction, observer()) of the changed properties.

Framework-specific note: The store reference is never replaced. All mutations are applied directly to the existing observable object so that autorun, reaction, and observer() consumers continue to work without any re-wiring.

Type Parameters ​

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

Parameters ​

ParameterTypeDescription
storeStateThe MobX observable (or any object satisfying MobXStoreLike) to apply snapshots into.
optionsMobXSnapshotApplierOptions<State, Data>Configuration for apply mode, key filtering, data mapping, runInAction wrapping, and strict validation.

Returns ​

SnapshotApplier<Data>

A SnapshotApplier whose apply method writes snapshot data into the MobX store.

Throws ​

When strict is true (the default) and toState returns a non-plain-object value.

Examples ​

ts
import { makeAutoObservable, runInAction } from 'mobx';
import { createMobXSnapshotApplier } from '@statesync/mobx';
import { createRevisionSync } from '@statesync/core';

class ProfileStore {
  name = '';
  email = '';
  constructor() { makeAutoObservable(this); }
}

const store = new ProfileStore();
const applier = createMobXSnapshotApplier(store, { runInAction });

const sync = createRevisionSync({
  topic: 'user-profile',
  subscriber: mySubscriber,
  provider: mySnapshotProvider,
  applier,
});
ts
const applier = createMobXSnapshotApplier(store, {
  mode: 'replace',
  runInAction,
  omitKeys: ['isMenuOpen'],
});

Released under the MIT License.