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.cjs CHANGED
@@ -3588,6 +3588,22 @@ function realtimeSessionPlugin(options) {
3588
3588
  render: () => /* @__PURE__ */ jsxRuntime.jsx(RealtimeSessionPanel, { ...options })
3589
3589
  };
3590
3590
  }
3591
+ function getSceneItemId(item) {
3592
+ const id = item.id;
3593
+ return typeof id === "string" ? id : null;
3594
+ }
3595
+ function hasMissingLocalItems(localItems, incomingItems) {
3596
+ const incomingIds = /* @__PURE__ */ new Set();
3597
+ for (const item of incomingItems) {
3598
+ const id = getSceneItemId(item);
3599
+ if (id) incomingIds.add(id);
3600
+ }
3601
+ for (const item of localItems) {
3602
+ const id = getSceneItemId(item);
3603
+ if (id && !incomingIds.has(id)) return true;
3604
+ }
3605
+ return false;
3606
+ }
3591
3607
  function useRealtimeCanvasDocument(options) {
3592
3608
  const {
3593
3609
  session,
@@ -3606,6 +3622,8 @@ function useRealtimeCanvasDocument(options) {
3606
3622
  const documentItems = session?.document?.items;
3607
3623
  const documentUpdatedByClientId = session?.document?.updatedByClientId ?? null;
3608
3624
  const connectionClientId = session?.connection.clientId ?? null;
3625
+ const hasLocalOfflineDraft = session?.hasLocalOfflineDraft ?? false;
3626
+ const hasPendingDocumentSync = session?.hasPendingDocumentSync ?? false;
3609
3627
  const applyIncomingItems = react.useCallback(
3610
3628
  async (nextItems) => {
3611
3629
  const normalizedItems = normalizeItems ? normalizeItems(nextItems) : [...nextItems];
@@ -3626,6 +3644,11 @@ function useRealtimeCanvasDocument(options) {
3626
3644
  },
3627
3645
  [enabled, normalizeItems, onItemsChange, session]
3628
3646
  );
3647
+ react.useEffect(() => {
3648
+ if (items.length > 0) {
3649
+ hasEverPropagatedItemsRef.current = true;
3650
+ }
3651
+ }, [items.length]);
3629
3652
  react.useEffect(() => {
3630
3653
  if (!realtimeEnabled || !onItemsChange || !session?.document) return;
3631
3654
  if (documentUpdatedByClientId === connectionClientId) return;
@@ -3639,7 +3662,18 @@ function useRealtimeCanvasDocument(options) {
3639
3662
  if (cancelled) return;
3640
3663
  if (inFlightRevisionRef.current !== documentRevision) return;
3641
3664
  lastAppliedRevisionRef.current = documentRevision;
3642
- if (resolvedItems.length === 0 && hasEverPropagatedItemsRef.current) {
3665
+ const hasLocalItems = items.length > 0;
3666
+ const hasPendingLocalChanges = hasLocalOfflineDraft || hasPendingDocumentSync;
3667
+ if (resolvedItems.length === 0 && (hasEverPropagatedItemsRef.current || hasLocalItems || hasPendingLocalChanges)) {
3668
+ if (hasLocalItems) {
3669
+ const normalizedLocalItems = normalizeItems ? normalizeItems(items) : [...items];
3670
+ session.remoteAdapter.send?.(normalizedLocalItems);
3671
+ }
3672
+ return;
3673
+ }
3674
+ if (hasPendingLocalChanges && hasLocalItems && hasMissingLocalItems(items, resolvedItems)) {
3675
+ const normalizedLocalItems = normalizeItems ? normalizeItems(items) : [...items];
3676
+ session.remoteAdapter.send?.(normalizedLocalItems);
3643
3677
  return;
3644
3678
  }
3645
3679
  if (resolvedItems.length > 0) {
@@ -3662,8 +3696,13 @@ function useRealtimeCanvasDocument(options) {
3662
3696
  documentItems,
3663
3697
  documentRevision,
3664
3698
  documentUpdatedByClientId,
3699
+ hasLocalOfflineDraft,
3700
+ hasPendingDocumentSync,
3701
+ items,
3702
+ normalizeItems,
3665
3703
  onItemsChange,
3666
3704
  realtimeEnabled,
3705
+ session?.remoteAdapter,
3667
3706
  session?.document
3668
3707
  ]);
3669
3708
  return react.useMemo(