@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.d.mts CHANGED
@@ -2,7 +2,7 @@ import * as React$1 from 'react';
2
2
  import { ReactElement, ReactNode } from 'react';
3
3
  import * as _liveblocks_core from '@liveblocks/core';
4
4
  import { CommentBody, BaseMetadata, ThreadData, Resolve, ToImmutable, RoomEventMessage, CommentData, RoomInitializers } from '@liveblocks/core';
5
- import { JsonObject, LsonObject, BaseUserMeta, LiveObject, User, Json, Room, Status, BroadcastOptions, LostConnectionEvent, History, Client } from '@liveblocks/client';
5
+ import { JsonObject, LsonObject, BaseUserMeta, LiveObject, User, Json, Room, Status, BroadcastOptions, OthersEvent, LostConnectionEvent, History, Client } from '@liveblocks/client';
6
6
  export { Json, JsonObject, shallow } from '@liveblocks/client';
7
7
 
8
8
  declare type Props = {
@@ -204,6 +204,19 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
204
204
  * broadcast({ type: "CUSTOM_EVENT", data: { x: 0, y: 0 } });
205
205
  */
206
206
  useBroadcastEvent(): (event: TRoomEvent, options?: BroadcastOptions) => void;
207
+ /**
208
+ * Get informed when users enter or leave the room, as an event.
209
+ *
210
+ * @example
211
+ * useOthersListener({ type, user, others }) => {
212
+ * if (type === 'enter') {
213
+ * // `user` has joined the room
214
+ * } else if (type === 'leave') {
215
+ * // `user` has left the room
216
+ * }
217
+ * })
218
+ */
219
+ useOthersListener(callback: (event: OthersEvent<TPresence, TUserMeta>) => void): void;
207
220
  /**
208
221
  * Get informed when reconnecting to the Liveblocks servers is taking
209
222
  * longer than usual. This typically is a sign of a client that has lost
@@ -226,7 +239,8 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
226
239
  */
227
240
  useLostConnectionListener(callback: (event: LostConnectionEvent) => void): void;
228
241
  /**
229
- * useErrorListener is a react hook that lets you react to potential room connection errors.
242
+ * useErrorListener is a React hook that allows you to respond to potential room
243
+ * connection errors.
230
244
  *
231
245
  * @example
232
246
  * useErrorListener(er => {
@@ -235,7 +249,8 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
235
249
  */
236
250
  useErrorListener(callback: (err: Error) => void): void;
237
251
  /**
238
- * useEventListener is a react hook that lets you react to event broadcasted by other users in the room.
252
+ * useEventListener is a React hook that allows you to respond to events broadcast
253
+ * by other users in the room.
239
254
  *
240
255
  * @example
241
256
  * useEventListener(({ connectionId, event }) => {
@@ -792,6 +807,7 @@ declare function useRoomContextBundle(): {
792
807
  useStatus: () => Status;
793
808
  useBatch: <T>() => (callback: () => T) => T;
794
809
  useBroadcastEvent: () => (event: never, options?: BroadcastOptions | undefined) => void;
810
+ useOthersListener: (callback: (event: OthersEvent<JsonObject, BaseUserMeta>) => void) => void;
795
811
  useLostConnectionListener: (callback: (event: LostConnectionEvent) => void) => void;
796
812
  useErrorListener: (callback: (err: Error) => void) => void;
797
813
  useEventListener: (callback: (data: RoomEventMessage<JsonObject, BaseUserMeta, never>) => void) => void;
@@ -1149,6 +1165,7 @@ declare function useRoomContextBundle(): {
1149
1165
  useStatus: () => Status;
1150
1166
  useBatch: <T>() => (callback: () => T) => T;
1151
1167
  useBroadcastEvent: () => (event: never, options?: BroadcastOptions | undefined) => void;
1168
+ useOthersListener: (callback: (event: OthersEvent<JsonObject, BaseUserMeta>) => void) => void;
1152
1169
  useLostConnectionListener: (callback: (event: LostConnectionEvent) => void) => void;
1153
1170
  useErrorListener: (callback: (err: Error) => void) => void;
1154
1171
  useEventListener: (callback: (data: RoomEventMessage<JsonObject, BaseUserMeta, never>) => void) => void;
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ import * as React$1 from 'react';
2
2
  import { ReactElement, ReactNode } from 'react';
3
3
  import * as _liveblocks_core from '@liveblocks/core';
4
4
  import { CommentBody, BaseMetadata, ThreadData, Resolve, ToImmutable, RoomEventMessage, CommentData, RoomInitializers } from '@liveblocks/core';
5
- import { JsonObject, LsonObject, BaseUserMeta, LiveObject, User, Json, Room, Status, BroadcastOptions, LostConnectionEvent, History, Client } from '@liveblocks/client';
5
+ import { JsonObject, LsonObject, BaseUserMeta, LiveObject, User, Json, Room, Status, BroadcastOptions, OthersEvent, LostConnectionEvent, History, Client } from '@liveblocks/client';
6
6
  export { Json, JsonObject, shallow } from '@liveblocks/client';
7
7
 
8
8
  declare type Props = {
@@ -204,6 +204,19 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
204
204
  * broadcast({ type: "CUSTOM_EVENT", data: { x: 0, y: 0 } });
205
205
  */
206
206
  useBroadcastEvent(): (event: TRoomEvent, options?: BroadcastOptions) => void;
207
+ /**
208
+ * Get informed when users enter or leave the room, as an event.
209
+ *
210
+ * @example
211
+ * useOthersListener({ type, user, others }) => {
212
+ * if (type === 'enter') {
213
+ * // `user` has joined the room
214
+ * } else if (type === 'leave') {
215
+ * // `user` has left the room
216
+ * }
217
+ * })
218
+ */
219
+ useOthersListener(callback: (event: OthersEvent<TPresence, TUserMeta>) => void): void;
207
220
  /**
208
221
  * Get informed when reconnecting to the Liveblocks servers is taking
209
222
  * longer than usual. This typically is a sign of a client that has lost
@@ -226,7 +239,8 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
226
239
  */
227
240
  useLostConnectionListener(callback: (event: LostConnectionEvent) => void): void;
228
241
  /**
229
- * useErrorListener is a react hook that lets you react to potential room connection errors.
242
+ * useErrorListener is a React hook that allows you to respond to potential room
243
+ * connection errors.
230
244
  *
231
245
  * @example
232
246
  * useErrorListener(er => {
@@ -235,7 +249,8 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
235
249
  */
236
250
  useErrorListener(callback: (err: Error) => void): void;
237
251
  /**
238
- * useEventListener is a react hook that lets you react to event broadcasted by other users in the room.
252
+ * useEventListener is a React hook that allows you to respond to events broadcast
253
+ * by other users in the room.
239
254
  *
240
255
  * @example
241
256
  * useEventListener(({ connectionId, event }) => {
@@ -792,6 +807,7 @@ declare function useRoomContextBundle(): {
792
807
  useStatus: () => Status;
793
808
  useBatch: <T>() => (callback: () => T) => T;
794
809
  useBroadcastEvent: () => (event: never, options?: BroadcastOptions | undefined) => void;
810
+ useOthersListener: (callback: (event: OthersEvent<JsonObject, BaseUserMeta>) => void) => void;
795
811
  useLostConnectionListener: (callback: (event: LostConnectionEvent) => void) => void;
796
812
  useErrorListener: (callback: (err: Error) => void) => void;
797
813
  useEventListener: (callback: (data: RoomEventMessage<JsonObject, BaseUserMeta, never>) => void) => void;
@@ -1149,6 +1165,7 @@ declare function useRoomContextBundle(): {
1149
1165
  useStatus: () => Status;
1150
1166
  useBatch: <T>() => (callback: () => T) => T;
1151
1167
  useBroadcastEvent: () => (event: never, options?: BroadcastOptions | undefined) => void;
1168
+ useOthersListener: (callback: (event: OthersEvent<JsonObject, BaseUserMeta>) => void) => void;
1152
1169
  useLostConnectionListener: (callback: (event: LostConnectionEvent) => void) => void;
1153
1170
  useErrorListener: (callback: (err: Error) => void) => void;
1154
1171
  useEventListener: (callback: (data: RoomEventMessage<JsonObject, BaseUserMeta, never>) => void) => void;
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var _core = require('@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 = "cjs";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -634,7 +634,7 @@ function useDebounce(value, delay = DEFAULT_DELAY) {
634
634
  // src/lib/use-initial.ts
635
635
 
636
636
  function useInitial(value) {
637
- return _react.useRef.call(void 0, value).current;
637
+ return _react.useState.call(void 0, value)[0];
638
638
  }
639
639
 
640
640
  // src/lib/use-async-cache.ts
@@ -705,6 +705,16 @@ function useAsyncCache(cache, key, options) {
705
705
  };
706
706
  }
707
707
 
708
+ // src/lib/use-latest.ts
709
+
710
+ function useLatest(value) {
711
+ const ref = _react.useRef.call(void 0, value);
712
+ _react.useEffect.call(void 0, () => {
713
+ ref.current = value;
714
+ }, [value]);
715
+ return ref;
716
+ }
717
+
708
718
  // src/lib/use-rerender.ts
709
719
 
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 _withselectorjs.useSyncExternalStoreWithSelector.call(void 0, 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 = _nullishCoalesce(_nullishCoalesce(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: _nullishCoalesce(_nullishCoalesce(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,