@homeostate/core 0.0.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +34 -7
  3. package/THIRD_PARTY_NOTICES.md +1 -1
  4. package/dist/apply.d.ts +37 -0
  5. package/dist/apply.d.ts.map +1 -0
  6. package/dist/apply.js +58 -0
  7. package/dist/apply.js.map +1 -0
  8. package/dist/change.d.ts +6 -3
  9. package/dist/change.d.ts.map +1 -1
  10. package/dist/change.js +0 -2
  11. package/dist/change.js.map +1 -1
  12. package/dist/diff.d.ts +2 -3
  13. package/dist/diff.d.ts.map +1 -1
  14. package/dist/diff.js +156 -166
  15. package/dist/diff.js.map +1 -1
  16. package/dist/index.d.ts +11 -3
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +6 -2
  19. package/dist/index.js.map +1 -1
  20. package/dist/memory-backend.d.ts +11 -0
  21. package/dist/memory-backend.d.ts.map +1 -0
  22. package/dist/memory-backend.js +26 -0
  23. package/dist/memory-backend.js.map +1 -0
  24. package/dist/patching.d.ts +6 -16
  25. package/dist/patching.d.ts.map +1 -1
  26. package/dist/patching.js +46 -137
  27. package/dist/patching.js.map +1 -1
  28. package/dist/sync-engine.d.ts +14 -8
  29. package/dist/sync-engine.d.ts.map +1 -1
  30. package/dist/sync-engine.js +67 -93
  31. package/dist/sync-engine.js.map +1 -1
  32. package/dist/types.d.ts +42 -16
  33. package/dist/types.d.ts.map +1 -1
  34. package/dist/types.js +3 -3
  35. package/dist/types.js.map +1 -1
  36. package/package.json +7 -11
  37. package/src/apply.ts +87 -0
  38. package/src/change.ts +23 -0
  39. package/src/diff.ts +229 -0
  40. package/src/index.ts +18 -0
  41. package/src/memory-backend.ts +38 -0
  42. package/src/patching.ts +86 -0
  43. package/src/sync-engine.ts +135 -0
  44. package/src/types.ts +98 -0
  45. package/dist/mapping.d.ts +0 -19
  46. package/dist/mapping.d.ts.map +0 -1
  47. package/dist/mapping.js +0 -45
  48. package/dist/mapping.js.map +0 -1
@@ -1,136 +1,110 @@
1
- import { defaultSyncFilter } from './types.js';
2
- import { patchSharedType, patchState } from './patching.js';
1
+ import { defaultSyncFilter } from "./types.js";
2
+ import { patchState } from "./patching.js";
3
3
  /**
4
- * Creates a sync engine that manages bidirectional synchronization
5
- * between any store (via adapter) and a Yjs document.
4
+ * Creates a sync engine that keeps a store (via its adapter) and a CRDT backend
5
+ * in sync in both directions.
6
6
  *
7
- * This is the core abstraction that makes the sync logic state-manager agnostic.
7
+ * This is the core abstraction that makes the sync logic state-manager and
8
+ * CRDT-library agnostic.
9
+ *
10
+ * On `connect()` the two sides are reconciled per key over the filtered view: a key the
11
+ * backend holds wins over the store's value, and a synced key the backend lacks stays in
12
+ * the store and is seeded into the backend unless `seed` is `'never'`. Afterwards the
13
+ * backend owns the synced document: every write replaces it with the store's filtered
14
+ * state, and a key removed from the backend is removed from the store.
8
15
  *
9
16
  * @example
10
17
  * ```typescript
11
- * const doc = new Y.Doc();
18
+ * const backend = createYjsBackend(new Y.Doc(), 'shared');
12
19
  * const adapter = new ZustandAdapter(store);
13
- * const engine = createSyncEngine(doc, adapter, { name: 'shared' });
20
+ * const engine = createSyncEngine(backend, adapter);
14
21
  * engine.connect();
15
22
  * ```
16
23
  */
