@liveblocks/core 3.16.0-flow1 → 3.16.0-flow2

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.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "3.16.0-flow1";
9
+ var PKG_VERSION = "3.16.0-flow2";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -6302,7 +6302,6 @@ var AbstractCrdt = class {
6302
6302
  }
6303
6303
  /**
6304
6304
  * @internal
6305
- *
6306
6305
  * Return an snapshot of this Live tree for use in DevTools.
6307
6306
  */
6308
6307
  toTreeNode(key) {
@@ -6312,6 +6311,14 @@ var AbstractCrdt = class {
6312
6311
  }
6313
6312
  return this.#cachedTreeNode;
6314
6313
  }
6314
+ /**
6315
+ * @private
6316
+ * Returns true if the cached immutable snapshot exists and is
6317
+ * reference-equal to the given value. Does not trigger a recompute.
6318
+ */
6319
+ immutableIs(value) {
6320
+ return this.#cachedImmutable !== void 0 && this.#cachedImmutable === value;
6321
+ }
6315
6322
  /**
6316
6323
  * Return an immutable snapshot of this Live node and its children.
6317
6324
  */
@@ -7859,271 +7866,675 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
7859
7866
  }
7860
7867
  };
7861
7868
 
7862
- // src/crdts/LiveObject.ts
7863
- var MAX_LIVE_OBJECT_SIZE = 128 * 1024;
7864
- var LiveObject = class _LiveObject extends AbstractCrdt {
7865
- #map;
7866
- /**
7867
- * Tracks unacknowledged local changes per property to preserve optimistic
7868
- * updates. Maps property keys to their pending operation IDs.
7869
- *
7870
- * INVARIANT: Only locally-generated opIds are ever stored here. Remote opIds
7871
- * are only compared against (to detect ACKs), never stored.
7872
- *
7873
- * When a local change is made, the opId is stored here. When a remote op
7874
- * arrives for the same key:
7875
- * - If no entry exists → apply remote op
7876
- * - If opId matches → it's an ACK, clear the entry
7877
- * - If opId differs → ignore remote op to preserve optimistic update
7878
- */
7879
- #unackedOpsByKey;
7880
- /**
7881
- * Enable or disable detection of too large LiveObjects.
7882
- * When enabled, throws an error if LiveObject static data exceeds 128KB, which
7883
- * is the maximum value the server will be able to accept.
7884
- * By default, this behavior is disabled to avoid the runtime performance
7885
- * overhead on every LiveObject.set() or LiveObject.update() call.
7886
- *
7887
- * @experimental
7888
- */
7889
- static detectLargeObjects = false;
7890
- static #buildRootAndParentToChildren(nodes) {
7891
- const parentToChildren = /* @__PURE__ */ new Map();
7892
- let root = null;
7893
- for (const node of nodes) {
7894
- if (isRootStorageNode(node)) {
7895
- root = node[1];
7869
+ // src/lib/Json.ts
7870
+ function isJsonScalar(data) {
7871
+ return data === null || typeof data === "string" || typeof data === "number" || typeof data === "boolean";
7872
+ }
7873
+ function isJsonArray(data) {
7874
+ return Array.isArray(data);
7875
+ }
7876
+ function isJsonObject(data) {
7877
+ return !isJsonScalar(data) && !isJsonArray(data);
7878
+ }
7879
+
7880
+ // src/immutable.ts
7881
+ function lsonObjectToJson(obj) {
7882
+ const result = {};
7883
+ for (const key in obj) {
7884
+ const val = obj[key];
7885
+ if (val !== void 0) {
7886
+ result[key] = lsonToJson(val);
7887
+ }
7888
+ }
7889
+ return result;
7890
+ }
7891
+ function liveObjectToJson(liveObject) {
7892
+ return lsonObjectToJson(liveObject.toObject());
7893
+ }
7894
+ function liveMapToJson(map) {
7895
+ const result = {};
7896
+ for (const [key, value] of map.entries()) {
7897
+ result[key] = lsonToJson(value);
7898
+ }
7899
+ return result;
7900
+ }
7901
+ function lsonListToJson(value) {
7902
+ return value.map(lsonToJson);
7903
+ }
7904
+ function liveListToJson(value) {
7905
+ return lsonListToJson(value.toArray());
7906
+ }
7907
+ function lsonToJson(value) {
7908
+ if (value instanceof LiveObject) {
7909
+ return liveObjectToJson(value);
7910
+ } else if (value instanceof LiveList) {
7911
+ return liveListToJson(value);
7912
+ } else if (value instanceof LiveMap) {
7913
+ return liveMapToJson(value);
7914
+ } else if (value instanceof LiveRegister) {
7915
+ return value.data;
7916
+ }
7917
+ if (Array.isArray(value)) {
7918
+ return lsonListToJson(value);
7919
+ } else if (isPlainObject(value)) {
7920
+ return lsonObjectToJson(value);
7921
+ }
7922
+ return value;
7923
+ }
7924
+ function deepLiveify(value, config) {
7925
+ if (Array.isArray(value)) {
7926
+ return new LiveList(value.map((v) => deepLiveify(v, config)));
7927
+ } else if (isPlainObject(value)) {
7928
+ const init = {};
7929
+ const locals = {};
7930
+ for (const key in value) {
7931
+ const val = value[key];
7932
+ if (val === void 0) {
7933
+ continue;
7934
+ }
7935
+ const subConfig = isPlainObject(config) ? config[key] : config;
7936
+ if (subConfig === false) {
7937
+ locals[key] = val;
7938
+ } else if (subConfig === "atomic") {
7939
+ init[key] = val;
7896
7940
  } else {
7897
- const crdt = node[1];
7898
- const children = parentToChildren.get(crdt.parentId);
7899
- if (children !== void 0) {
7900
- children.push(node);
7901
- } else {
7902
- parentToChildren.set(crdt.parentId, [node]);
7903
- }
7941
+ init[key] = deepLiveify(val, subConfig);
7904
7942
  }
7905
7943
  }
7906
- if (root === null) {
7907
- throw new Error("Root can't be null");
7944
+ const lo = new LiveObject(init);
7945
+ for (const key in locals) {
7946
+ lo.setLocal(key, locals[key]);
7908
7947
  }
7909
- return [root, parentToChildren];
7948
+ return lo;
7949
+ } else {
7950
+ return value;
7910
7951
  }
7911
- /** @private Do not use this API directly */
7912
- static _fromItems(nodes, pool) {
7913
- const [root, parentToChildren] = _LiveObject.#buildRootAndParentToChildren(nodes);
7914
- return _LiveObject._deserialize(
7915
- ["root", root],
7916
- parentToChildren,
7917
- pool
7918
- );
7952
+ }
7953
+ function deepLiveifyObject(obj, config) {
7954
+ return deepLiveify(obj, config);
7955
+ }
7956
+ function reconcile(live, json, config) {
7957
+ if (isLiveObject(live) && isPlainObject(json)) {
7958
+ return reconcileLiveObject(live, json, config);
7959
+ } else if (isLiveList(live) && Array.isArray(json)) {
7960
+ return reconcileLiveList(live, json, config);
7961
+ } else if (isLiveMap(live) && isPlainObject(json)) {
7962
+ return reconcileLiveMap(live, config);
7963
+ } else {
7964
+ return deepLiveify(json, config);
7919
7965
  }
7920
- constructor(obj = {}) {
7921
- super();
7922
- this.#unackedOpsByKey = /* @__PURE__ */ new Map();
7923
- const o = compactObject(obj);
7924
- for (const key of Object.keys(o)) {
7925
- const value = o[key];
7926
- if (isLiveNode(value)) {
7927
- value._setParentLink(this, key);
7966
+ }
7967
+ function reconcileLiveMap(_liveMap, _config) {
7968
+ throw new Error("Reconciling a LiveMap is not supported yet");
7969
+ }
7970
+ function reconcileLiveObject(liveObj, jsonObj, config) {
7971
+ const currentKeys = liveObj.keys();
7972
+ for (const key in jsonObj) {
7973
+ currentKeys.delete(key);
7974
+ const newVal = jsonObj[key];
7975
+ if (newVal === void 0) {
7976
+ liveObj.delete(key);
7977
+ continue;
7978
+ }
7979
+ const subConfig = isPlainObject(config) ? config[key] : config;
7980
+ if (subConfig === false) {
7981
+ liveObj.setLocal(key, newVal);
7982
+ } else if (subConfig === "atomic") {
7983
+ const curVal = liveObj.get(key);
7984
+ if (curVal !== newVal) {
7985
+ liveObj.set(key, newVal);
7986
+ }
7987
+ } else {
7988
+ const curVal = liveObj.get(key);
7989
+ if (curVal === void 0) {
7990
+ liveObj.set(key, deepLiveify(newVal, subConfig));
7991
+ } else if (isLiveStructure(curVal)) {
7992
+ const next = reconcile(curVal, newVal, subConfig);
7993
+ if (next !== curVal) {
7994
+ liveObj.set(key, next);
7995
+ }
7996
+ } else if (curVal !== newVal) {
7997
+ liveObj.set(key, deepLiveify(newVal, subConfig));
7928
7998
  }
7929
7999
  }
7930
- this.#map = new Map(Object.entries(o));
7931
8000
  }
7932
- /** @internal */
7933
- _toOps(parentId, parentKey) {
7934
- if (this._id === void 0) {
7935
- throw new Error("Cannot serialize item is not attached");
7936
- }
7937
- const ops = [];
7938
- const op = {
7939
- type: OpCode.CREATE_OBJECT,
7940
- id: this._id,
7941
- parentId,
7942
- parentKey,
7943
- data: {}
7944
- };
7945
- ops.push(op);
7946
- for (const [key, value] of this.#map) {
7947
- if (isLiveNode(value)) {
7948
- for (const childOp of value._toOps(this._id, key)) {
7949
- ops.push(childOp);
7950
- }
7951
- } else {
7952
- op.data[key] = value;
8001
+ for (const key of currentKeys) {
8002
+ liveObj.delete(key);
8003
+ }
8004
+ return liveObj;
8005
+ }
8006
+ function reconcileLiveList(liveList, jsonArr, config) {
8007
+ const curLen = liveList.length;
8008
+ const newLen = jsonArr.length;
8009
+ for (let i = 0; i < Math.min(curLen, newLen); i++) {
8010
+ const curVal = liveList.get(i);
8011
+ const newVal = jsonArr[i];
8012
+ if (isLiveStructure(curVal)) {
8013
+ const next = reconcile(curVal, newVal, config);
8014
+ if (next !== curVal) {
8015
+ liveList.set(i, next);
7953
8016
  }
8017
+ } else if (curVal !== newVal) {
8018
+ liveList.set(i, deepLiveify(newVal, config));
7954
8019
  }
7955
- return ops;
7956
8020
  }
7957
- /** @internal */
7958
- static _deserialize([id, item], parentToChildren, pool) {
7959
- const liveObj = new _LiveObject(item.data);
7960
- liveObj._attach(id, pool);
7961
- return this._deserializeChildren(liveObj, parentToChildren, pool);
8021
+ for (let i = curLen; i < newLen; i++) {
8022
+ liveList.push(deepLiveify(jsonArr[i], config));
7962
8023
  }
7963
- /** @internal */
7964
- static _deserializeChildren(liveObj, parentToChildren, pool) {
7965
- const children = parentToChildren.get(nn(liveObj._id));
7966
- if (children === void 0) {
7967
- return liveObj;
8024
+ for (let i = curLen - 1; i >= newLen; i--) {
8025
+ liveList.delete(i);
8026
+ }
8027
+ return liveList;
8028
+ }
8029
+ function legacy_patchLiveList(liveList, prev, next) {
8030
+ let i = 0;
8031
+ let prevEnd = prev.length - 1;
8032
+ let nextEnd = next.length - 1;
8033
+ let prevNode = prev[0];
8034
+ let nextNode = next[0];
8035
+ outer: {
8036
+ while (prevNode === nextNode) {
8037
+ ++i;
8038
+ if (i > prevEnd || i > nextEnd) {
8039
+ break outer;
8040
+ }
8041
+ prevNode = prev[i];
8042
+ nextNode = next[i];
7968
8043
  }
7969
- for (const node of children) {
7970
- const child = deserializeToLson(node, parentToChildren, pool);
7971
- const crdt = node[1];
7972
- if (isLiveStructure(child)) {
7973
- child._setParentLink(liveObj, crdt.parentKey);
8044
+ prevNode = prev[prevEnd];
8045
+ nextNode = next[nextEnd];
8046
+ while (prevNode === nextNode) {
8047
+ prevEnd--;
8048
+ nextEnd--;
8049
+ if (i > prevEnd || i > nextEnd) {
8050
+ break outer;
7974
8051
  }
7975
- liveObj.#map.set(crdt.parentKey, child);
7976
- liveObj.invalidate();
8052
+ prevNode = prev[prevEnd];
8053
+ nextNode = next[nextEnd];
7977
8054
  }
7978
- return liveObj;
7979
8055
  }
7980
- /** @internal */
7981
- _attach(id, pool) {
7982
- super._attach(id, pool);
7983
- for (const [_key, value] of this.#map) {
7984
- if (isLiveNode(value)) {
7985
- value._attach(pool.generateId(), pool);
8056
+ if (i > prevEnd) {
8057
+ if (i <= nextEnd) {
8058
+ while (i <= nextEnd) {
8059
+ liveList.insert(deepLiveify(next[i]), i);
8060
+ i++;
7986
8061
  }
7987
8062
  }
7988
- }
7989
- /** @internal */
7990
- _attachChild(op, source) {
7991
- if (this._pool === void 0) {
7992
- throw new Error("Can't attach child if managed pool is not present");
8063
+ } else if (i > nextEnd) {
8064
+ let localI = i;
8065
+ while (localI <= prevEnd) {
8066
+ liveList.delete(i);
8067
+ localI++;
7993
8068
  }
7994
- const { id, opId, parentKey: key } = op;
7995
- const child = creationOpToLson(op);
7996
- if (this._pool.getNode(id) !== void 0) {
7997
- if (this.#unackedOpsByKey.get(key) === opId) {
7998
- this.#unackedOpsByKey.delete(key);
8069
+ } else {
8070
+ while (i <= prevEnd && i <= nextEnd) {
8071
+ prevNode = prev[i];
8072
+ nextNode = next[i];
8073
+ const liveListNode = liveList.get(i);
8074
+ if (isLiveObject(liveListNode) && isPlainObject(prevNode) && isPlainObject(nextNode)) {
8075
+ legacy_patchLiveObject(liveListNode, prevNode, nextNode);
8076
+ } else {
8077
+ liveList.set(i, deepLiveify(nextNode));
7999
8078
  }
8000
- return { modified: false };
8079
+ i++;
8001
8080
  }
8002
- if (source === 0 /* LOCAL */) {
8003
- this.#unackedOpsByKey.set(key, nn(opId));
8004
- } else if (this.#unackedOpsByKey.get(key) === void 0) {
8005
- } else if (this.#unackedOpsByKey.get(key) === opId) {
8006
- this.#unackedOpsByKey.delete(key);
8007
- return { modified: false };
8008
- } else {
8009
- return { modified: false };
8081
+ while (i <= nextEnd) {
8082
+ liveList.insert(deepLiveify(next[i]), i);
8083
+ i++;
8010
8084
  }
8011
- const thisId = nn(this._id);
8012
- const previousValue = this.#map.get(key);
8013
- let reverse;
8014
- if (isLiveNode(previousValue)) {
8015
- reverse = previousValue._toOps(thisId, key);
8016
- previousValue._detach();
8017
- } else if (previousValue === void 0) {
8018
- reverse = [{ type: OpCode.DELETE_OBJECT_KEY, id: thisId, key }];
8019
- } else {
8020
- reverse = [
8021
- {
8022
- type: OpCode.UPDATE_OBJECT,
8023
- id: thisId,
8024
- data: { [key]: previousValue }
8025
- }
8026
- ];
8027
- }
8028
- this.#map.set(key, child);
8029
- this.invalidate();
8030
- if (isLiveStructure(child)) {
8031
- child._setParentLink(this, key);
8032
- child._attach(id, this._pool);
8085
+ let localI = i;
8086
+ while (localI <= prevEnd) {
8087
+ liveList.delete(i);
8088
+ localI++;
8033
8089
  }
8034
- return {
8035
- reverse,
8036
- modified: {
8037
- node: this,
8038
- type: "LiveObject",
8039
- updates: { [key]: { type: "update" } }
8040
- }
8041
- };
8042
8090
  }
8043
- /** @internal */
8044
- _detachChild(child) {
8045
- if (child) {
8046
- const id = nn(this._id);
8047
- const parentKey = nn(child._parentKey);
8048
- const reverse = child._toOps(id, parentKey);
8049
- for (const [key, value] of this.#map) {
8050
- if (value === child) {
8051
- this.#map.delete(key);
8052
- this.invalidate();
8053
- }
8054
- }
8055
- child._detach();
8056
- const storageUpdate = {
8057
- node: this,
8058
- type: "LiveObject",
8059
- updates: {
8060
- [parentKey]: { type: "delete" }
8061
- }
8062
- };
8063
- return { modified: storageUpdate, reverse };
8091
+ }
8092
+ function legacy_patchLiveObjectKey(liveObject, key, prev, next) {
8093
+ if (process.env.NODE_ENV !== "production") {
8094
+ const nonSerializableValue = findNonSerializableValue(next);
8095
+ if (nonSerializableValue) {
8096
+ error2(
8097
+ `New state path: '${nonSerializableValue.path}' value: '${String(
8098
+ nonSerializableValue.value
8099
+ )}' is not serializable.
8100
+ Only serializable value can be synced with Liveblocks.`
8101
+ );
8102
+ return;
8064
8103
  }
8065
- return { modified: false };
8066
8104
  }
8067
- /** @internal */
8068
- _detach() {
8069
- super._detach();
8070
- for (const value of this.#map.values()) {
8071
- if (isLiveNode(value)) {
8072
- value._detach();
8073
- }
8105
+ const value = liveObject.get(key);
8106
+ if (next === void 0) {
8107
+ liveObject.delete(key);
8108
+ } else if (value === void 0) {
8109
+ liveObject.set(key, deepLiveify(next));
8110
+ } else if (prev === next) {
8111
+ return;
8112
+ } else if (isLiveList(value) && Array.isArray(prev) && Array.isArray(next)) {
8113
+ legacy_patchLiveList(value, prev, next);
8114
+ } else if (isLiveObject(value) && isPlainObject(prev) && isPlainObject(next)) {
8115
+ legacy_patchLiveObject(value, prev, next);
8116
+ } else {
8117
+ liveObject.set(key, deepLiveify(next));
8118
+ }
8119
+ }
8120
+ function legacy_patchLiveObject(root, prev, next) {
8121
+ const updates = {};
8122
+ for (const key in next) {
8123
+ legacy_patchLiveObjectKey(root, key, prev[key], next[key]);
8124
+ }
8125
+ for (const key in prev) {
8126
+ if (next[key] === void 0) {
8127
+ root.delete(key);
8074
8128
  }
8075
8129
  }
8076
- /** @internal */
8077
- _apply(op, isLocal) {
8078
- if (op.type === OpCode.UPDATE_OBJECT) {
8079
- return this.#applyUpdate(op, isLocal);
8080
- } else if (op.type === OpCode.DELETE_OBJECT_KEY) {
8081
- return this.#applyDeleteObjectKey(op, isLocal);
8130
+ if (Object.keys(updates).length > 0) {
8131
+ root.update(updates);
8132
+ }
8133
+ }
8134
+ function getParentsPath(node) {
8135
+ const path = [];
8136
+ while (node.parent.type === "HasParent") {
8137
+ if (isLiveList(node.parent.node)) {
8138
+ path.push(node.parent.node._indexOfPosition(node.parent.key));
8139
+ } else {
8140
+ path.push(node.parent.key);
8082
8141
  }
8083
- return super._apply(op, isLocal);
8142
+ node = node.parent.node;
8084
8143
  }
8085
- /** @internal */
8086
- _serialize() {
8087
- const data = {};
8088
- for (const [key, value] of this.#map) {
8089
- if (!isLiveNode(value)) {
8090
- data[key] = value;
8144
+ return path;
8145
+ }
8146
+ function legacy_patchImmutableObject(state, updates) {
8147
+ return updates.reduce(
8148
+ (state2, update) => legacy_patchImmutableObjectWithUpdate(state2, update),
8149
+ state
8150
+ );
8151
+ }
8152
+ function legacy_patchImmutableObjectWithUpdate(state, update) {
8153
+ const path = getParentsPath(update.node);
8154
+ return legacy_patchImmutableNode(state, path, update);
8155
+ }
8156
+ function legacy_patchImmutableNode(state, path, update) {
8157
+ const pathItem = path.pop();
8158
+ if (pathItem === void 0) {
8159
+ switch (update.type) {
8160
+ case "LiveObject": {
8161
+ if (!isJsonObject(state)) {
8162
+ throw new Error(
8163
+ "Internal: received update on LiveObject but state was not an object"
8164
+ );
8165
+ }
8166
+ const newState = Object.assign({}, state);
8167
+ for (const key in update.updates) {
8168
+ if (update.updates[key]?.type === "update") {
8169
+ const val = update.node.get(key);
8170
+ if (val !== void 0) {
8171
+ newState[key] = lsonToJson(val);
8172
+ }
8173
+ } else if (update.updates[key]?.type === "delete") {
8174
+ delete newState[key];
8175
+ }
8176
+ }
8177
+ return newState;
8178
+ }
8179
+ case "LiveList": {
8180
+ if (!Array.isArray(state)) {
8181
+ throw new Error(
8182
+ "Internal: received update on LiveList but state was not an array"
8183
+ );
8184
+ }
8185
+ let newState = state.map((x) => x);
8186
+ for (const listUpdate of update.updates) {
8187
+ if (listUpdate.type === "set") {
8188
+ newState = newState.map(
8189
+ (item, index) => index === listUpdate.index ? lsonToJson(listUpdate.item) : item
8190
+ );
8191
+ } else if (listUpdate.type === "insert") {
8192
+ if (listUpdate.index === newState.length) {
8193
+ newState.push(lsonToJson(listUpdate.item));
8194
+ } else {
8195
+ newState = [
8196
+ ...newState.slice(0, listUpdate.index),
8197
+ lsonToJson(listUpdate.item),
8198
+ ...newState.slice(listUpdate.index)
8199
+ ];
8200
+ }
8201
+ } else if (listUpdate.type === "delete") {
8202
+ newState.splice(listUpdate.index, 1);
8203
+ } else if (listUpdate.type === "move") {
8204
+ if (listUpdate.previousIndex > listUpdate.index) {
8205
+ newState = [
8206
+ ...newState.slice(0, listUpdate.index),
8207
+ lsonToJson(listUpdate.item),
8208
+ ...newState.slice(listUpdate.index, listUpdate.previousIndex),
8209
+ ...newState.slice(listUpdate.previousIndex + 1)
8210
+ ];
8211
+ } else {
8212
+ newState = [
8213
+ ...newState.slice(0, listUpdate.previousIndex),
8214
+ ...newState.slice(
8215
+ listUpdate.previousIndex + 1,
8216
+ listUpdate.index + 1
8217
+ ),
8218
+ lsonToJson(listUpdate.item),
8219
+ ...newState.slice(listUpdate.index + 1)
8220
+ ];
8221
+ }
8222
+ }
8223
+ }
8224
+ return newState;
8225
+ }
8226
+ case "LiveMap": {
8227
+ if (!isJsonObject(state)) {
8228
+ throw new Error(
8229
+ "Internal: received update on LiveMap but state was not an object"
8230
+ );
8231
+ }
8232
+ const newState = Object.assign({}, state);
8233
+ for (const key in update.updates) {
8234
+ if (update.updates[key]?.type === "update") {
8235
+ const value = update.node.get(key);
8236
+ if (value !== void 0) {
8237
+ newState[key] = lsonToJson(value);
8238
+ }
8239
+ } else if (update.updates[key]?.type === "delete") {
8240
+ delete newState[key];
8241
+ }
8242
+ }
8243
+ return newState;
8091
8244
  }
8092
8245
  }
8093
- if (this.parent.type === "HasParent" && this.parent.node._id) {
8094
- return {
8095
- type: CrdtType.OBJECT,
8096
- parentId: this.parent.node._id,
8097
- parentKey: this.parent.key,
8098
- data
8099
- };
8246
+ }
8247
+ if (Array.isArray(state)) {
8248
+ const newArray = [...state];
8249
+ newArray[pathItem] = legacy_patchImmutableNode(
8250
+ state[pathItem],
8251
+ path,
8252
+ update
8253
+ );
8254
+ return newArray;
8255
+ } else if (isJsonObject(state)) {
8256
+ const node = state[pathItem];
8257
+ if (node === void 0) {
8258
+ return state;
8100
8259
  } else {
8260
+ const stateAsObj = state;
8101
8261
  return {
8102
- type: CrdtType.OBJECT,
8103
- data
8262
+ ...stateAsObj,
8263
+ [pathItem]: legacy_patchImmutableNode(node, path, update)
8104
8264
  };
8105
8265
  }
8266
+ } else {
8267
+ return state;
8106
8268
  }
8107
- #applyUpdate(op, isLocal) {
8108
- let isModified = false;
8109
- const id = nn(this._id);
8110
- const reverse = [];
8111
- const reverseUpdate = {
8112
- type: OpCode.UPDATE_OBJECT,
8113
- id,
8114
- data: {}
8115
- };
8116
- for (const key in op.data) {
8117
- const oldValue = this.#map.get(key);
8118
- if (isLiveNode(oldValue)) {
8119
- for (const childOp of oldValue._toOps(id, key)) {
8120
- reverse.push(childOp);
8121
- }
8122
- oldValue._detach();
8123
- } else if (oldValue !== void 0) {
8124
- reverseUpdate.data[key] = oldValue;
8125
- } else if (oldValue === void 0) {
8126
- reverse.push({ type: OpCode.DELETE_OBJECT_KEY, id, key });
8269
+ }
8270
+
8271
+ // src/crdts/LiveObject.ts
8272
+ var MAX_LIVE_OBJECT_SIZE = 128 * 1024;
8273
+ var LiveObject = class _LiveObject extends AbstractCrdt {
8274
+ #synced;
8275
+ #local = /* @__PURE__ */ new Map();
8276
+ /**
8277
+ * Tracks unacknowledged local changes per property to preserve optimistic
8278
+ * updates. Maps property keys to their pending operation IDs.
8279
+ *
8280
+ * INVARIANT: Only locally-generated opIds are ever stored here. Remote opIds
8281
+ * are only compared against (to detect ACKs), never stored.
8282
+ *
8283
+ * When a local change is made, the opId is stored here. When a remote op
8284
+ * arrives for the same key:
8285
+ * - If no entry exists apply remote op
8286
+ * - If opId matches → it's an ACK, clear the entry
8287
+ * - If opId differs ignore remote op to preserve optimistic update
8288
+ */
8289
+ #unackedOpsByKey;
8290
+ /**
8291
+ * Enable or disable detection of too large LiveObjects.
8292
+ * When enabled, throws an error if LiveObject static data exceeds 128KB, which
8293
+ * is the maximum value the server will be able to accept.
8294
+ * By default, this behavior is disabled to avoid the runtime performance
8295
+ * overhead on every LiveObject.set() or LiveObject.update() call.
8296
+ *
8297
+ * @experimental
8298
+ */
8299
+ static detectLargeObjects = false;
8300
+ static #buildRootAndParentToChildren(nodes) {
8301
+ const parentToChildren = /* @__PURE__ */ new Map();
8302
+ let root = null;
8303
+ for (const node of nodes) {
8304
+ if (isRootStorageNode(node)) {
8305
+ root = node[1];
8306
+ } else {
8307
+ const crdt = node[1];
8308
+ const children = parentToChildren.get(crdt.parentId);
8309
+ if (children !== void 0) {
8310
+ children.push(node);
8311
+ } else {
8312
+ parentToChildren.set(crdt.parentId, [node]);
8313
+ }
8314
+ }
8315
+ }
8316
+ if (root === null) {
8317
+ throw new Error("Root can't be null");
8318
+ }
8319
+ return [root, parentToChildren];
8320
+ }
8321
+ /** @private Do not use this API directly */
8322
+ static _fromItems(nodes, pool) {
8323
+ const [root, parentToChildren] = _LiveObject.#buildRootAndParentToChildren(nodes);
8324
+ return _LiveObject._deserialize(
8325
+ ["root", root],
8326
+ parentToChildren,
8327
+ pool
8328
+ );
8329
+ }
8330
+ constructor(obj = {}) {
8331
+ super();
8332
+ this.#unackedOpsByKey = /* @__PURE__ */ new Map();
8333
+ const o = compactObject(obj);
8334
+ for (const key of Object.keys(o)) {
8335
+ const value = o[key];
8336
+ if (isLiveNode(value)) {
8337
+ value._setParentLink(this, key);
8338
+ }
8339
+ }
8340
+ this.#synced = new Map(Object.entries(o));
8341
+ }
8342
+ /** @internal */
8343
+ _toOps(parentId, parentKey) {
8344
+ if (this._id === void 0) {
8345
+ throw new Error("Cannot serialize item is not attached");
8346
+ }
8347
+ const ops = [];
8348
+ const op = {
8349
+ type: OpCode.CREATE_OBJECT,
8350
+ id: this._id,
8351
+ parentId,
8352
+ parentKey,
8353
+ data: {}
8354
+ };
8355
+ ops.push(op);
8356
+ for (const [key, value] of this.#synced) {
8357
+ if (isLiveNode(value)) {
8358
+ for (const childOp of value._toOps(this._id, key)) {
8359
+ ops.push(childOp);
8360
+ }
8361
+ } else {
8362
+ op.data[key] = value;
8363
+ }
8364
+ }
8365
+ return ops;
8366
+ }
8367
+ /** @internal */
8368
+ static _deserialize([id, item], parentToChildren, pool) {
8369
+ const liveObj = new _LiveObject(item.data);
8370
+ liveObj._attach(id, pool);
8371
+ return this._deserializeChildren(liveObj, parentToChildren, pool);
8372
+ }
8373
+ /** @internal */
8374
+ static _deserializeChildren(liveObj, parentToChildren, pool) {
8375
+ const children = parentToChildren.get(nn(liveObj._id));
8376
+ if (children === void 0) {
8377
+ return liveObj;
8378
+ }
8379
+ for (const node of children) {
8380
+ const child = deserializeToLson(node, parentToChildren, pool);
8381
+ const crdt = node[1];
8382
+ if (isLiveStructure(child)) {
8383
+ child._setParentLink(liveObj, crdt.parentKey);
8384
+ }
8385
+ liveObj.#synced.set(crdt.parentKey, child);
8386
+ liveObj.invalidate();
8387
+ }
8388
+ return liveObj;
8389
+ }
8390
+ /** @internal */
8391
+ _attach(id, pool) {
8392
+ super._attach(id, pool);
8393
+ for (const [_key, value] of this.#synced) {
8394
+ if (isLiveNode(value)) {
8395
+ value._attach(pool.generateId(), pool);
8396
+ }
8397
+ }
8398
+ }
8399
+ /** @internal */
8400
+ _attachChild(op, source) {
8401
+ if (this._pool === void 0) {
8402
+ throw new Error("Can't attach child if managed pool is not present");
8403
+ }
8404
+ const { id, opId, parentKey: key } = op;
8405
+ const child = creationOpToLson(op);
8406
+ if (this._pool.getNode(id) !== void 0) {
8407
+ if (this.#unackedOpsByKey.get(key) === opId) {
8408
+ this.#unackedOpsByKey.delete(key);
8409
+ }
8410
+ return { modified: false };
8411
+ }
8412
+ if (source === 0 /* LOCAL */) {
8413
+ this.#unackedOpsByKey.set(key, nn(opId));
8414
+ } else if (this.#unackedOpsByKey.get(key) === void 0) {
8415
+ } else if (this.#unackedOpsByKey.get(key) === opId) {
8416
+ this.#unackedOpsByKey.delete(key);
8417
+ return { modified: false };
8418
+ } else {
8419
+ return { modified: false };
8420
+ }
8421
+ const thisId = nn(this._id);
8422
+ const previousValue = this.#synced.get(key);
8423
+ let reverse;
8424
+ if (isLiveNode(previousValue)) {
8425
+ reverse = previousValue._toOps(thisId, key);
8426
+ previousValue._detach();
8427
+ } else if (previousValue === void 0) {
8428
+ reverse = [{ type: OpCode.DELETE_OBJECT_KEY, id: thisId, key }];
8429
+ } else {
8430
+ reverse = [
8431
+ {
8432
+ type: OpCode.UPDATE_OBJECT,
8433
+ id: thisId,
8434
+ data: { [key]: previousValue }
8435
+ }
8436
+ ];
8437
+ }
8438
+ this.#local.delete(key);
8439
+ this.#synced.set(key, child);
8440
+ this.invalidate();
8441
+ if (isLiveStructure(child)) {
8442
+ child._setParentLink(this, key);
8443
+ child._attach(id, this._pool);
8444
+ }
8445
+ return {
8446
+ reverse,
8447
+ modified: {
8448
+ node: this,
8449
+ type: "LiveObject",
8450
+ updates: { [key]: { type: "update" } }
8451
+ }
8452
+ };
8453
+ }
8454
+ /** @internal */
8455
+ _detachChild(child) {
8456
+ if (child) {
8457
+ const id = nn(this._id);
8458
+ const parentKey = nn(child._parentKey);
8459
+ const reverse = child._toOps(id, parentKey);
8460
+ for (const [key, value] of this.#synced) {
8461
+ if (value === child) {
8462
+ this.#synced.delete(key);
8463
+ this.invalidate();
8464
+ }
8465
+ }
8466
+ child._detach();
8467
+ const storageUpdate = {
8468
+ node: this,
8469
+ type: "LiveObject",
8470
+ updates: {
8471
+ [parentKey]: { type: "delete" }
8472
+ }
8473
+ };
8474
+ return { modified: storageUpdate, reverse };
8475
+ }
8476
+ return { modified: false };
8477
+ }
8478
+ /** @internal */
8479
+ _detach() {
8480
+ super._detach();
8481
+ for (const value of this.#synced.values()) {
8482
+ if (isLiveNode(value)) {
8483
+ value._detach();
8484
+ }
8485
+ }
8486
+ }
8487
+ /** @internal */
8488
+ _apply(op, isLocal) {
8489
+ if (op.type === OpCode.UPDATE_OBJECT) {
8490
+ return this.#applyUpdate(op, isLocal);
8491
+ } else if (op.type === OpCode.DELETE_OBJECT_KEY) {
8492
+ return this.#applyDeleteObjectKey(op, isLocal);
8493
+ }
8494
+ return super._apply(op, isLocal);
8495
+ }
8496
+ /** @internal */
8497
+ _serialize() {
8498
+ const data = {};
8499
+ for (const [key, value] of this.#synced) {
8500
+ if (!isLiveNode(value)) {
8501
+ data[key] = value;
8502
+ }
8503
+ }
8504
+ if (this.parent.type === "HasParent" && this.parent.node._id) {
8505
+ return {
8506
+ type: CrdtType.OBJECT,
8507
+ parentId: this.parent.node._id,
8508
+ parentKey: this.parent.key,
8509
+ data
8510
+ };
8511
+ } else {
8512
+ return {
8513
+ type: CrdtType.OBJECT,
8514
+ data
8515
+ };
8516
+ }
8517
+ }
8518
+ #applyUpdate(op, isLocal) {
8519
+ let isModified = false;
8520
+ const id = nn(this._id);
8521
+ const reverse = [];
8522
+ const reverseUpdate = {
8523
+ type: OpCode.UPDATE_OBJECT,
8524
+ id,
8525
+ data: {}
8526
+ };
8527
+ for (const key in op.data) {
8528
+ const oldValue = this.#synced.get(key);
8529
+ if (isLiveNode(oldValue)) {
8530
+ for (const childOp of oldValue._toOps(id, key)) {
8531
+ reverse.push(childOp);
8532
+ }
8533
+ oldValue._detach();
8534
+ } else if (oldValue !== void 0) {
8535
+ reverseUpdate.data[key] = oldValue;
8536
+ } else if (oldValue === void 0) {
8537
+ reverse.push({ type: OpCode.DELETE_OBJECT_KEY, id, key });
8127
8538
  }
8128
8539
  }
8129
8540
  const updateDelta = {};
@@ -8142,13 +8553,14 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8142
8553
  } else {
8143
8554
  continue;
8144
8555
  }
8145
- const oldValue = this.#map.get(key);
8556
+ const oldValue = this.#synced.get(key);
8146
8557
  if (isLiveNode(oldValue)) {
8147
8558
  oldValue._detach();
8148
8559
  }
8149
8560
  isModified = true;
8150
8561
  updateDelta[key] = { type: "update" };
8151
- this.#map.set(key, value);
8562
+ this.#local.delete(key);
8563
+ this.#synced.set(key, value);
8152
8564
  this.invalidate();
8153
8565
  }
8154
8566
  if (Object.keys(reverseUpdate.data).length !== 0) {
@@ -8165,7 +8577,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8165
8577
  }
8166
8578
  #applyDeleteObjectKey(op, isLocal) {
8167
8579
  const key = op.key;
8168
- const oldValue = this.#map.get(key);
8580
+ const oldValue = this.#synced.get(key);
8169
8581
  if (oldValue === void 0) {
8170
8582
  return { modified: false };
8171
8583
  }
@@ -8186,7 +8598,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8186
8598
  }
8187
8599
  ];
8188
8600
  }
8189
- this.#map.delete(key);
8601
+ this.#local.delete(key);
8602
+ this.#synced.delete(key);
8190
8603
  this.invalidate();
8191
8604
  return {
8192
8605
  modified: {
@@ -8199,11 +8612,23 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8199
8612
  reverse
8200
8613
  };
8201
8614
  }
8615
+ /** @private */
8616
+ keys() {
8617
+ const result = new Set(this.#synced.keys());
8618
+ for (const key of this.#local.keys()) {
8619
+ result.add(key);
8620
+ }
8621
+ return result;
8622
+ }
8202
8623
  /**
8203
8624
  * Transform the LiveObject into a javascript object
8204
8625
  */
8205
8626
  toObject() {
8206
- return Object.fromEntries(this.#map);
8627
+ const result = Object.fromEntries(this.#synced);
8628
+ for (const [key, value] of this.#local) {
8629
+ result[key] = value;
8630
+ }
8631
+ return result;
8207
8632
  }
8208
8633
  /**
8209
8634
  * Adds or updates a property with a specified key and a value.
@@ -8211,49 +8636,109 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8211
8636
  * @param value The value of the property to add
8212
8637
  */
8213
8638
  set(key, value) {
8214
- this._pool?.assertStorageIsWritable();
8215
8639
  this.update({ [key]: value });
8216
8640
  }
8641
+ /**
8642
+ * @experimental
8643
+ *
8644
+ * Sets a local-only property that is not synchronized over the wire.
8645
+ * The value will be visible via get(), toObject(), and toImmutable() on
8646
+ * this client only. Other clients and the server will see `undefined`
8647
+ * for this key.
8648
+ *
8649
+ * Caveat: this method will not add changes to the undo/redo stack.
8650
+ */
8651
+ setLocal(key, value) {
8652
+ this._pool?.assertStorageIsWritable();
8653
+ const deleteResult = this.#prepareDelete(key);
8654
+ this.#local.set(key, value);
8655
+ this.invalidate();
8656
+ if (this._pool !== void 0 && this._id !== void 0) {
8657
+ const ops = deleteResult?.[0] ?? [];
8658
+ const reverse = deleteResult?.[1] ?? [];
8659
+ const storageUpdates = deleteResult?.[2] ?? /* @__PURE__ */ new Map();
8660
+ const existing = storageUpdates.get(this._id);
8661
+ storageUpdates.set(this._id, {
8662
+ node: this,
8663
+ type: "LiveObject",
8664
+ updates: {
8665
+ ...existing?.updates,
8666
+ [key]: { type: "update" }
8667
+ }
8668
+ });
8669
+ this._pool.dispatch(ops, reverse, storageUpdates);
8670
+ }
8671
+ }
8217
8672
  /**
8218
8673
  * Returns a specified property from the LiveObject.
8219
8674
  * @param key The key of the property to get
8220
8675
  */
8221
8676
  get(key) {
8222
- return this.#map.get(key);
8677
+ return this.#local.has(key) ? this.#local.get(key) : this.#synced.get(key);
8223
8678
  }
8224
8679
  /**
8225
- * Deletes a key from the LiveObject
8226
- * @param key The key of the property to delete
8680
+ * Removes a synced key, returning the ops, reverse ops, and storage updates
8681
+ * needed to notify the pool. Returns null if the key doesn't exist in
8682
+ * #synced or pool/id are unavailable. Does NOT dispatch.
8227
8683
  */
8228
- delete(key) {
8684
+ #prepareDelete(key) {
8229
8685
  this._pool?.assertStorageIsWritable();
8230
- const keyAsString = key;
8231
- const oldValue = this.#map.get(keyAsString);
8686
+ const k = key;
8687
+ if (this.#local.has(k) && !this.#synced.has(k)) {
8688
+ const oldValue2 = this.#local.get(k);
8689
+ this.#local.delete(k);
8690
+ this.invalidate();
8691
+ if (this._pool !== void 0 && this._id !== void 0) {
8692
+ const storageUpdates2 = /* @__PURE__ */ new Map();
8693
+ storageUpdates2.set(this._id, {
8694
+ node: this,
8695
+ type: "LiveObject",
8696
+ updates: {
8697
+ [k]: {
8698
+ type: "delete",
8699
+ deletedItem: oldValue2
8700
+ }
8701
+ }
8702
+ });
8703
+ return [[], [], storageUpdates2];
8704
+ }
8705
+ return null;
8706
+ }
8707
+ this.#local.delete(k);
8708
+ const oldValue = this.#synced.get(k);
8232
8709
  if (oldValue === void 0) {
8233
- return;
8710
+ return null;
8234
8711
  }
8235
8712
  if (this._pool === void 0 || this._id === void 0) {
8236
8713
  if (isLiveNode(oldValue)) {
8237
8714
  oldValue._detach();
8238
8715
  }
8239
- this.#map.delete(keyAsString);
8716
+ this.#synced.delete(k);
8240
8717
  this.invalidate();
8241
- return;
8718
+ return null;
8242
8719
  }
8720
+ const ops = [
8721
+ {
8722
+ type: OpCode.DELETE_OBJECT_KEY,
8723
+ key: k,
8724
+ id: this._id,
8725
+ opId: this._pool.generateOpId()
8726
+ }
8727
+ ];
8243
8728
  let reverse;
8244
8729
  if (isLiveNode(oldValue)) {
8245
8730
  oldValue._detach();
8246
- reverse = oldValue._toOps(this._id, keyAsString);
8731
+ reverse = oldValue._toOps(this._id, k);
8247
8732
  } else {
8248
8733
  reverse = [
8249
8734
  {
8250
8735
  type: OpCode.UPDATE_OBJECT,
8251
- data: { [keyAsString]: oldValue },
8736
+ data: { [k]: oldValue },
8252
8737
  id: this._id
8253
8738
  }
8254
8739
  ];
8255
8740
  }
8256
- this.#map.delete(keyAsString);
8741
+ this.#synced.delete(k);
8257
8742
  this.invalidate();
8258
8743
  const storageUpdates = /* @__PURE__ */ new Map();
8259
8744
  storageUpdates.set(this._id, {
@@ -8263,18 +8748,18 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8263
8748
  [key]: { type: "delete", deletedItem: oldValue }
8264
8749
  }
8265
8750
  });
8266
- this._pool.dispatch(
8267
- [
8268
- {
8269
- type: OpCode.DELETE_OBJECT_KEY,
8270
- key: keyAsString,
8271
- id: this._id,
8272
- opId: this._pool.generateOpId()
8273
- }
8274
- ],
8275
- reverse,
8276
- storageUpdates
8277
- );
8751
+ return [ops, reverse, storageUpdates];
8752
+ }
8753
+ /**
8754
+ * Deletes a key from the LiveObject
8755
+ * @param key The key of the property to delete
8756
+ */
8757
+ delete(key) {
8758
+ const result = this.#prepareDelete(key);
8759
+ if (result) {
8760
+ const [ops, reverse, storageUpdates] = result;
8761
+ this._pool?.dispatch(ops, reverse, storageUpdates);
8762
+ }
8278
8763
  }
8279
8764
  /**
8280
8765
  * Adds or updates multiple properties at once with an object.
@@ -8284,7 +8769,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8284
8769
  this._pool?.assertStorageIsWritable();
8285
8770
  if (_LiveObject.detectLargeObjects) {
8286
8771
  const data = {};
8287
- for (const [key, value] of this.#map) {
8772
+ for (const [key, value] of this.#synced) {
8288
8773
  if (!isLiveNode(value)) {
8289
8774
  data[key] = value;
8290
8775
  }
@@ -8313,14 +8798,15 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8313
8798
  if (newValue === void 0) {
8314
8799
  continue;
8315
8800
  }
8316
- const oldValue = this.#map.get(key);
8801
+ const oldValue = this.#synced.get(key);
8317
8802
  if (isLiveNode(oldValue)) {
8318
8803
  oldValue._detach();
8319
8804
  }
8320
8805
  if (isLiveNode(newValue)) {
8321
8806
  newValue._setParentLink(this, key);
8322
8807
  }
8323
- this.#map.set(key, newValue);
8808
+ this.#local.delete(key);
8809
+ this.#synced.set(key, newValue);
8324
8810
  this.invalidate();
8325
8811
  }
8326
8812
  return;
@@ -8340,7 +8826,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8340
8826
  if (newValue === void 0) {
8341
8827
  continue;
8342
8828
  }
8343
- const oldValue = this.#map.get(key);
8829
+ const oldValue = this.#synced.get(key);
8344
8830
  if (isLiveNode(oldValue)) {
8345
8831
  for (const childOp of oldValue._toOps(this._id, key)) {
8346
8832
  reverseOps.push(childOp);
@@ -8372,7 +8858,8 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8372
8858
  updatedProps[key] = newValue;
8373
8859
  this.#unackedOpsByKey.set(key, opId);
8374
8860
  }
8375
- this.#map.set(key, newValue);
8861
+ this.#local.delete(key);
8862
+ this.#synced.set(key, newValue);
8376
8863
  this.invalidate();
8377
8864
  updateDelta[key] = { type: "update" };
8378
8865
  }
@@ -8395,6 +8882,19 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8395
8882
  });
8396
8883
  this._pool.dispatch(ops, reverseOps, storageUpdates);
8397
8884
  }
8885
+ /**
8886
+ * Reconciles a LiveObject tree to match the given JSON object and sync
8887
+ * config. Only mutates keys that actually changed. Recursively reconciles
8888
+ * the entire Live tree below it.
8889
+ */
8890
+ reconcile(jsonObj, config) {
8891
+ if (this.immutableIs(jsonObj)) return;
8892
+ if (!isPlainObject(jsonObj))
8893
+ throw new Error(
8894
+ "Reconciling the document root expects a plain object value"
8895
+ );
8896
+ reconcileLiveObject(this, jsonObj, config);
8897
+ }
8398
8898
  toImmutable() {
8399
8899
  return super.toImmutable();
8400
8900
  }
@@ -8409,7 +8909,7 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8409
8909
  type: "LiveObject",
8410
8910
  id: nodeId,
8411
8911
  key,
8412
- payload: Array.from(this.#map.entries()).map(
8912
+ payload: Array.from(this.#synced.entries()).map(
8413
8913
  ([key2, value]) => isLiveNode(value) ? value.toTreeNode(key2) : { type: "Json", id: `${nodeId}:${key2}`, key: key2, payload: value }
8414
8914
  )
8415
8915
  };
@@ -8417,20 +8917,27 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
8417
8917
  /** @internal */
8418
8918
  _toImmutable() {
8419
8919
  const result = {};
8420
- for (const [key, val] of this.#map) {
8920
+ for (const [key, val] of this.#synced) {
8421
8921
  result[key] = isLiveStructure(val) ? val.toImmutable() : val;
8422
8922
  }
8923
+ for (const [key, val] of this.#local) {
8924
+ result[key] = val;
8925
+ }
8423
8926
  return process.env.NODE_ENV === "production" ? result : Object.freeze(result);
8424
8927
  }
8425
8928
  clone() {
8426
- return new _LiveObject(
8929
+ const cloned = new _LiveObject(
8427
8930
  Object.fromEntries(
8428
- Array.from(this.#map).map(([key, value]) => [
8931
+ Array.from(this.#synced).map(([key, value]) => [
8429
8932
  key,
8430
8933
  isLiveStructure(value) ? value.clone() : deepClone(value)
8431
8934
  ])
8432
8935
  )
8433
8936
  );
8937
+ for (const [key, value] of this.#local) {
8938
+ cloned.#local.set(key, deepClone(value));
8939
+ }
8940
+ return cloned;
8434
8941
  }
8435
8942
  };
8436
8943
 
@@ -8730,17 +9237,6 @@ var Deque = class {
8730
9237
  }
8731
9238
  };
8732
9239
 
8733
- // src/lib/Json.ts
8734
- function isJsonScalar(data) {
8735
- return data === null || typeof data === "string" || typeof data === "number" || typeof data === "boolean";
8736
- }
8737
- function isJsonArray(data) {
8738
- return Array.isArray(data);
8739
- }
8740
- function isJsonObject(data) {
8741
- return !isJsonScalar(data) && !isJsonArray(data);
8742
- }
8743
-
8744
9240
  // src/lib/stopwatch.ts
8745
9241
  function makeStopWatch() {
8746
9242
  let startTime = 0;
@@ -9226,9 +9722,13 @@ function createRoom(options, config) {
9226
9722
  }
9227
9723
  context.activeBatch.reverseOps.pushLeft(reverse);
9228
9724
  } else {
9229
- addToUndoStack(reverse);
9230
- context.redoStack.length = 0;
9231
- dispatchOps(ops);
9725
+ if (reverse.length > 0) {
9726
+ addToUndoStack(reverse);
9727
+ }
9728
+ if (ops.length > 0) {
9729
+ context.redoStack.length = 0;
9730
+ dispatchOps(ops);
9731
+ }
9232
9732
  notify({ storageUpdates });
9233
9733
  }
9234
9734
  }
@@ -9337,19 +9837,20 @@ function createRoom(options, config) {
9337
9837
  );
9338
9838
  }
9339
9839
  const canWrite = self.get()?.canWrite ?? true;
9340
- const stackSizeBefore = context.undoStack.length;
9341
- for (const key in context.initialStorage) {
9342
- if (context.root.get(key) === void 0) {
9343
- if (canWrite) {
9344
- context.root.set(key, cloneLson(context.initialStorage[key]));
9345
- } else {
9346
- warn(
9347
- `Attempted to populate missing storage key '${key}', but current user has no write access`
9348
- );
9840
+ const root = context.root;
9841
+ withoutHistory(() => {
9842
+ for (const key in context.initialStorage) {
9843
+ if (root.get(key) === void 0) {
9844
+ if (canWrite) {
9845
+ root.set(key, cloneLson(context.initialStorage[key]));
9846
+ } else {
9847
+ warn(
9848
+ `Attempted to populate missing storage key '${key}', but current user has no write access`
9849
+ );
9850
+ }
9349
9851
  }
9350
9852
  }
9351
- }
9352
- context.undoStack.length = stackSizeBefore;
9853
+ });
9353
9854
  }
9354
9855
  function _addToRealUndoStack(frames) {
9355
9856
  if (context.undoStack.length >= 50) {
@@ -10042,6 +10543,16 @@ function createRoom(options, config) {
10042
10543
  _addToRealUndoStack(Array.from(frames));
10043
10544
  }
10044
10545
  }
10546
+ function withoutHistory(fn) {
10547
+ const undoBefore = context.undoStack.length;
10548
+ const redoBefore = context.redoStack.length;
10549
+ try {
10550
+ return fn();
10551
+ } finally {
10552
+ context.undoStack.length = undoBefore;
10553
+ context.redoStack.length = redoBefore;
10554
+ }
10555
+ }
10045
10556
  const syncSourceForStorage = config.createSyncSource();
10046
10557
  function getStorageStatus() {
10047
10558
  if (context.root === void 0) {
@@ -10340,7 +10851,10 @@ function createRoom(options, config) {
10340
10851
  canRedo,
10341
10852
  clear,
10342
10853
  pause: pauseHistory,
10343
- resume: resumeHistory
10854
+ resume: resumeHistory,
10855
+ [kInternal]: {
10856
+ withoutHistory
10857
+ }
10344
10858
  },
10345
10859
  fetchYDoc,
10346
10860
  getStorage,
@@ -11250,309 +11764,6 @@ function toPlainLson(lson) {
11250
11764
  }
11251
11765
  }
11252
11766
 
11253
- // src/immutable.ts
11254
- function lsonObjectToJson(obj) {
11255
- const result = {};
11256
- for (const key in obj) {
11257
- const val = obj[key];
11258
- if (val !== void 0) {
11259
- result[key] = lsonToJson(val);
11260
- }
11261
- }
11262
- return result;
11263
- }
11264
- function liveObjectToJson(liveObject) {
11265
- return lsonObjectToJson(liveObject.toObject());
11266
- }
11267
- function liveMapToJson(map) {
11268
- const result = {};
11269
- for (const [key, value] of map.entries()) {
11270
- result[key] = lsonToJson(value);
11271
- }
11272
- return result;
11273
- }
11274
- function lsonListToJson(value) {
11275
- return value.map(lsonToJson);
11276
- }
11277
- function liveListToJson(value) {
11278
- return lsonListToJson(value.toArray());
11279
- }
11280
- function lsonToJson(value) {
11281
- if (value instanceof LiveObject) {
11282
- return liveObjectToJson(value);
11283
- } else if (value instanceof LiveList) {
11284
- return liveListToJson(value);
11285
- } else if (value instanceof LiveMap) {
11286
- return liveMapToJson(value);
11287
- } else if (value instanceof LiveRegister) {
11288
- return value.data;
11289
- }
11290
- if (Array.isArray(value)) {
11291
- return lsonListToJson(value);
11292
- } else if (isPlainObject(value)) {
11293
- return lsonObjectToJson(value);
11294
- }
11295
- return value;
11296
- }
11297
- function deepLiveify(value) {
11298
- if (Array.isArray(value)) {
11299
- return new LiveList(value.map(deepLiveify));
11300
- } else if (isPlainObject(value)) {
11301
- const init = {};
11302
- for (const key in value) {
11303
- const val = value[key];
11304
- if (val === void 0) {
11305
- continue;
11306
- }
11307
- init[key] = deepLiveify(val);
11308
- }
11309
- return new LiveObject(init);
11310
- } else {
11311
- return value;
11312
- }
11313
- }
11314
- function patchLiveList(liveList, prev, next) {
11315
- let i = 0;
11316
- let prevEnd = prev.length - 1;
11317
- let nextEnd = next.length - 1;
11318
- let prevNode = prev[0];
11319
- let nextNode = next[0];
11320
- outer: {
11321
- while (prevNode === nextNode) {
11322
- ++i;
11323
- if (i > prevEnd || i > nextEnd) {
11324
- break outer;
11325
- }
11326
- prevNode = prev[i];
11327
- nextNode = next[i];
11328
- }
11329
- prevNode = prev[prevEnd];
11330
- nextNode = next[nextEnd];
11331
- while (prevNode === nextNode) {
11332
- prevEnd--;
11333
- nextEnd--;
11334
- if (i > prevEnd || i > nextEnd) {
11335
- break outer;
11336
- }
11337
- prevNode = prev[prevEnd];
11338
- nextNode = next[nextEnd];
11339
- }
11340
- }
11341
- if (i > prevEnd) {
11342
- if (i <= nextEnd) {
11343
- while (i <= nextEnd) {
11344
- liveList.insert(deepLiveify(next[i]), i);
11345
- i++;
11346
- }
11347
- }
11348
- } else if (i > nextEnd) {
11349
- let localI = i;
11350
- while (localI <= prevEnd) {
11351
- liveList.delete(i);
11352
- localI++;
11353
- }
11354
- } else {
11355
- while (i <= prevEnd && i <= nextEnd) {
11356
- prevNode = prev[i];
11357
- nextNode = next[i];
11358
- const liveListNode = liveList.get(i);
11359
- if (isLiveObject(liveListNode) && isPlainObject(prevNode) && isPlainObject(nextNode)) {
11360
- patchLiveObject(liveListNode, prevNode, nextNode);
11361
- } else {
11362
- liveList.set(i, deepLiveify(nextNode));
11363
- }
11364
- i++;
11365
- }
11366
- while (i <= nextEnd) {
11367
- liveList.insert(deepLiveify(next[i]), i);
11368
- i++;
11369
- }
11370
- let localI = i;
11371
- while (localI <= prevEnd) {
11372
- liveList.delete(i);
11373
- localI++;
11374
- }
11375
- }
11376
- }
11377
- function patchLiveObjectKey(liveObject, key, prev, next) {
11378
- if (process.env.NODE_ENV !== "production") {
11379
- const nonSerializableValue = findNonSerializableValue(next);
11380
- if (nonSerializableValue) {
11381
- error2(
11382
- `New state path: '${nonSerializableValue.path}' value: '${String(
11383
- nonSerializableValue.value
11384
- )}' is not serializable.
11385
- Only serializable value can be synced with Liveblocks.`
11386
- );
11387
- return;
11388
- }
11389
- }
11390
- const value = liveObject.get(key);
11391
- if (next === void 0) {
11392
- liveObject.delete(key);
11393
- } else if (value === void 0) {
11394
- liveObject.set(key, deepLiveify(next));
11395
- } else if (prev === next) {
11396
- return;
11397
- } else if (isLiveList(value) && Array.isArray(prev) && Array.isArray(next)) {
11398
- patchLiveList(value, prev, next);
11399
- } else if (isLiveObject(value) && isPlainObject(prev) && isPlainObject(next)) {
11400
- patchLiveObject(value, prev, next);
11401
- } else {
11402
- liveObject.set(key, deepLiveify(next));
11403
- }
11404
- }
11405
- function patchLiveObject(root, prev, next) {
11406
- const updates = {};
11407
- for (const key in next) {
11408
- patchLiveObjectKey(root, key, prev[key], next[key]);
11409
- }
11410
- for (const key in prev) {
11411
- if (next[key] === void 0) {
11412
- root.delete(key);
11413
- }
11414
- }
11415
- if (Object.keys(updates).length > 0) {
11416
- root.update(updates);
11417
- }
11418
- }
11419
- function getParentsPath(node) {
11420
- const path = [];
11421
- while (node.parent.type === "HasParent") {
11422
- if (isLiveList(node.parent.node)) {
11423
- path.push(node.parent.node._indexOfPosition(node.parent.key));
11424
- } else {
11425
- path.push(node.parent.key);
11426
- }
11427
- node = node.parent.node;
11428
- }
11429
- return path;
11430
- }
11431
- function legacy_patchImmutableObject(state, updates) {
11432
- return updates.reduce(
11433
- (state2, update) => legacy_patchImmutableObjectWithUpdate(state2, update),
11434
- state
11435
- );
11436
- }
11437
- function legacy_patchImmutableObjectWithUpdate(state, update) {
11438
- const path = getParentsPath(update.node);
11439
- return legacy_patchImmutableNode(state, path, update);
11440
- }
11441
- function legacy_patchImmutableNode(state, path, update) {
11442
- const pathItem = path.pop();
11443
- if (pathItem === void 0) {
11444
- switch (update.type) {
11445
- case "LiveObject": {
11446
- if (!isJsonObject(state)) {
11447
- throw new Error(
11448
- "Internal: received update on LiveObject but state was not an object"
11449
- );
11450
- }
11451
- const newState = Object.assign({}, state);
11452
- for (const key in update.updates) {
11453
- if (update.updates[key]?.type === "update") {
11454
- const val = update.node.get(key);
11455
- if (val !== void 0) {
11456
- newState[key] = lsonToJson(val);
11457
- }
11458
- } else if (update.updates[key]?.type === "delete") {
11459
- delete newState[key];
11460
- }
11461
- }
11462
- return newState;
11463
- }
11464
- case "LiveList": {
11465
- if (!Array.isArray(state)) {
11466
- throw new Error(
11467
- "Internal: received update on LiveList but state was not an array"
11468
- );
11469
- }
11470
- let newState = state.map((x) => x);
11471
- for (const listUpdate of update.updates) {
11472
- if (listUpdate.type === "set") {
11473
- newState = newState.map(
11474
- (item, index) => index === listUpdate.index ? lsonToJson(listUpdate.item) : item
11475
- );
11476
- } else if (listUpdate.type === "insert") {
11477
- if (listUpdate.index === newState.length) {
11478
- newState.push(lsonToJson(listUpdate.item));
11479
- } else {
11480
- newState = [
11481
- ...newState.slice(0, listUpdate.index),
11482
- lsonToJson(listUpdate.item),
11483
- ...newState.slice(listUpdate.index)
11484
- ];
11485
- }
11486
- } else if (listUpdate.type === "delete") {
11487
- newState.splice(listUpdate.index, 1);
11488
- } else if (listUpdate.type === "move") {
11489
- if (listUpdate.previousIndex > listUpdate.index) {
11490
- newState = [
11491
- ...newState.slice(0, listUpdate.index),
11492
- lsonToJson(listUpdate.item),
11493
- ...newState.slice(listUpdate.index, listUpdate.previousIndex),
11494
- ...newState.slice(listUpdate.previousIndex + 1)
11495
- ];
11496
- } else {
11497
- newState = [
11498
- ...newState.slice(0, listUpdate.previousIndex),
11499
- ...newState.slice(
11500
- listUpdate.previousIndex + 1,
11501
- listUpdate.index + 1
11502
- ),
11503
- lsonToJson(listUpdate.item),
11504
- ...newState.slice(listUpdate.index + 1)
11505
- ];
11506
- }
11507
- }
11508
- }
11509
- return newState;
11510
- }
11511
- case "LiveMap": {
11512
- if (!isJsonObject(state)) {
11513
- throw new Error(
11514
- "Internal: received update on LiveMap but state was not an object"
11515
- );
11516
- }
11517
- const newState = Object.assign({}, state);
11518
- for (const key in update.updates) {
11519
- if (update.updates[key]?.type === "update") {
11520
- const value = update.node.get(key);
11521
- if (value !== void 0) {
11522
- newState[key] = lsonToJson(value);
11523
- }
11524
- } else if (update.updates[key]?.type === "delete") {
11525
- delete newState[key];
11526
- }
11527
- }
11528
- return newState;
11529
- }
11530
- }
11531
- }
11532
- if (Array.isArray(state)) {
11533
- const newArray = [...state];
11534
- newArray[pathItem] = legacy_patchImmutableNode(
11535
- state[pathItem],
11536
- path,
11537
- update
11538
- );
11539
- return newArray;
11540
- } else if (isJsonObject(state)) {
11541
- const node = state[pathItem];
11542
- if (node === void 0) {
11543
- return state;
11544
- } else {
11545
- const stateAsObj = state;
11546
- return {
11547
- ...stateAsObj,
11548
- [pathItem]: legacy_patchImmutableNode(node, path, update)
11549
- };
11550
- }
11551
- } else {
11552
- return state;
11553
- }
11554
- }
11555
-
11556
11767
  // src/lib/abortController.ts
11557
11768
  function makeAbortController(externalSignal) {
11558
11769
  const ctl = new AbortController();
@@ -11765,6 +11976,7 @@ export {
11765
11976
  createManagedPool,
11766
11977
  createNotificationSettings,
11767
11978
  createThreadId,
11979
+ deepLiveifyObject,
11768
11980
  defineAiTool,
11769
11981
  deprecate,
11770
11982
  deprecateIf,
@@ -11798,6 +12010,7 @@ export {
11798
12010
  kInternal,
11799
12011
  keys,
11800
12012
  legacy_patchImmutableObject,
12013
+ legacy_patchLiveObjectKey,
11801
12014
  lsonToJson,
11802
12015
  makeAbortController,
11803
12016
  makeEventSource,
@@ -11809,7 +12022,6 @@ export {
11809
12022
  nn,
11810
12023
  nodeStreamToCompactNodes,
11811
12024
  objectToQuery,
11812
- patchLiveObjectKey,
11813
12025
  patchNotificationSettings,
11814
12026
  raise,
11815
12027
  resolveMentionsInCommentBody,