@statesync/mobx / createMobXSnapshotApplier
Function: createMobXSnapshotApplier()
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:
- The sync engine receives an invalidation event and fetches a new snapshot from the server.
- The engine calls
applier.apply(envelope)with the snapshot data. - This adapter maps the snapshot data to a state patch (via
toState), filters keys (viapickKeys/omitKeys), and mutates the MobX observable in place by assigning or deleting individual properties inside a singlerunInActioncall (when provided). - 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 Parameter | Default type | Description |
|---|---|---|
State extends Record<string, unknown> | - | The shape of the MobX observable's state object. |
Data | State | The snapshot payload type received from the sync engine. Defaults to State when the snapshot data matches the store shape. |
Parameters
| Parameter | Type | Description |
|---|---|---|
store | State | The MobX observable (or any object satisfying MobXStoreLike) to apply snapshots into. |
options | MobXSnapshotApplierOptions<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
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,
});const applier = createMobXSnapshotApplier(store, {
mode: 'replace',
runInAction,
omitKeys: ['isMenuOpen'],
});