17
- export function createSyncEngine(doc, adapter, config) {
18
- const { name, filter = defaultSyncFilter } = config;
19
- // The root Y.Map that the store is written and read from
20
- const yMap = doc.getMap(name);
24
+ export function createSyncEngine(backend, adapter, config = {}) {
25
+ const { filter = defaultSyncFilter, seed = "if-empty" } = config;
21
26
  let connected = false;
22
27
  let storeUnsubscribe = null;
23
- let isUpdatingFromYjs = false;
24
- let isUpdatingFromStore = false;
25
- /**
26
- * Filter state to only include syncable properties
27
- */
28
+ let backendUnsubscribe = null;
29
+ let applyingRemote = false;
28
30
  const filterState = (state) => {
29
- if (typeof state !== 'object' || state === null) {
30
- return state;
31
- }
32
31
  const filtered = {};
33
32
  for (const [key, value] of Object.entries(state)) {
34
- if (filter(key, value)) {
33
+ if (filter(key, value))
35
34
  filtered[key] = value;
36
- }
37
35
  }
38
36
  return filtered;
39
37
  };
38
+ const readBackend = () => {
39
+ const value = backend.read();
40
+ return value !== null && typeof value === "object" ? value : {};
41
+ };
40
42
  /**
41
- * Sync store state to Yjs
43
+ * Builds the next store state from `remote`. With `keepLocalOnly`, a synced key the
44
+ * backend does not hold is left in place instead of being deleted; that is the
45
+ * connect-time reconciliation. Otherwise `remote` is the whole synced state.
42
46
  */
43
- const syncToYjs = () => {
44
- if (isUpdatingFromYjs)
45
- return;
46
- isUpdatingFromStore = true;
47
- try {
48
- const state = adapter.getState();
49
- const filteredState = filterState(state);
50
- doc.transact(() => {
51
- patchSharedType(yMap, filteredState);
52
- });
53
- }
54
- finally {
55
- isUpdatingFromStore = false;
47
+ const mergeStates = (current, remote, keepLocalOnly) => {
48
+ const synced = filterState(current);
49
+ const target = keepLocalOnly ? { ...synced, ...remote } : remote;
50
+ const patched = patchState(synced, target);
51
+ if (patched === synced)
52
+ return current;
53
+ const merged = { ...current, ...patched };
54
+ for (const key of Object.keys(synced)) {
55
+ if (!(key in patched))
56
+ delete merged[key];
56
57
  }
58
+ return merged;
57
59
  };
58
- /**
59
- * Sync Yjs state to store
60
- */
61
- const syncToStore = () => {
62
- if (isUpdatingFromStore)
60
+ const syncToBackend = () => {
61
+ if (applyingRemote)
63
62
  return;
64
- isUpdatingFromYjs = true;
63
+ backend.write(filterState(adapter.getState()));
64
+ };
65
+ const applyRemote = (remote, keepLocalOnly) => {
66
+ applyingRemote = true;
65
67
  try {
66
- const yjsState = yMap.toJSON();
67
- const currentState = adapter.getState();
68
- // Merge Yjs state with current state (preserving functions and non-synced props)
69
- const mergedState = mergeStates(currentState, yjsState);
70
- adapter.setState(mergedState, true);
68
+ const current = adapter.getState();
69
+ const merged = mergeStates(current, remote, keepLocalOnly);
70
+ if (merged !== current)
71
+ adapter.setState(merged);
71
72
  }
72
73
  finally {
73
- isUpdatingFromYjs = false;
74
- }
75
- };
76
- /**
77
- * Merge Yjs state with current store state, preserving non-syncable properties
78
- */
79
- const mergeStates = (current, fromYjs) => {
80
- if (typeof current !== 'object' || current === null) {
81
- return fromYjs;
82
- }
83
- const currentObj = current;
84
- const yjsObj = fromYjs;
85
- // Start with current state (preserves functions, etc.)
86
- const merged = { ...currentObj };
87
- // Apply Yjs state using patching for proper deep merging
88
- const patchedData = patchState({ ...merged }, yjsObj);
89
- // Restore non-syncable properties (like functions)
90
- for (const [key, value] of Object.entries(currentObj)) {
91
- if (!filter(key, value)) {
92
- patchedData[key] = value;
93
- }
74
+ applyingRemote = false;
94
75
  }
95
- return patchedData;
96
76
  };
97
- /**
98
- * Handler for Yjs changes
99
- */
100
- const handleYjsChange = () => {
101
- syncToStore();
77
+ const syncToStore = () => {
78
+ applyRemote(filterState(readBackend()), false);
102
79
  };
103
80
  return {
104
81
  connect: () => {
105
82
  if (connected)
106
83
  return;
107
- // Initialize Yjs with current store state
108
- const initialState = adapter.getInitialState();
109
- const filteredInitial = filterState(initialState);
110
- doc.transact(() => {
111
- patchSharedType(yMap, filteredInitial);
112
- });
113
- // Subscribe to Yjs changes
114
- yMap.observeDeep(handleYjsChange);
115
- // Subscribe to store changes
116
- storeUnsubscribe = adapter.subscribe(() => {
117
- syncToYjs();
118
- });
84
+ const remote = filterState(readBackend());
85
+ const local = filterState(adapter.getState());
86
+ const remoteKeys = new Set(Object.keys(remote));
87
+ const missing = Object.keys(local).filter((key) => !remoteKeys.has(key));
88
+ if (seed === "if-empty" && missing.length > 0) {
89
+ const seeded = { ...remote };
90
+ for (const key of missing)
91
+ seeded[key] = local[key];
92
+ backend.write(seeded);
93
+ }
94
+ applyRemote(remote, true);
95
+ backendUnsubscribe = backend.subscribe(syncToStore);
96
+ storeUnsubscribe = adapter.subscribe(syncToBackend);
119
97
  connected = true;
120
98
  },
121
99
  disconnect: () => {
122
100
  if (!connected)
123
101
  return;
124
- // Unsubscribe from Yjs
125
- yMap.unobserveDeep(handleYjsChange);
126
- // Unsubscribe from store
127
- if (storeUnsubscribe) {
128
- storeUnsubscribe();
129
- storeUnsubscribe = null;
130
- }
102
+ backendUnsubscribe?.();
103
+ backendUnsubscribe = null;
104
+ storeUnsubscribe?.();
105
+ storeUnsubscribe = null;
131
106
  connected = false;
132
107
  },
133
- getYMap: () => yMap,
134
108
  isConnected: () => connected,
135
109
  };
136
110
  }
@@ -1 +1 @@
1
- {"version":3,"file":"sync-engine.js","sourceRoot":"","sources":["../src/sync-engine.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE5D;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,gBAAgB,CAC9B,GAAU,EACV,OAAwB,EACxB,MAAwB;IAExB,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,iBAAiB,EAAE,GAAG,MAAM,CAAC;IAEpD,yDAAyD;IACzD,MAAM,IAAI,GAAmB,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAE9C,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,gBAAgB,GAAuB,IAAI,CAAC;IAChD,IAAI,iBAAiB,GAAG,KAAK,CAAC;IAC9B,IAAI,mBAAmB,GAAG,KAAK,CAAC;IAEhC;;OAEG;IACH,MAAM,WAAW,GAAG,CAAC,KAAQ,EAAc,EAAE;QAC3C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YAChD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,QAAQ,GAA4B,EAAE,CAAC;QAC7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAgC,CAAC,EAAE,CAAC;YAC5E,IAAI,MAAM,CAAC,GAAG,EAA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W,CAAC,CAAC;YACpD,gBAAgB,GAAG,OAAO,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;YACpD,SAAS,GAAG,IAAI,CAAC;QACnB,CAAC;QAED,UAAU,EAAE,GAAS,EAAE;YACrB,IAAI,CAAC,SAAS;gBAAE,OAAO;YAEvB,kBAAkB,EAAE,EAAE,CAAC;YACvB,kBAAkB,GAAG,IAAI,CAAC;YAC1B,gBAAgB,EAAE,EAAE,CAAC;YACrB,gBAAgB,GAAG,IAAI,CAAC;YACxB,SAAS,GAAG,KAAK,CAAC;QACpB,CAAC;QAED,WAAW,EAAE,GAAY,EAAE,CAAC,SAAS;KACtC,CAAC;AACJ,CAAC"}
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
- * Core types for the state-manager agnostic Yjs sync engine.
3
- * These interfaces allow any store manager to be connected to Yjs for P2P sync.
2
+ * Core types for the state-manager and CRDT-backend agnostic sync engine.
3
+ * These interfaces let any store be kept in sync through any replicated backend.
4
4
  */
5
5
  /**
6
6
  * Unsubscribe function returned by subscribe
@@ -9,47 +9,73 @@ export type Unsubscribe = () => void;
9
9
  /**
10
10
  * Adapter interface that bridges a specific state manager with the sync engine.
11
11
  * Implement this interface to add support for any state manager (Zustand, Redux, MobX, etc.)
12
+ *
13
+ * State must be plain JSON: objects, arrays, strings, numbers, booleans, and null.
14
+ * Functions are dropped by the default filter; other values such as Date, Map, or Set
15
+ * are neither diffed nor synced.
12
16
  */
13
- export interface StoreAdapter<S> {
17
+ export interface StoreAdapter<S extends object> {
14
18
  /** Get the current state from the store */
15
19
  getState: () => S;
16
20
  /**
17
- * Set state in the store.
18
- * @param state - New state to set
19
- * @param fromSync - If true, this update came from Yjs sync and should not trigger sync back
21
+ * Replace the store state. Called only for changes coming from the backend; the
22
+ * engine ignores store notifications raised while it runs, so the adapter needs no
23
+ * echo suppression of its own.
20
24
  */
21
- setState: (state: S, fromSync?: boolean) => void;
25
+ setState: (state: S) => void;
22
26
  /**
23
- * Subscribe to store changes that should trigger Yjs sync.
27
+ * Subscribe to store changes that should be written to the backend.
24
28
  * @returns Unsubscribe function
25
29
  */
26
30
  subscribe: (onStoreChange: () => void) => Unsubscribe;
27
- /** Get the initial state of the store */
28
- getInitialState: () => S;
29
31
  }
32
+ /**
33
+ * Backend that holds the synced subtree in a CRDT or any other replicated store.
34
+ * The engine only ever exchanges plain JSON with it.
35
+ */
36
+ export interface CrdtBackend {
37
+ /** Plain JSON snapshot of the synced subtree. Must not alias backend internals. */
38
+ read: () => unknown;
39
+ /**
40
+ * Make the backend equal to `next` in one atomic transaction.
41
+ * The backend decides the granularity of the operations; `getChanges` is exported for that.
42
+ */
43
+ write: (next: unknown) => void;
44
+ /**
45
+ * Notify about changes that did not come through this backend's own `write`.
46
+ * @returns Unsubscribe function
47
+ */
48
+ subscribe: (onRemoteChange: () => void) => Unsubscribe;
49
+ }
50
+ /**
51
+ * Whether `connect()` may write the store's synced keys that the backend does not hold.
52
+ * Reconciliation is per key over the filtered view: a key the backend holds always wins
53
+ * over the store's value, a key only the store holds always stays in the store.
54
+ * - `'if-empty'`: seed the keys the backend lacks, in one write (default)
55
+ * - `'never'`: leave seeding to the caller; the keys are written on the next local change
56
+ */
57
+ export type SeedStrategy = "if-empty" | "never";
30
58
  /**
31
59
  * Configuration options for the sync engine
32
60
  */
33
61
  export interface SyncEngineConfig {
34
- /** Name of the Y.Map in the Y.Doc to store the state */
35
- name: string;
36
62
  /**
37
- * Filter function to determine which state keys should be synced.
63
+ * Filter function to determine which state keys should be synced, in both directions.
38
64
  * Return true to sync the key, false to exclude it.
39
65
  * By default, functions are excluded from sync.
40
66
  */
41
67
  filter?: (key: string, value: unknown) => boolean;
68
+ /** Seeding strategy used by `connect()`; defaults to `'if-empty'` */
69
+ seed?: SeedStrategy;
42
70
  }
43
71
  /**
44
- * The sync engine interface - manages bidirectional sync between store and Yjs
72
+ * The sync engine interface - manages bidirectional sync between a store and a backend
45
73
  */
46
74
  export interface SyncEngine {
47
75
  /** Start synchronization */
48
76
  connect: () => void;
49
77
  /** Stop synchronization and cleanup */
50
78
  disconnect: () => void;
51
- /** Get the underlying Y.Map */
52
- getYMap: () => unknown;
53
79
  /** Check if engine is connected */
54
80
  isConnected: () => boolean;
55
81
  }
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AAErC;;;GAGG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC,CAAC;IAElB;;;;OAIG;IACH,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IAEjD;;;OAGG;IACH,SAAS,EAAE,CAAC,aAAa,EAAE,MAAM,IAAI,KAAK,WAAW,CAAC;IAEtD,yCAAyC;IACzC,eAAe,EAAE,MAAM,CAAC,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,wDAAwD;IACxD,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;CACnD;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,4BAA4B;IAC5B,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,uCAAuC;IACvC,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,+BAA+B;IAC/B,OAAO,EAAE,MAAM,OAAO,CAAC;IACvB,mCAAmC;IACnC,WAAW,EAAE,MAAM,OAAO,CAAC;CAC5B;AAED;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,MAAM,MAAM,EAAE,OAAO,OAAO,KAAG,OAEhE,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceR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,11 @@
1
1
  /**
2
- * Core types for the state-manager agnostic Yjs sync engine.
3
- * These interfaces allow any store manager to be connected to Yjs for P2P sync.
2
+ * Core types for the state-manager and CRDT-backend agnostic sync engine.
3
+ * These interfaces let any store be kept in sync through any replicated backend.
4
4
  */
5
5
  /**
6
6
  * Default filter that excludes functions from sync
7
7
  */
8
8
  export const defaultSyncFilter = (_key, value) => {
9
- return typeof value !== 'function';
9
+ return typeof value !== "function";
10
10
  };
11
11
  //# sourceMappingURL=types.js.map
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA4DH;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAE,KAAc,EAAW,EAAE;IACzE,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACrC,CAAC,CAAC"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAyFH;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAE,KAAc,EAAW,EAAE;IACzE,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACrC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@homeostate/core",
3
- "version": "0.0.0",
4
- "description": "State-manager agnostic sync engine between a store adapter and a Yjs document",
3
+ "version": "0.1.2",
4
+ "description": "State-manager and CRDT-backend agnostic sync engine between a store adapter and a CrdtBackend",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "repository": {
@@ -9,12 +9,11 @@
9
9
  "url": "git+https://github.com/mixedrays/homeostate.git",
10
10
  "directory": "packages/core"
11
11
  },
12
- "homepage": "https://github.com/mixedrays/homeostate#readme",
12
+ "homepage": "https://homeostate.pages.dev",
13
13
  "bugs": {
14
14
  "url": "https://github.com/mixedrays/homeostate/issues"
15
15
  },
16
16
  "keywords": [
17
- "yjs",
18
17
  "crdt",
19
18
  "sync",
20
19
  "state-management",
@@ -30,19 +29,16 @@
30
29
  },
31
30
  "files": [
32
31
  "dist",
32
+ "src",
33
+ "!src/**/__tests__",
33
34
  "README.md",
34
35
  "LICENSE",
35
- "THIRD_PARTY_NOTICES.md"
36
+ "THIRD_PARTY_NOTICES.md",
37
+ "CHANGELOG.md"
36
38
  ],
37
39
  "publishConfig": {
38
40
  "access": "public"
39
41
  },
40
- "peerDependencies": {
41
- "yjs": "^13.6.0"
42
- },
43
- "devDependencies": {
44
- "yjs": "^13.6.23"
45
- },
46
42
  "scripts": {
47
43
  "build": "tsc -b"
48
44
  }
package/src/apply.ts ADDED
@@ -0,0 +1,87 @@
1
+ import { ChangeType, type Change } from "./change.js";
2
+
3
+ type Plain = Record<string, unknown>;
4
+
5
+ /**
6
+ * How one store writes into its containers. `applyChanges` decides *what* to write; an
7
+ * implementation of this decides *how*, so the same edit script drives a MobX observable
8
+ * tree, a Valtio proxy, or a plain object.
9
+ *
10
+ * Every method receives a container that already lives in the store, never a copy.
11
+ */
12
+ export interface ApplyOps {
13
+ /** Assign `value` at `key`, creating the property if the target is a record. */
14
+ set(target: object, key: string | number, value: unknown): void;
15
+ /** Remove the record property `key`. */
16
+ remove(target: object, key: string): void;
17
+ /** `Array.prototype.splice`: drop `deleteCount` elements at `index`, then add `inserted`. */
18
+ splice(
19
+ target: unknown[],
20
+ index: number,
21
+ deleteCount: number,
22
+ inserted: unknown[],
23
+ ): void;
24
+ }
25
+
26
+ /**
27
+ * Revises `value` by the character-level edit script `changes`, which is what `getChanges`
28
+ * returns for two strings. Each index addresses the string as revised by the steps before it.
29
+ */
30
+ export const applyStringChanges = (value: string, changes: Change[]): string =>
31
+ changes.reduce((revised, [type, index, inserted]) => {
32
+ const at = index as number;
33
+ if (type === ChangeType.INSERT)
34
+ return revised.slice(0, at) + (inserted as string) + revised.slice(at);
35
+ if (type === ChangeType.DELETE)
36
+ return revised.slice(0, at) + revised.slice(at + 1);
37
+ return revised;
38
+ }, value);
39
+
40
+ /**
41
+ * Applies `changes` to `target` in place, mutating only the paths the edit script names.
42
+ * Everything else keeps its identity, which is what fine-grained stores react to.
43
+ *
44
+ * `changes` must be `getChanges(before, after)` where `before` describes the shape `target`
45
+ * currently holds — an adapter gets that by diffing against the snapshot it last handed out.
46
+ * A `PENDING` step carries no replacement value, so it can only be followed by recursing into
47
+ * the container already there; a target that does not mirror `before` leaves such a step
48
+ * unapplied rather than writing something wrong.
49
+ *
50
+ * @param target The container to mutate — a record, or an array for a positional edit script.
51
+ * @param changes The edit script to apply, in order.
52
+ * @param ops How this store writes; see {@link ApplyOps}.
53
+ */
54
+ export const applyChanges = (
55
+ target: object,
56
+ changes: Change[],
57
+ ops: ApplyOps,
58
+ ): void => {
59
+ const array = Array.isArray(target) ? (target as unknown[]) : null;
60
+
61
+ for (const [type, key, value] of changes) {
62
+ switch (type) {
63
+ case ChangeType.PENDING: {
64
+ const child = (target as Plain)[key as string];
65
+ if (typeof child === "string")
66
+ ops.set(target, key, applyStringChanges(child, value as Change[]));
67
+ else if (child !== null && typeof child === "object")
68
+ applyChanges(child, value as Change[], ops);
69
+ break;
70
+ }
71
+
72
+ case ChangeType.DELETE:
73
+ if (array) ops.splice(array, key as number, 1, []);
74
+ else ops.remove(target, key as string);
75
+ break;
76
+
77
+ case ChangeType.INSERT:
78
+ if (array) ops.splice(array, key as number, 0, [value]);
79
+ else ops.set(target, key, value);
80
+ break;
81
+
82
+ case ChangeType.UPDATE:
83
+ ops.set(target, key, value);
84
+ break;
85
+ }
86
+ }
87
+ };
package/src/change.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Describes the change that needs to be made.
3
+ */
4
+ export enum ChangeType {
5
+ /** A value was inserted. */
6
+ INSERT = "insert",
7
+ /** A value was replaced. */
8
+ UPDATE = "update",
9
+ /** A value was deleted. */
10
+ DELETE = "delete",
11
+ /** The value requires a recursive diff to identify further changes. */
12
+ PENDING = "pending",
13
+ }
14
+
15
+ /**
16
+ * One step in turning a container into another: `[type, key, value]`.
17
+ *
18
+ * Steps apply in order. A numeric key addresses the array or string as revised by the
19
+ * steps before it, so appliers never sort or clamp. `value` is the inserted or replacing
20
+ * value (a string insert may carry several characters), `undefined` for a delete, and the
21
+ * nested `Change[]` for a pending entry.
22
+ */
23
+ export type Change = [ChangeType, string | number, unknown];