canvu-react 0.4.8 → 0.4.9

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.cjs CHANGED
@@ -2398,6 +2398,30 @@ function prepareRealtimeItems(items) {
2398
2398
  return null;
2399
2399
  }
2400
2400
  }
2401
+ function getRealtimeItemId(item) {
2402
+ const id = item.id;
2403
+ return typeof id === "string" ? id : null;
2404
+ }
2405
+ function serializeRealtimeItem(item) {
2406
+ try {
2407
+ return JSON.stringify(sanitizeRealtimeItem(item));
2408
+ } catch {
2409
+ return null;
2410
+ }
2411
+ }
2412
+ function resolvePendingDraftItemIds(board, items) {
2413
+ const pendingIds = /* @__PURE__ */ new Set();
2414
+ for (const item of items) {
2415
+ const id = getRealtimeItemId(item);
2416
+ if (!id) continue;
2417
+ const confirmedSerialized = board.lastServerConfirmedItemSerializations.get(id);
2418
+ const serialized = serializeRealtimeItem(item);
2419
+ if (!confirmedSerialized || !serialized || serialized !== confirmedSerialized) {
2420
+ pendingIds.add(id);
2421
+ }
2422
+ }
2423
+ return pendingIds.size > 0 ? Array.from(pendingIds) : void 0;
2424
+ }
2401
2425
  function useRealtimeSession(options) {
2402
2426
  const {
2403
2427
  url,
@@ -2588,7 +2612,8 @@ function useRealtimeSession(options) {
2588
2612
  }
2589
2613
  const board = boardRef.current;
2590
2614
  const draft = localDraftRef.current;
2591
- const draftToWrite = board && draft.yDocState == null ? { ...draft, yDocState: encodeYDocState(board) } : draft;
2615
+ const canEncodeYDocState = board && draft.yDocState == null && sameSerializedItems(readVectorItems(board.yItems), draft.items);
2616
+ const draftToWrite = canEncodeYDocState ? { ...draft, yDocState: encodeYDocState(board) } : draft;
2592
2617
  writeRealtimeOfflineDraft(draftToWrite);
2593
2618
  }, [clearDraftPersistSchedule, roomId]);
2594
2619
  const scheduleDraftPersistence = react.useCallback(() => {
@@ -2728,13 +2753,26 @@ function useRealtimeSession(options) {
2728
2753
  }, DOCUMENT_FLUSH_DEBOUNCE_MS);
2729
2754
  }, DOCUMENT_FLUSH_DEBOUNCE_MS);
2730
2755
  }, [clearDocumentFlushSchedule, flushQueuedDocument]);
2731
- const queueDocumentSend = react.useCallback((items) => {
2732
- pendingLocalItemsRef.current = items;
2733
- queuedDirtyRef.current = true;
2734
- setHasPendingDocumentSync(true);
2735
- setHasLocalOfflineDraft(true);
2736
- scheduleDocumentFlushRef.current();
2737
- }, []);
2756
+ const queueDocumentSend = react.useCallback(
2757
+ (items) => {
2758
+ pendingLocalItemsRef.current = items;
2759
+ queuedDirtyRef.current = true;
2760
+ setHasPendingDocumentSync(true);
2761
+ setHasLocalOfflineDraft(true);
2762
+ const board = boardRef.current;
2763
+ const draftItems = sanitizeRealtimeItems(items);
2764
+ setLocalDraftRef.current({
2765
+ roomId,
2766
+ baseRevision: currentRevisionRef.current,
2767
+ items: draftItems,
2768
+ updatedAt: nowMs(),
2769
+ pendingIds: board ? resolvePendingDraftItemIds(board, draftItems) : void 0
2770
+ });
2771
+ scheduleDraftPersistenceRef.current?.();
2772
+ scheduleDocumentFlushRef.current();
2773
+ },
2774
+ [roomId]
2775
+ );
2738
2776
  const applyDraftSnapshot = react.useCallback(
2739
2777
  (draft, options2) => {
2740
2778
  const board = boardRef.current;
@@ -2898,6 +2936,25 @@ function useRealtimeSession(options) {
2898
2936
  setLocalDraftRef.current = setLocalDraft;
2899
2937
  const scheduleDraftPersistenceRef = react.useRef(scheduleDraftPersistence);
2900
2938
  scheduleDraftPersistenceRef.current = scheduleDraftPersistence;
2939
+ const persistLocalDraftRef = react.useRef(persistLocalDraft);
2940
+ persistLocalDraftRef.current = persistLocalDraft;
2941
+ react.useEffect(() => {
2942
+ const persistDraft = () => {
2943
+ persistLocalDraftRef.current();
2944
+ };
2945
+ const handleVisibilityChange = () => {
2946
+ if (window.document.visibilityState === "hidden") persistDraft();
2947
+ };
2948
+ window.addEventListener("pagehide", persistDraft);
2949
+ window.document.addEventListener("visibilitychange", handleVisibilityChange);
2950
+ return () => {
2951
+ window.removeEventListener("pagehide", persistDraft);
2952
+ window.document.removeEventListener(
2953
+ "visibilitychange",
2954
+ handleVisibilityChange
2955
+ );
2956
+ };
2957
+ }, []);
2901
2958
  react.useEffect(() => {
2902
2959
  if (boardRef.current) {
2903
2960
  boardRef.current.doc.destroy();