canvu-react 0.4.7 → 0.4.8

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/realtime.js CHANGED
@@ -1996,6 +1996,7 @@ function createYjsBoardDoc() {
1996
1996
  doc,
1997
1997
  yItems,
1998
1998
  lastServerConfirmedIds: /* @__PURE__ */ new Set(),
1999
+ lastServerConfirmedItemSerializations: /* @__PURE__ */ new Map(),
1999
2000
  serverItemSeenAt: /* @__PURE__ */ new Map()
2000
2001
  };
2001
2002
  }
@@ -2052,6 +2053,10 @@ function valuesEqual(left, right) {
2052
2053
  return false;
2053
2054
  }
2054
2055
  }
2056
+ function serializeItem(value) {
2057
+ const item = value instanceof Y.Map ? yMapToVectorItem(value) : value;
2058
+ return JSON.stringify(item);
2059
+ }
2055
2060
  function updateYMapInPlace(yMap, next) {
2056
2061
  const nextKeys = /* @__PURE__ */ new Set();
2057
2062
  for (const [key, value] of Object.entries(next)) {
@@ -2121,6 +2126,14 @@ function applyServerSnapshotToYDoc(board, options) {
2121
2126
  if (!id) continue;
2122
2127
  const existing = indexYItemsById(board.yItems).get(id);
2123
2128
  if (existing) {
2129
+ const currentSerialized = serializeItem(existing.yMap);
2130
+ const incomingSerialized = serializeItem(item);
2131
+ const confirmedSerialized = board.lastServerConfirmedItemSerializations.get(id);
2132
+ const hasPendingLocalChange = confirmedSerialized === void 0 ? currentSerialized !== incomingSerialized : currentSerialized !== confirmedSerialized && currentSerialized !== incomingSerialized;
2133
+ if (hasPendingLocalChange) {
2134
+ board.serverItemSeenAt.set(id, now);
2135
+ continue;
2136
+ }
2124
2137
  updateYMapInPlace(existing.yMap, item);
2125
2138
  board.serverItemSeenAt.set(id, now);
2126
2139
  continue;
@@ -2131,9 +2144,13 @@ function applyServerSnapshotToYDoc(board, options) {
2131
2144
  }
2132
2145
  }, origin);
2133
2146
  board.lastServerConfirmedIds = /* @__PURE__ */ new Set();
2147
+ board.lastServerConfirmedItemSerializations = /* @__PURE__ */ new Map();
2134
2148
  for (const item of snapshotItems) {
2135
2149
  const id = getItemId(item);
2136
- if (id) board.lastServerConfirmedIds.add(id);
2150
+ if (id) {
2151
+ board.lastServerConfirmedIds.add(id);
2152
+ board.lastServerConfirmedItemSerializations.set(id, serializeItem(item));
2153
+ }
2137
2154
  }
2138
2155
  }
2139
2156
  function replaceYDocWithSnapshot(board, options) {
@@ -2148,11 +2165,13 @@ function replaceYDocWithSnapshot(board, options) {
2148
2165
  }
2149
2166
  }, origin);
2150
2167
  board.lastServerConfirmedIds = /* @__PURE__ */ new Set();
2168
+ board.lastServerConfirmedItemSerializations = /* @__PURE__ */ new Map();
2151
2169
  board.serverItemSeenAt = /* @__PURE__ */ new Map();
2152
2170
  for (const item of snapshotItems) {
2153
2171
  const id = getItemId(item);
2154
2172
  if (id) {
2155
2173
  board.lastServerConfirmedIds.add(id);
2174
+ board.lastServerConfirmedItemSerializations.set(id, serializeItem(item));
2156
2175
  board.serverItemSeenAt.set(id, now);
2157
2176
  }
2158
2177
  }
@@ -2164,7 +2183,8 @@ function getLocallyPendingItemIds(board) {
2164
2183
  if (!yMap) continue;
2165
2184
  const id = getItemId(yMap);
2166
2185
  if (!id) continue;
2167
- if (!board.lastServerConfirmedIds.has(id)) {
2186
+ const confirmedSerialized = board.lastServerConfirmedItemSerializations.get(id);
2187
+ if (!confirmedSerialized || serializeItem(yMap) !== confirmedSerialized) {
2168
2188
  pending.add(id);
2169
2189
  }
2170
2190
  }
@@ -2704,6 +2724,7 @@ function useRealtimeSession(options) {
2704
2724
  restoredFromBinary = decodeYDocState(board, draft.yDocState);
2705
2725
  if (restoredFromBinary) {
2706
2726
  const allIds = /* @__PURE__ */ new Set();
2727
+ const allItems = readVectorItems(board.yItems);
2707
2728
  for (let i = 0; i < board.yItems.length; i++) {
2708
2729
  const yMap = board.yItems.get(i);
2709
2730
  if (!yMap) continue;
@@ -2712,9 +2733,17 @@ function useRealtimeSession(options) {
2712
2733
  }
2713
2734
  const pendingIds = new Set(draft.pendingIds ?? []);
2714
2735
  board.lastServerConfirmedIds = /* @__PURE__ */ new Set();
2736
+ board.lastServerConfirmedItemSerializations = /* @__PURE__ */ new Map();
2715
2737
  for (const id of allIds) {
2716
2738
  if (!pendingIds.has(id)) {
2717
2739
  board.lastServerConfirmedIds.add(id);
2740
+ const item = allItems.find((candidate) => candidate.id === id);
2741
+ if (item) {
2742
+ board.lastServerConfirmedItemSerializations.set(
2743
+ id,
2744
+ JSON.stringify(item)
2745
+ );
2746
+ }
2718
2747
  }
2719
2748
  }
2720
2749
  }
@@ -2725,6 +2754,7 @@ function useRealtimeSession(options) {
2725
2754
  origin: ORIGIN_BOOTSTRAP
2726
2755
  });
2727
2756
  board.lastServerConfirmedIds = /* @__PURE__ */ new Set();
2757
+ board.lastServerConfirmedItemSerializations = /* @__PURE__ */ new Map();
2728
2758
  }
2729
2759
  }
2730
2760
  const items = board ? readVectorItems(board.yItems) : draft.items;