@liveblocks/react 1.4.7 → 1.5.0-test1

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, Others, Json, Room, Status, BroadcastOptions, LostConnectionEvent, History, Client } from '@liveblocks/client';
5
+ import { JsonObject, LsonObject, BaseUserMeta, LiveObject, User, Json, Room, Status, BroadcastOptions, LostConnectionEvent, History, Client } from '@liveblocks/client';
6
6
  export { Json, JsonObject, shallow } from '@liveblocks/client';
7
7
 
8
8
  declare type Props = {
@@ -133,6 +133,8 @@ declare type RoomProviderProps<TPresence extends JsonObject, TStorage extends Ls
133
133
  * meaning the RoomProvider tries to connect to Liveblocks servers
134
134
  * only on the client side.
135
135
  */
136
+ autoConnect?: boolean;
137
+ /** @deprecated Renamed to `autoConnect` */
136
138
  shouldInitiallyConnect?: boolean;
137
139
  /**
138
140
  * If you're on React 17 or lower, pass in a reference to
@@ -158,7 +160,7 @@ declare type OmitFirstArg<F> = F extends (first: any, ...rest: infer A) => infer
158
160
  declare type MutationContext<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta> = {
159
161
  storage: LiveObject<TStorage>;
160
162
  self: User<TPresence, TUserMeta>;
161
- others: Others<TPresence, TUserMeta>;
163
+ others: readonly User<TPresence, TUserMeta>[];
162
164
  setMyPresence: (patch: Partial<TPresence>, options?: {
163
165
  addToHistory: boolean;
164
166
  }) => void;
@@ -369,7 +371,7 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
369
371
  * </>
370
372
  * )
371
373
  */
372
- useOthers(): Others<TPresence, TUserMeta>;
374
+ useOthers(): readonly User<TPresence, TUserMeta>[];
373
375
  /**
374
376
  * Extract arbitrary data based on all the users currently connected in the
375
377
  * room (except yourself).
@@ -391,7 +393,7 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
391
393
  * const someoneIsTyping = useOthers(users => users.some(u => u.presence.isTyping));
392
394
  *
393
395
  */
394
- useOthers<T>(selector: (others: Others<TPresence, TUserMeta>) => T, isEqual?: (prev: T, curr: T) => boolean): T;
396
+ useOthers<T>(selector: (others: readonly User<TPresence, TUserMeta>[]) => T, isEqual?: (prev: T, curr: T) => boolean): T;
395
397
  /**
396
398
  * Returns an array of connection IDs. This matches the values you'll get by
397
399
  * using the `useOthers()` hook.
@@ -780,6 +782,7 @@ declare function useRoomContextBundle(): {
780
782
  RoomProvider: (props: {
781
783
  id: string;
782
784
  children: React$1.ReactNode;
785
+ autoConnect?: boolean | undefined;
783
786
  shouldInitiallyConnect?: boolean | undefined;
784
787
  unstable_batchedUpdates?: ((cb: () => void) => void) | undefined;
785
788
  initialPresence: JsonObject | ((roomId: string) => JsonObject);
@@ -806,8 +809,8 @@ declare function useRoomContextBundle(): {
806
809
  } | undefined) => void;
807
810
  useMutation: <F extends (context: MutationContext<JsonObject, LsonObject, BaseUserMeta>, ...args: any[]) => any>(callback: F, deps: readonly unknown[]) => OmitFirstArg<F>;
808
811
  useOthers: {
809
- (): _liveblocks_core.Others<JsonObject, BaseUserMeta>;
810
- <T_1>(selector: (others: _liveblocks_core.Others<JsonObject, BaseUserMeta>) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
812
+ (): readonly User<JsonObject, BaseUserMeta>[];
813
+ <T_1>(selector: (others: readonly User<JsonObject, BaseUserMeta>[]) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
811
814
  };
812
815
  useOthersConnectionIds: () => readonly number[];
813
816
  useOthersMapped: <T_2>(itemSelector: (other: User<JsonObject, BaseUserMeta>) => T_2, itemIsEqual?: ((prev: T_2, curr: T_2) => boolean) | undefined) => readonly (readonly [connectionId: number, data: T_2])[];
@@ -1136,6 +1139,7 @@ declare function useRoomContextBundle(): {
1136
1139
  RoomProvider: (props: {
1137
1140
  id: string;
1138
1141
  children: React$1.ReactNode;
1142
+ autoConnect?: boolean | undefined;
1139
1143
  shouldInitiallyConnect?: boolean | undefined;
1140
1144
  unstable_batchedUpdates?: ((cb: () => void) => void) | undefined;
1141
1145
  initialPresence: JsonObject | ((roomId: string) => JsonObject);
@@ -1162,8 +1166,8 @@ declare function useRoomContextBundle(): {
1162
1166
  } | undefined) => void;
1163
1167
  useMutation: <F extends (context: MutationContext<JsonObject, LsonObject, BaseUserMeta>, ...args: any[]) => any>(callback: F, deps: readonly unknown[]) => OmitFirstArg<F>;
1164
1168
  useOthers: {
1165
- (): _liveblocks_core.Others<JsonObject, BaseUserMeta>;
1166
- <T_1>(selector: (others: _liveblocks_core.Others<JsonObject, BaseUserMeta>) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
1169
+ (): readonly User<JsonObject, BaseUserMeta>[];
1170
+ <T_1>(selector: (others: readonly User<JsonObject, BaseUserMeta>[]) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
1167
1171
  };
1168
1172
  useOthersConnectionIds: () => readonly number[];
1169
1173
  useOthersMapped: <T_2>(itemSelector: (other: User<JsonObject, BaseUserMeta>) => T_2, itemIsEqual?: ((prev: T_2, curr: T_2) => boolean) | undefined) => readonly (readonly [connectionId: number, data: T_2])[];
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, Others, Json, Room, Status, BroadcastOptions, LostConnectionEvent, History, Client } from '@liveblocks/client';
5
+ import { JsonObject, LsonObject, BaseUserMeta, LiveObject, User, Json, Room, Status, BroadcastOptions, LostConnectionEvent, History, Client } from '@liveblocks/client';
6
6
  export { Json, JsonObject, shallow } from '@liveblocks/client';
7
7
 
8
8
  declare type Props = {
@@ -133,6 +133,8 @@ declare type RoomProviderProps<TPresence extends JsonObject, TStorage extends Ls
133
133
  * meaning the RoomProvider tries to connect to Liveblocks servers
134
134
  * only on the client side.
135
135
  */
136
+ autoConnect?: boolean;
137
+ /** @deprecated Renamed to `autoConnect` */
136
138
  shouldInitiallyConnect?: boolean;
137
139
  /**
138
140
  * If you're on React 17 or lower, pass in a reference to
@@ -158,7 +160,7 @@ declare type OmitFirstArg<F> = F extends (first: any, ...rest: infer A) => infer
158
160
  declare type MutationContext<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta> = {
159
161
  storage: LiveObject<TStorage>;
160
162
  self: User<TPresence, TUserMeta>;
161
- others: Others<TPresence, TUserMeta>;
163
+ others: readonly User<TPresence, TUserMeta>[];
162
164
  setMyPresence: (patch: Partial<TPresence>, options?: {
163
165
  addToHistory: boolean;
164
166
  }) => void;
@@ -369,7 +371,7 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
369
371
  * </>
370
372
  * )
371
373
  */
372
- useOthers(): Others<TPresence, TUserMeta>;
374
+ useOthers(): readonly User<TPresence, TUserMeta>[];
373
375
  /**
374
376
  * Extract arbitrary data based on all the users currently connected in the
375
377
  * room (except yourself).
@@ -391,7 +393,7 @@ declare type RoomContextBundleShared<TPresence extends JsonObject, TStorage exte
391
393
  * const someoneIsTyping = useOthers(users => users.some(u => u.presence.isTyping));
392
394
  *
393
395
  */
394
- useOthers<T>(selector: (others: Others<TPresence, TUserMeta>) => T, isEqual?: (prev: T, curr: T) => boolean): T;
396
+ useOthers<T>(selector: (others: readonly User<TPresence, TUserMeta>[]) => T, isEqual?: (prev: T, curr: T) => boolean): T;
395
397
  /**
396
398
  * Returns an array of connection IDs. This matches the values you'll get by
397
399
  * using the `useOthers()` hook.
@@ -780,6 +782,7 @@ declare function useRoomContextBundle(): {
780
782
  RoomProvider: (props: {
781
783
  id: string;
782
784
  children: React$1.ReactNode;
785
+ autoConnect?: boolean | undefined;
783
786
  shouldInitiallyConnect?: boolean | undefined;
784
787
  unstable_batchedUpdates?: ((cb: () => void) => void) | undefined;
785
788
  initialPresence: JsonObject | ((roomId: string) => JsonObject);
@@ -806,8 +809,8 @@ declare function useRoomContextBundle(): {
806
809
  } | undefined) => void;
807
810
  useMutation: <F extends (context: MutationContext<JsonObject, LsonObject, BaseUserMeta>, ...args: any[]) => any>(callback: F, deps: readonly unknown[]) => OmitFirstArg<F>;
808
811
  useOthers: {
809
- (): _liveblocks_core.Others<JsonObject, BaseUserMeta>;
810
- <T_1>(selector: (others: _liveblocks_core.Others<JsonObject, BaseUserMeta>) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
812
+ (): readonly User<JsonObject, BaseUserMeta>[];
813
+ <T_1>(selector: (others: readonly User<JsonObject, BaseUserMeta>[]) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
811
814
  };
812
815
  useOthersConnectionIds: () => readonly number[];
813
816
  useOthersMapped: <T_2>(itemSelector: (other: User<JsonObject, BaseUserMeta>) => T_2, itemIsEqual?: ((prev: T_2, curr: T_2) => boolean) | undefined) => readonly (readonly [connectionId: number, data: T_2])[];
@@ -1136,6 +1139,7 @@ declare function useRoomContextBundle(): {
1136
1139
  RoomProvider: (props: {
1137
1140
  id: string;
1138
1141
  children: React$1.ReactNode;
1142
+ autoConnect?: boolean | undefined;
1139
1143
  shouldInitiallyConnect?: boolean | undefined;
1140
1144
  unstable_batchedUpdates?: ((cb: () => void) => void) | undefined;
1141
1145
  initialPresence: JsonObject | ((roomId: string) => JsonObject);
@@ -1162,8 +1166,8 @@ declare function useRoomContextBundle(): {
1162
1166
  } | undefined) => void;
1163
1167
  useMutation: <F extends (context: MutationContext<JsonObject, LsonObject, BaseUserMeta>, ...args: any[]) => any>(callback: F, deps: readonly unknown[]) => OmitFirstArg<F>;
1164
1168
  useOthers: {
1165
- (): _liveblocks_core.Others<JsonObject, BaseUserMeta>;
1166
- <T_1>(selector: (others: _liveblocks_core.Others<JsonObject, BaseUserMeta>) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
1169
+ (): readonly User<JsonObject, BaseUserMeta>[];
1170
+ <T_1>(selector: (others: readonly User<JsonObject, BaseUserMeta>[]) => T_1, isEqual?: ((prev: T_1, curr: T_1) => boolean) | undefined): T_1;
1167
1171
  };
1168
1172
  useOthersConnectionIds: () => readonly number[];
1169
1173
  useOthersMapped: <T_2>(itemSelector: (other: User<JsonObject, BaseUserMeta>) => T_2, itemIsEqual?: ((prev: T_2, curr: T_2) => boolean) | undefined) => readonly (readonly [connectionId: number, data: T_2])[];
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.4.7";
8
+ var PKG_VERSION = "1.5.0-test1";
9
9
  var PKG_FORMAT = "cjs";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -744,9 +744,6 @@ function alwaysEmptyList() {
744
744
  function alwaysNull() {
745
745
  return null;
746
746
  }
747
- function alwaysConnecting() {
748
- return "connecting";
749
- }
750
747
  function makeMutationContext(room) {
751
748
  const errmsg = "This mutation cannot be used until connected to the Liveblocks room";
752
749
  return {
@@ -800,16 +797,40 @@ function useRoomContextBundle() {
800
797
  }
801
798
  return bundle;
802
799
  }
800
+ var lastInstanceId = 0;
803
801
  function createRoomContext(client, options) {
804
802
  const RoomContext = React2.createContext(null);
805
- function RoomProvider(props) {
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
+ function RoomProviderOuter(props) {
822
+ const instanceId = React2.useRef(`p${++lastInstanceId}`).current;
823
+ return /* @__PURE__ */ React2.createElement(RoomProviderInner, { instanceId, ...props });
824
+ }
825
+ function RoomProviderInner(props) {
806
826
  const {
827
+ instanceId,
807
828
  id: roomId,
808
829
  initialPresence,
809
830
  initialStorage,
810
- unstable_batchedUpdates,
811
- shouldInitiallyConnect
831
+ unstable_batchedUpdates
812
832
  } = props;
833
+ const autoConnect = _nullishCoalesce(_nullishCoalesce(props.autoConnect, () => ( props.shouldInitiallyConnect)), () => ( typeof window !== "undefined"));
813
834
  if (process.env.NODE_ENV !== "production") {
814
835
  if (!roomId) {
815
836
  throw new Error(
@@ -831,36 +852,38 @@ function createRoomContext(client, options) {
831
852
  );
832
853
  }
833
854
  const frozen = useInitial({
855
+ instanceId,
834
856
  initialPresence,
835
857
  initialStorage,
836
858
  unstable_batchedUpdates,
837
- shouldInitiallyConnect: shouldInitiallyConnect === void 0 ? typeof window !== "undefined" : shouldInitiallyConnect
859
+ autoConnect
838
860
  });
839
- const [room, setRoom] = React2.useState(
840
- () => client.enter(roomId, {
861
+ const [{ room }, setRoomLeavePair] = React2.useState(
862
+ () => stableEnterRoom(frozen.instanceId, roomId, {
841
863
  initialPresence: frozen.initialPresence,
842
864
  initialStorage: frozen.initialStorage,
843
- shouldInitiallyConnect: frozen.shouldInitiallyConnect,
865
+ autoConnect: false,
844
866
  unstable_batchedUpdates: frozen.unstable_batchedUpdates
845
867
  })
846
868
  );
847
869
  React2.useEffect(() => {
848
- const room2 = client.enter(
849
- roomId,
850
- {
851
- initialPresence: frozen.initialPresence,
852
- initialStorage: frozen.initialStorage,
853
- shouldInitiallyConnect: frozen.shouldInitiallyConnect,
854
- unstable_batchedUpdates: frozen.unstable_batchedUpdates
855
- }
856
- );
857
- setRoom(room2);
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
+ });
876
+ setRoomLeavePair(pair);
877
+ const { room: room2, leave } = pair;
878
+ if (frozen.autoConnect) {
879
+ room2.connect();
880
+ }
858
881
  return () => {
859
882
  const commentsRoom = commentsRooms.get(room2);
860
883
  if (commentsRoom) {
861
884
  commentsRooms.delete(room2);
862
885
  }
863
- client.leave(roomId);
886
+ leave();
864
887
  };
865
888
  }, [roomId, frozen]);
866
889
  return /* @__PURE__ */ React2.createElement(RoomContext.Provider, { value: room }, /* @__PURE__ */ React2.createElement(
@@ -885,7 +908,7 @@ function createRoomContext(client, options) {
885
908
  const room = useRoom();
886
909
  const subscribe = room.events.status.subscribe;
887
910
  const getSnapshot = room.getStatus;
888
- const getServerSnapshot = alwaysConnecting;
911
+ const getServerSnapshot = room.getStatus;
889
912
  return useSyncExternalStore3(subscribe, getSnapshot, getServerSnapshot);
890
913
  }
891
914
  function useMyPresence() {
@@ -1370,7 +1393,7 @@ function createRoomContext(client, options) {
1370
1393
  }
1371
1394
  const bundle = {
1372
1395
  RoomContext,
1373
- RoomProvider,
1396
+ RoomProvider: RoomProviderOuter,
1374
1397
  useRoom,
1375
1398
  useStatus,
1376
1399
  useBatch,
@@ -1408,7 +1431,7 @@ function createRoomContext(client, options) {
1408
1431
  useRemoveReaction,
1409
1432
  suspense: {
1410
1433
  RoomContext,
1411
- RoomProvider,
1434
+ RoomProvider: RoomProviderOuter,
1412
1435
  useRoom,
1413
1436
  useStatus,
1414
1437
  useBatch,