@liveblocks/react 1.5.0-test1 → 1.5.0-test3

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/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { detectDupes } from "@liveblocks/core";
5
5
 
6
6
  // src/version.ts
7
7
  var PKG_NAME = "@liveblocks/react";
8
- var PKG_VERSION = "1.5.0-test1";
8
+ var PKG_VERSION = "1.5.0-test3";
9
9
  var PKG_FORMAT = "esm";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -628,13 +628,13 @@ function useDebounce(value, delay = DEFAULT_DELAY) {
628
628
  }
629
629
 
630
630
  // src/lib/use-async-cache.ts
631
- import { useCallback, useEffect as useEffect4, useMemo, useRef as useRef3 } from "react";
631
+ import { useCallback, useEffect as useEffect4, useMemo, useRef as useRef2 } from "react";
632
632
  import { useSyncExternalStore as useSyncExternalStore2 } from "use-sync-external-store/shim/index.js";
633
633
 
634
634
  // src/lib/use-initial.ts
635
- import { useRef as useRef2 } from "react";
635
+ import { useState as useState3 } from "react";
636
636
  function useInitial(value) {
637
- return useRef2(value).current;
637
+ return useState3(value)[0];
638
638
  }
639
639
 
640
640
  // src/lib/use-async-cache.ts
@@ -665,7 +665,7 @@ function useAsyncCache(cache, key, options) {
665
665
  );
666
666
  const revalidate = useCallback(() => cacheItem?.revalidate(), [cacheItem]);
667
667
  const state = useSyncExternalStore2(subscribe, getState, getState);
668
- const previousData = useRef3();
668
+ const previousData = useRef2();
669
669
  let data = state.data;
