canvu-react 0.4.9 → 0.4.10

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
@@ -3563,6 +3563,22 @@ function realtimeSessionPlugin(options) {
3563
3563
  render: () => /* @__PURE__ */ jsx(RealtimeSessionPanel, { ...options })
3564
3564
  };
3565
3565
  }
3566
+ function getSceneItemId(item) {
3567
+ const id = item.id;
3568
+ return typeof id === "string" ? id : null;
3569
+ }
3570
+ function hasMissingLocalItems(localItems, incomingItems) {
3571
+ const incomingIds = /* @__PURE__ */ new Set();
3572
+ for (const item of incomingItems) {
3573
+ const id = getSceneItemId(item);
3574
+ if (id) incomingIds.add(id);
3575
+ }
3576
+ for (const item of localItems) {
3577
+ const id = getSceneItemId(item);
3578
+ if (id && !incomingIds.has(id)) return true;
3579
+ }
3580
+ return false;
3581
+ }
3566
3582
  function useRealtimeCanvasDocument(options) {
3567
3583
  const {
3568
3584
  session,
@@ -3581,6 +3597,8 @@ function useRealtimeCanvasDocument(options) {
3581
3597
  const documentItems = session?.document?.items;
3582
3598
  const documentUpdatedByClientId = session?.document?.updatedByClientId ?? null;
3583
3599
  const connectionClientId = session?.connection.clientId ?? null;
3600
+ const hasLocalOfflineDraft = session?.hasLocalOfflineDraft ?? false;
3601
+ const hasPendingDocumentSync = session?.hasPendingDocumentSync ?? false;
3584
3602
  const applyIncomingItems = useCallback(
3585
3603
  async (nextItems) => {
3586
3604
  const normalizedItems = normalizeItems ? normalizeItems(nextItems) : [...nextItems];
@@ -3601,6 +3619,11 @@ function useRealtimeCanvasDocument(options) {
3601
3619
  },
3602
3620
  [enabled, normalizeItems, onItemsChange, session]
3603
3621
  );
3622
+ useEffect(() => {
3623
+ if (items.length > 0) {
3624
+ hasEverPropagatedItemsRef.current = true;
3625
+ }
3626
+ }, [items.length]);
3604
3627
  useEffect(() => {
3605
3628
  if (!realtimeEnabled || !onItemsChange || !session?.document) return;
3606
3629
  if (documentUpdatedByClientId === connectionClientId) return;
@@ -3614,7 +3637,18 @@ function useRealtimeCanvasDocument(options) {
3614
3637
  if (cancelled) return;
3615
3638
  if (inFlightRevisionRef.current !== documentRevision) return;
3616
3639
  lastAppliedRevisionRef.current = documentRevision;
3617
- if (resolvedItems.length === 0 && hasEverPropagatedItemsRef.current) {
3640
+ const hasLocalItems = items.length > 0;
3641
+ const hasPendingLocalChanges = hasLocalOfflineDraft || hasPendingDocumentSync;
3642
+ if (resolvedItems.length === 0 && (hasEverPropagatedItemsRef.current || hasLocalItems || hasPendingLocalChanges)) {
3643
+ if (hasLocalItems) {
3644
+ const normalizedLocalItems = normalizeItems ? normalizeItems(items) : [...items];
3645
+ session.remoteAdapter.send?.(normalizedLocalItems);
3646
+ }
3647
+ return;
3648
+ }
3649
+ if (hasPendingLocalChanges && hasLocalItems && hasMissingLocalItems(items, resolvedItems)) {
3650
+ const normalizedLocalItems = normalizeItems ? normalizeItems(items) : [...items];
3651
+ session.remoteAdapter.send?.(normalizedLocalItems);
3618
3652
  return;
3619
3653
  }
3620
3654
  if (resolvedItems.length > 0) {
@@ -3637,8 +3671,13 @@ function useRealtimeCanvasDocument(options) {
3637
3671
  documentItems,
3638
3672
  documentRevision,
3639
3673
  documentUpdatedByClientId,
3674
+ hasLocalOfflineDraft,
3675
+ hasPendingDocumentSync,
3676
+ items,
3677
+ normalizeItems,
3640
3678
  onItemsChange,
3641
3679
  realtimeEnabled,
3680
+ session?.remoteAdapter,
3642
3681
  session?.document
3643
3682
  ]);
3644
3683
  return useMemo(