@liveblocks/core 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.mjs CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "1.4.7";
9
+ var PKG_VERSION = "1.5.0-test1";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -5911,15 +5911,35 @@ function createClient(options) {
5911
5911
  clientOptions.lostConnectionTimeout ?? DEFAULT_LOST_CONNECTION_TIMEOUT
5912
5912
  );
5913
5913
  const authManager = createAuthManager(options);
5914
- const rooms = /* @__PURE__ */ new Map();
5915
- function getRoom(roomId) {
5916
- const room = rooms.get(roomId);
5917
- return room ? room : null;
5914
+ const roomsById = /* @__PURE__ */ new Map();
5915
+ function teardownRoom(room) {
5916
+ unlinkDevTools(room.id);
5917
+ roomsById.delete(room.id);
5918
+ room.destroy();
5919
+ }
5920
+ function leaseRoom(info) {
5921
+ const leave = () => {
5922
+ const self = leave;
5923
+ if (!info.unsubs.delete(self)) {
5924
+ warn(
5925
+ "This leave function was already called. Calling it more than once has no effect."
5926
+ );
5927
+ } else {
5928
+ if (info.unsubs.size === 0) {
5929
+ teardownRoom(info.room);
5930
+ }
5931
+ }
5932
+ };
5933
+ info.unsubs.add(leave);
5934
+ return {
5935
+ room: info.room,
5936
+ leave
5937
+ };
5918
5938
  }
5919
- function enter(roomId, options2) {
5920
- const existingRoom = rooms.get(roomId);
5921
- if (existingRoom !== void 0) {
5922
- return existingRoom;
5939
+ function enterRoom(roomId, options2) {
5940
+ const existing = roomsById.get(roomId);
5941
+ if (existing !== void 0) {
5942
+ return leaseRoom(existing);
5923
5943
  }
5924
5944
  deprecateIf(
5925
5945
  options2.initialPresence === null || options2.initialPresence === void 0,
@@ -5949,10 +5969,14 @@ function createClient(options) {
5949
5969
  unstable_fallbackToHTTP: !!clientOptions.unstable_fallbackToHTTP
5950
5970
  }
5951
5971
  );
5952
- rooms.set(roomId, newRoom);
5953
- setupDevTools(() => Array.from(rooms.keys()));
5972
+ const newRoomInfo = {
5973
+ room: newRoom,
5974
+ unsubs: /* @__PURE__ */ new Set()
5975
+ };
5976
+ roomsById.set(roomId, newRoomInfo);
5977
+ setupDevTools(() => Array.from(roomsById.keys()));
5954
5978
  linkDevTools(roomId, newRoom);
5955
- const shouldConnect = options2.shouldInitiallyConnect ?? true;
5979
+ const shouldConnect = options2.autoConnect ?? options2.shouldInitiallyConnect ?? true;
5956
5980
  if (shouldConnect) {
5957
5981
  if (typeof atob === "undefined") {
5958
5982
  if (clientOptions.polyfills?.atob === void 0) {
@@ -5964,20 +5988,29 @@ function createClient(options) {
5964
5988
  }
5965
5989
  newRoom.connect();
5966
5990
  }
5967
- return newRoom;
5991
+ return leaseRoom(newRoomInfo);
5992
+ }
5993
+ function enter(roomId, options2) {
5994
+ const { room, leave: _ } = enterRoom(roomId, options2);
5995
+ return room;
5996
+ }
5997
+ function getRoom(roomId) {
5998
+ const room = roomsById.get(roomId)?.room;
5999
+ return room ? room : null;
5968
6000
  }
5969
- function leave(roomId) {
5970
- unlinkDevTools(roomId);
5971
- const room = rooms.get(roomId);
5972
- if (room !== void 0) {
5973
- room.destroy();
5974
- rooms.delete(roomId);
6001
+ function forceLeave(roomId) {
6002
+ const unsubs = roomsById.get(roomId)?.unsubs ?? /* @__PURE__ */ new Set();
6003
+ for (const unsub of unsubs) {
6004
+ unsub();
5975
6005
  }
5976
6006
  }
5977
6007
  return {
5978
- getRoom,
6008
+ // Old, deprecated APIs
5979
6009
  enter,
5980
- leave
6010
+ getRoom,
6011
+ leave: forceLeave,
6012
+ // New, preferred API
6013
+ enterRoom
5981
6014
  };
5982
6015
  }
5983
6016
  function checkBounds(option, value, min, max, recommendedMin) {