670
670
  useEffect4(() => {
671
671
  previousData.current = { key, data: state.data };
@@ -705,6 +705,16 @@ function useAsyncCache(cache, key, options) {
705
705
  };
706
706
  }
707
707
 
708
+ // src/lib/use-latest.ts
709
+ import { useEffect as useEffect5, useRef as useRef3 } from "react";
710
+ function useLatest(value) {
711
+ const ref = useRef3(value);
712
+ useEffect5(() => {
713
+ ref.current = value;
714
+ }, [value]);
715
+ return ref;
716
+ }
717
+
708
718
  // src/lib/use-rerender.ts
709
719
  import { useReducer } from "react";
710
720
  function useRerender() {
@@ -732,7 +742,7 @@ var missing_unstable_batchedUpdates = (reactVersion, roomId) => `We noticed you\
732
742
  ...
733
743
  </RoomProvider>
734
744
 
735
- Why? Please see https://liveblocks.io/docs/guides/troubleshooting#stale-props-zombie-child for more information`;
745
+ Why? Please see https://liveblocks.io/docs/platform/troubleshooting#stale-props-zombie-child for more information`;
736
746
  var superfluous_unstable_batchedUpdates = "You don\u2019t need to pass unstable_batchedUpdates to RoomProvider anymore, since you\u2019re on React 18+ already.";
737
747
  function useSyncExternalStore3(s, gs, gss) {
738
748
  return useSyncExternalStoreWithSelector(s, gs, gss, identity);
@@ -797,40 +807,35 @@ function useRoomContextBundle() {
797
807
  }
798
808
  return bundle;
799
809
  }
800
- var lastInstanceId = 0;
801
810
  function createRoomContext(client, options) {
802
811
  const RoomContext = React2.createContext(null);
803
- const roomCache = /* @__PURE__ */ new Map();
804
- function stableEnterRoom(instanceId, roomId, options2) {
805
- const key = `${instanceId}:${roomId}`;
806
- const cached = roomCache.get(key);
807
- if (cached)
808
- return cached;
809
- const rv = client.enterRoom(
810
- roomId,
811
- options2
812
- );
813
- const origLeave = rv.leave;
814
- rv.leave = () => {
815
- origLeave();
816
- roomCache.delete(key);
817
- };
818
- roomCache.set(key, rv);
819
- return rv;
820
- }
821
812
  function RoomProviderOuter(props) {
822
- const instanceId = React2.useRef(`p${++lastInstanceId}`).current;
823
- return /* @__PURE__ */ React2.createElement(RoomProviderInner, { instanceId, ...props });
813
+ const [cache] = React2.useState(
814
+ () => /* @__PURE__ */ new Map()
815
+ );
816
+ const stableEnterRoom = React2.useCallback(
817
+ (roomId, options2) => {
818
+ const cached = cache.get(roomId);
819
+ if (cached)
820
+ return cached;
821
+ const rv = client.enterRoom(
822
+ roomId,
823
+ options2
824
+ );
825
+ const origLeave = rv.leave;
826
+ rv.leave = () => {
827
+ origLeave();
828
+ cache.delete(roomId);
829
+ };
830
+ cache.set(roomId, rv);
831
+ return rv;
832
+ },
833
+ [cache]
834
+ );
835
+ return /* @__PURE__ */ React2.createElement(RoomProviderInner, { ...props, stableEnterRoom });
824
836
  }
825
837
  function RoomProviderInner(props) {
826
- const {
827
- instanceId,
828
- id: roomId,
829
- initialPresence,
830
- initialStorage,
831
- unstable_batchedUpdates
832
- } = props;
833
- const autoConnect = props.autoConnect ?? props.shouldInitiallyConnect ?? typeof window !== "undefined";
838
+ const { id: roomId, stableEnterRoom } = props;
834
839
  if (process.env.NODE_ENV !== "production") {
835
840
  if (!roomId) {
836
841
  throw new Error(
@@ -851,31 +856,24 @@ function createRoomContext(client, options) {
851
856
  superfluous_unstable_batchedUpdates
852
857
  );
853
858
  }
854
- const frozen = useInitial({
855
- instanceId,
856
- initialPresence,
857
- initialStorage,
858
- unstable_batchedUpdates,
859
- autoConnect
859
+ const frozenProps = useInitial({
860
+ initialPresence: props.initialPresence,
861
+ initialStorage: props.initialStorage,
862
+ unstable_batchedUpdates: props.unstable_batchedUpdates,
863
+ autoConnect: props.autoConnect ?? props.shouldInitiallyConnect ?? typeof window !== "undefined"
860
864
  });
861
865
  const [{ room }, setRoomLeavePair] = React2.useState(
862
- () => stableEnterRoom(frozen.instanceId, roomId, {
863
- initialPresence: frozen.initialPresence,
864
- initialStorage: frozen.initialStorage,
865
- autoConnect: false,
866
- unstable_batchedUpdates: frozen.unstable_batchedUpdates
866
+ () => stableEnterRoom(roomId, {
867
+ ...frozenProps,
868
+ autoConnect: false
869
+ // Deliberately using false here on the first render, see below
867
870
  })
868
871
  );
869
872
  React2.useEffect(() => {
870
- const pair = stableEnterRoom(frozen.instanceId, roomId, {
871
- initialPresence: frozen.initialPresence,
872
- initialStorage: frozen.initialStorage,
873
- autoConnect: false,
874
- unstable_batchedUpdates: frozen.unstable_batchedUpdates
875
- });
873
+ const pair = stableEnterRoom(roomId, frozenProps);
876
874
  setRoomLeavePair(pair);
877
875
  const { room: room2, leave } = pair;
878
- if (frozen.autoConnect) {
876
+ if (frozenProps.autoConnect) {
879
877
  room2.connect();
880
878
  }
881
879
  return () => {
@@ -885,7 +883,7 @@ function createRoomContext(client, options) {
885
883
  }
886
884
  leave();
887
885
  };
888
- }, [roomId, frozen]);
886
+ }, [roomId, frozenProps, stableEnterRoom]);
889
887
  return /* @__PURE__ */ React2.createElement(RoomContext.Provider, { value: room }, /* @__PURE__ */ React2.createElement(
890
888
  ContextBundle.Provider,
891
889
  {
@@ -995,42 +993,41 @@ function createRoomContext(client, options) {
995
993
  [room]
996
994
  );
997
995
  }
996
+ function useOthersListener(callback) {
997
+ const room = useRoom();
998
+ const savedCallback = useLatest(callback);
999
+ React2.useEffect(
1000
+ () => room.events.others.subscribe((event) => savedCallback.current(event)),
1001
+ [room, savedCallback]
1002
+ );
1003
+ }
998
1004
  function useLostConnectionListener(callback) {
999
1005
  const room = useRoom();
1000
- const savedCallback = React2.useRef(callback);
1001
- React2.useEffect(() => {
1002
- savedCallback.current = callback;
1003
- });
1006
+ const savedCallback = useLatest(callback);
1004
1007
  React2.useEffect(
1005
1008
  () => room.events.lostConnection.subscribe(
1006
1009
  (event) => savedCallback.current(event)
1007
1010
  ),
1008
- [room]
1011
+ [room, savedCallback]
1009
1012
  );
1010
1013
  }
1011
1014
  function useErrorListener(callback) {
1012
1015
  const room = useRoom();
1013
- const savedCallback = React2.useRef(callback);
1014
- React2.useEffect(() => {
1015
- savedCallback.current = callback;
1016
- });
1016
+ const savedCallback = useLatest(callback);
1017
1017
  React2.useEffect(
1018
1018
  () => room.events.error.subscribe((e) => savedCallback.current(e)),
1019
- [room]
1019
+ [room, savedCallback]
1020
1020
  );
1021
1021
  }
1022
1022
  function useEventListener(callback) {
1023
1023
  const room = useRoom();
1024
- const savedCallback = React2.useRef(callback);
1025
- React2.useEffect(() => {
1026
- savedCallback.current = callback;
1027
- });
1024
+ const savedCallback = useLatest(callback);
1028
1025
  React2.useEffect(() => {
1029
1026
  const listener = (eventData) => {
1030
1027
  savedCallback.current(eventData);
1031
1028
  };
1032
1029
  return room.events.customEvent.subscribe(listener);
1033
- }, [room]);
1030
+ }, [room, savedCallback]);
1034
1031
  }
1035
1032
  function useSelf(maybeSelector, isEqual) {
1036
1033
  const room = useRoom();
@@ -1398,6 +1395,7 @@ function createRoomContext(client, options) {
1398
1395
  useStatus,
1399
1396
  useBatch,
1400
1397
  useBroadcastEvent,
1398
+ useOthersListener,
1401
1399
  useLostConnectionListener,
1402
1400
  useErrorListener,
1403
1401
  useEventListener,
@@ -1436,6 +1434,7 @@ function createRoomContext(client, options) {
1436
1434
  useStatus,
1437
1435
  useBatch,
1438
1436
  useBroadcastEvent,
1437
+ useOthersListener,
1439
1438
  useLostConnectionListener,
1440
1439
  useErrorListener,
1441
1440
  useEventListener,