canvu-react 0.4.11 → 0.4.12

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.
@@ -44,6 +44,7 @@ type RealtimeDocumentSnapshot = {
44
44
  readonly items: VectorSceneItem[];
45
45
  readonly updatedAt: number;
46
46
  readonly updatedByClientId?: string;
47
+ readonly persistedRevision?: number;
47
48
  };
48
49
  type RealtimeConnectionInfo = {
49
50
  readonly state: RealtimeConnectionState;
@@ -44,6 +44,7 @@ type RealtimeDocumentSnapshot = {
44
44
  readonly items: VectorSceneItem[];
45
45
  readonly updatedAt: number;
46
46
  readonly updatedByClientId?: string;
47
+ readonly persistedRevision?: number;
47
48
  };
48
49
  type RealtimeConnectionInfo = {
49
50
  readonly state: RealtimeConnectionState;
package/dist/realtime.js CHANGED
@@ -503,7 +503,8 @@ function parseDocumentSnapshot(value) {
503
503
  revision,
504
504
  updatedAt,
505
505
  items,
506
- ...getString(value.updatedByClientId) ? { updatedByClientId: getString(value.updatedByClientId) } : {}
506
+ ...getString(value.updatedByClientId) ? { updatedByClientId: getString(value.updatedByClientId) } : {},
507
+ ...getNumber(value.persistedRevision) != null ? { persistedRevision: getNumber(value.persistedRevision) } : {}
507
508
  };
508
509
  }
509
510
  function parseRealtimeSessionPeer(value) {
@@ -2296,6 +2297,9 @@ function sameSerializedItems(left, right) {
2296
2297
  function nowMs() {
2297
2298
  return Date.now();
2298
2299
  }
2300
+ function hasDurableDocumentPersistence(snapshot) {
2301
+ return snapshot.persistedRevision == null || snapshot.persistedRevision >= snapshot.revision;
2302
+ }
2299
2303
  function draftStorageKey(roomId) {
2300
2304
  return `${DRAFT_STORAGE_PREFIX}${roomId}`;
2301
2305
  }
@@ -2521,7 +2525,7 @@ function useRealtimeSession(options) {
2521
2525
  const applyDocument = useCallback(
2522
2526
  (snapshot, options2) => {
2523
2527
  const currentSnapshot = latestDocumentRef.current;
2524
- if (currentSnapshot && currentSnapshot.revision === snapshot.revision && currentSnapshot.updatedByClientId === snapshot.updatedByClientId && sameSerializedItems(currentSnapshot.items, snapshot.items)) {
2528
+ if (currentSnapshot && currentSnapshot.revision === snapshot.revision && currentSnapshot.updatedByClientId === snapshot.updatedByClientId && currentSnapshot.persistedRevision === snapshot.persistedRevision && sameSerializedItems(currentSnapshot.items, snapshot.items)) {
2525
2529
  return;
2526
2530
  }
2527
2531
  const board = boardRef.current;
@@ -2826,11 +2830,23 @@ function useRealtimeSession(options) {
2826
2830
  }
2827
2831
  const pendingIds = getLocallyPendingItemIds(board);
2828
2832
  if (pendingIds.size === 0) {
2829
- clearLocalDraftRef.current();
2830
- setHasPendingDocumentSync(false);
2831
2833
  outboundInFlightRef.current = null;
2832
2834
  queuedDirtyRef.current = false;
2833
- return false;
2835
+ if (hasDurableDocumentPersistence(serverDocument)) {
2836
+ clearLocalDraftRef.current();
2837
+ setHasPendingDocumentSync(false);
2838
+ return false;
2839
+ }
2840
+ const mergedItems2 = readVectorItems(board.yItems);
2841
+ setLocalDraft({
2842
+ roomId,
2843
+ baseRevision: serverDocument.revision,
2844
+ items: mergedItems2,
2845
+ updatedAt: nowMs(),
2846
+ pendingIds: []
2847
+ });
2848
+ setHasPendingDocumentSync(true);
2849
+ return true;
2834
2850
  }
2835
2851
  const mergedItems = readVectorItems(board.yItems);
2836
2852
  setLocalDraft({
@@ -3178,8 +3194,20 @@ function useRealtimeSession(options) {
3178
3194
  scheduleDocumentFlushRef.current();
3179
3195
  } else {
3180
3196
  queuedDirtyRef.current = false;
3181
- clearLocalDraftRef.current();
3182
- setHasPendingDocumentSync(false);
3197
+ if (hasDurableDocumentPersistence(parsed.document)) {
3198
+ clearLocalDraftRef.current();
3199
+ setHasPendingDocumentSync(false);
3200
+ } else {
3201
+ const mergedItems = board ? readVectorItems(board.yItems) : [];
3202
+ setLocalDraftRef.current({
3203
+ roomId,
3204
+ baseRevision: currentRevisionRef.current,
3205
+ items: mergedItems,
3206
+ updatedAt: nowMs(),
3207
+ pendingIds: []
3208
+ });
3209
+ setHasPendingDocumentSync(true);
3210
+ }
3183
3211
  }
3184
3212
  setConflictStateRef.current(null);
3185
3213
  return;