canvu-react 0.3.12 → 0.3.14

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
@@ -2072,6 +2072,10 @@ function useRealtimeSession(options) {
2072
2072
  }, []);
2073
2073
  const applyDocument = useCallback(
2074
2074
  (snapshot, options2) => {
2075
+ const currentSnapshot = latestDocumentRef.current;
2076
+ if (currentSnapshot && currentSnapshot.revision === snapshot.revision && currentSnapshot.updatedByClientId === snapshot.updatedByClientId && sameSerializedItems(currentSnapshot.items, snapshot.items)) {
2077
+ return;
2078
+ }
2075
2079
  currentRevisionRef.current = snapshot.revision;
2076
2080
  latestDocumentRef.current = snapshot;
2077
2081
  setDocument(snapshot);
@@ -2995,7 +2999,12 @@ function useRealtimeCanvasDocument(options) {
2995
2999
  } = options;
2996
3000
  const [loading, setLoading] = useState(false);
2997
3001
  const lastAppliedRevisionRef = useRef(null);
3002
+ const inFlightRevisionRef = useRef(null);
2998
3003
  const realtimeEnabled = enabled && session != null;
3004
+ const documentRevision = session?.document?.revision ?? null;
3005
+ const documentItems = session?.document?.items;
3006
+ const documentUpdatedByClientId = session?.document?.updatedByClientId ?? null;
3007
+ const connectionClientId = session?.connection.clientId ?? null;
2999
3008
  const applyIncomingItems = useCallback(
3000
3009
  async (nextItems) => {
3001
3010
  const normalizedItems = normalizeItems ? normalizeItems(nextItems) : [...nextItems];
@@ -3018,22 +3027,38 @@ function useRealtimeCanvasDocument(options) {
3018
3027
  );
3019
3028
  useEffect(() => {
3020
3029
  if (!realtimeEnabled || !onItemsChange || !session?.document) return;
3021
- if (session.document.updatedByClientId === session.connection.clientId) return;
3022
- if (lastAppliedRevisionRef.current === session.document.revision) return;
3030
+ if (documentUpdatedByClientId === connectionClientId) return;
3031
+ if (documentRevision == null) return;
3032
+ if (lastAppliedRevisionRef.current === documentRevision) return;
3033
+ if (inFlightRevisionRef.current === documentRevision) return;
3034
+ inFlightRevisionRef.current = documentRevision;
3023
3035
  let cancelled = false;
3024
3036
  setLoading(true);
3025
- void applyIncomingItems(session.document.items).then((resolvedItems) => {
3037
+ void applyIncomingItems(documentItems ?? []).then((resolvedItems) => {
3026
3038
  if (cancelled) return;
3027
- lastAppliedRevisionRef.current = session.document?.revision ?? null;
3039
+ if (inFlightRevisionRef.current !== documentRevision) return;
3040
+ lastAppliedRevisionRef.current = documentRevision;
3028
3041
  onItemsChange(resolvedItems);
3029
3042
  }).finally(() => {
3043
+ if (inFlightRevisionRef.current === documentRevision) {
3044
+ inFlightRevisionRef.current = null;
3045
+ }
3030
3046
  if (cancelled) return;
3031
3047
  setLoading(false);
3032
3048
  });
3033
3049
  return () => {
3034
3050
  cancelled = true;
3035
3051
  };
3036
- }, [applyIncomingItems, realtimeEnabled, onItemsChange, session]);
3052
+ }, [
3053
+ applyIncomingItems,
3054
+ connectionClientId,
3055
+ documentItems,
3056
+ documentRevision,
3057
+ documentUpdatedByClientId,
3058
+ onItemsChange,
3059
+ realtimeEnabled,
3060
+ session?.document
3061
+ ]);
3037
3062
  return useMemo(
3038
3063
  () => ({
3039
3064
  items,