@liveblocks/core 1.1.0-beta3 → 1.1.0-beta4

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.
Files changed (2) hide show
  1. package/dist/index.js +59 -36
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -157,7 +157,7 @@ var onMessageFromPanel = eventSource.observable;
157
157
  // src/devtools/index.ts
158
158
  var VERSION = true ? (
159
159
  /* istanbul ignore next */
160
- "1.1.0-beta3"
160
+ "1.1.0-beta4"
161
161
  ) : "dev";
162
162
  var _devtoolsSetupHasRun = false;
163
163
  function setupDevTools(getAllRooms) {
@@ -924,10 +924,6 @@ var logPermanentClose = log(
924
924
  1 /* WARN */,
925
925
  "Connection to WebSocket closed permanently. Won't retry."
926
926
  );
927
- function sendHeartbeat(ctx) {
928
- var _a;
929
- (_a = ctx.socket) == null ? void 0 : _a.send("ping");
930
- }
931
927
  function isCloseEvent(error2) {
932
928
  return !(error2 instanceof Error) && error2.type === "close";
933
929
  }
@@ -1168,20 +1164,18 @@ function createConnectionStateMachine(delegates, enableDebugLogging) {
1168
1164
  };
1169
1165
  }
1170
1166
  );
1171
- machine.addTimedTransition("@ok.connected", HEARTBEAT_INTERVAL, {
1167
+ const sendHeartbeat = {
1172
1168
  target: "@ok.awaiting-pong",
1173
- effect: sendHeartbeat
1174
- }).addTransitions("@ok.connected", {
1175
- WINDOW_GOT_FOCUS: { target: "@ok.awaiting-pong", effect: sendHeartbeat }
1176
- });
1177
- const noPongAction = {
1178
- target: "@connecting.busy",
1179
- // Log implicit connection loss and drop the current open socket
1180
- effect: log(
1181
- 1 /* WARN */,
1182
- "Received no pong from server, assume implicit connection loss."
1183
- )
1169
+ effect: (ctx) => {
1170
+ var _a;
1171
+ (_a = ctx.socket) == null ? void 0 : _a.send("ping");
1172
+ }
1184
1173
  };
1174
+ machine.addTimedTransition("@ok.connected", HEARTBEAT_INTERVAL, sendHeartbeat).addTransitions("@ok.connected", {
1175
+ NAVIGATOR_OFFLINE: sendHeartbeat,
1176
+ // Don't take the browser's word for it when it says it's offline. Do a ping/pong to make sure.
1177
+ WINDOW_GOT_FOCUS: sendHeartbeat
1178
+ });
1185
1179
  machine.onEnter("@ok.*", (ctx) => {
1186
1180
  ctx.patch({ successCount: ctx.successCount + 1 });
1187
1181
  const timerID = setTimeout(
@@ -1197,7 +1191,14 @@ function createConnectionStateMachine(delegates, enableDebugLogging) {
1197
1191
  clearTimeout(timerID);
1198
1192
  onMessage.pause();
1199
1193
  };
1200
- }).addTimedTransition("@ok.awaiting-pong", PONG_TIMEOUT, noPongAction).addTransitions("@ok.awaiting-pong", { PONG_TIMEOUT: noPongAction }).addTransitions("@ok.awaiting-pong", { PONG: "@ok.connected" }).addTransitions("@ok.*", {
1194
+ }).addTransitions("@ok.awaiting-pong", { PONG: "@ok.connected" }).addTimedTransition("@ok.awaiting-pong", PONG_TIMEOUT, {
1195
+ target: "@connecting.busy",
1196
+ // Log implicit connection loss and drop the current open socket
1197
+ effect: log(
1198
+ 1 /* WARN */,
1199
+ "Received no pong from server, assume implicit connection loss."
1200
+ )
1201
+ }).addTransitions("@ok.*", {
1201
1202
  // When a socket receives an error, this can cause the closing of the
1202
1203
  // socket, or not. So always check to see if the socket is still OPEN or
1203
1204
  // not. When still OPEN, don't transition.
@@ -1248,7 +1249,10 @@ function createConnectionStateMachine(delegates, enableDebugLogging) {
1248
1249
  const win = typeof window !== "undefined" ? window : void 0;
1249
1250
  const root = win != null ? win : doc;
1250
1251
  machine.onEnter("*", (ctx) => {
1251
- function onBackOnline() {
1252
+ function onNetworkOffline() {
1253
+ machine.send({ type: "NAVIGATOR_OFFLINE" });
1254
+ }
1255
+ function onNetworkBackOnline() {
1252
1256
  machine.send({ type: "NAVIGATOR_ONLINE" });
1253
1257
  }
1254
1258
  function onVisibilityChange() {
@@ -1256,11 +1260,13 @@ function createConnectionStateMachine(delegates, enableDebugLogging) {
1256
1260
  machine.send({ type: "WINDOW_GOT_FOCUS" });
1257
1261
  }
1258
1262
  }
1259
- win == null ? void 0 : win.addEventListener("online", onBackOnline);
1263
+ win == null ? void 0 : win.addEventListener("online", onNetworkBackOnline);
1264
+ win == null ? void 0 : win.addEventListener("offline", onNetworkOffline);
1260
1265
  root == null ? void 0 : root.addEventListener("visibilitychange", onVisibilityChange);
1261
1266
  return () => {
1262
1267
  root == null ? void 0 : root.removeEventListener("visibilitychange", onVisibilityChange);
1263
- win == null ? void 0 : win.removeEventListener("online", onBackOnline);
1268
+ win == null ? void 0 : win.removeEventListener("online", onNetworkBackOnline);
1269
+ win == null ? void 0 : win.removeEventListener("offline", onNetworkOffline);
1264
1270
  teardownSocket(ctx.socket);
1265
1271
  };
1266
1272
  });
@@ -5168,7 +5174,7 @@ ${Array.from(traces).join("\n\n")}`
5168
5174
  send: {
5169
5175
  // These exist only for our E2E testing app
5170
5176
  explicitClose: (event) => managedSocket._privateSendMachineEvent({ type: "EXPLICIT_SOCKET_CLOSE", event }),
5171
- implicitClose: () => managedSocket._privateSendMachineEvent({ type: "PONG_TIMEOUT" })
5177
+ implicitClose: () => managedSocket._privateSendMachineEvent({ type: "NAVIGATOR_OFFLINE" })
5172
5178
  }
5173
5179
  },
5174
5180
  id: config.roomId,
@@ -5305,7 +5311,7 @@ function makeCreateSocketDelegateForRoom(liveblocksServer, WebSocketPolyfill) {
5305
5311
  // @ts-ignore (__PACKAGE_VERSION__ will be injected by the build script)
5306
5312
  true ? (
5307
5313
  /* istanbul ignore next */
5308
- "1.1.0-beta3"
5314
+ "1.1.0-beta4"
5309
5315
  ) : "dev"}`
5310
5316
  );
5311
5317
  };
@@ -5408,20 +5414,28 @@ function fetchAuthEndpoint(fetch2, endpoint, body) {
5408
5414
  var MIN_THROTTLE = 16;
5409
5415
  var MAX_THROTTLE = 1e3;
5410
5416
  var DEFAULT_THROTTLE = 100;
5417
+ var MIN_LOST_CONNECTION_TIMEOUT = 200;
5418
+ var RECOMMENDED_MIN_LOST_CONNECTION_TIMEOUT = 1e3;
5419
+ var MAX_LOST_CONNECTION_TIMEOUT = 3e4;
5420
+ var DEFAULT_LOST_CONNECTION_TIMEOUT = 5e3;
5411
5421
  function getServerFromClientOptions(clientOptions) {
5412
5422
  const rawOptions = clientOptions;
5413
5423
  return typeof rawOptions.liveblocksServer === "string" ? rawOptions.liveblocksServer : "wss://api.liveblocks.io/v6";
5414
5424
  }
5415
5425
  function createClient(options) {
5426
+ var _a, _b;
5416
5427
  const clientOptions = options;
5417
- const throttleDelay = getThrottleDelayFromOptions(clientOptions);
5428
+ const throttleDelay = getThrottle((_a = clientOptions.throttle) != null ? _a : DEFAULT_THROTTLE);
5429
+ const lostConnectionTimeout = getLostConnectionTimeout(
5430
+ (_b = clientOptions.lostConnectionTimeout) != null ? _b : DEFAULT_LOST_CONNECTION_TIMEOUT
5431
+ );
5418
5432
  const rooms = /* @__PURE__ */ new Map();
5419
5433
  function getRoom(roomId) {
5420
5434
  const room = rooms.get(roomId);
5421
5435
  return room ? room : null;
5422
5436
  }
5423
5437
  function enter(roomId, options2) {
5424
- var _a, _b, _c, _d;
5438
+ var _a2, _b2, _c;
5425
5439
  const existingRoom = rooms.get(roomId);
5426
5440
  if (existingRoom !== void 0) {
5427
5441
  return existingRoom;
@@ -5432,13 +5446,13 @@ function createClient(options) {
5432
5446
  );
5433
5447
  const newRoom = createRoom(
5434
5448
  {
5435
- initialPresence: (_a = options2.initialPresence) != null ? _a : {},
5449
+ initialPresence: (_a2 = options2.initialPresence) != null ? _a2 : {},
5436
5450
  initialStorage: options2.initialStorage
5437
5451
  },
5438
5452
  {
5439
5453
  roomId,
5440
5454
  throttleDelay,
5441
- lostConnectionTimeout: (_b = clientOptions.lostConnectionTimeout) != null ? _b : 5e3,
5455
+ lostConnectionTimeout,
5442
5456
  polyfills: clientOptions.polyfills,
5443
5457
  delegates: clientOptions.mockedDelegates,
5444
5458
  enableDebugLogging: clientOptions.enableDebugLogging,
@@ -5455,10 +5469,10 @@ function createClient(options) {
5455
5469
  rooms.set(roomId, newRoom);
5456
5470
  setupDevTools(() => Array.from(rooms.keys()));
5457
5471
  linkDevTools(roomId, newRoom);
5458
- const shouldConnect = (_c = options2.shouldInitiallyConnect) != null ? _c : true;
5472
+ const shouldConnect = (_b2 = options2.shouldInitiallyConnect) != null ? _b2 : true;
5459
5473
  if (shouldConnect) {
5460
5474
  if (typeof atob === "undefined") {
5461
- if (((_d = clientOptions.polyfills) == null ? void 0 : _d.atob) === void 0) {
5475
+ if (((_c = clientOptions.polyfills) == null ? void 0 : _c.atob) === void 0) {
5462
5476
  throw new Error(
5463
5477
  "You need to polyfill atob to use the client in your environment. Please follow the instructions at https://liveblocks.io/docs/errors/liveblocks-client/atob-polyfill"
5464
5478
  );
@@ -5483,16 +5497,25 @@ function createClient(options) {
5483
5497
  leave
5484
5498
  };
5485
5499
  }
5486
- function getThrottleDelayFromOptions(options) {
5487
- if (options.throttle === void 0) {
5488
- return DEFAULT_THROTTLE;
5489
- }
5490
- if (typeof options.throttle !== "number" || options.throttle < MIN_THROTTLE || options.throttle > MAX_THROTTLE) {
5500
+ function checkBounds(option, value, min, max, recommendedMin) {
5501
+ if (typeof value !== "number" || value < min || value > max) {
5491
5502
  throw new Error(
5492
- `throttle should be a number between ${MIN_THROTTLE} and ${MAX_THROTTLE}.`
5503
+ `${option} should be a number between ${recommendedMin != null ? recommendedMin : min} and ${max}.`
5493
5504
  );
5494
5505
  }
5495
- return options.throttle;
5506
+ return value;
5507
+ }
5508
+ function getThrottle(value) {
5509
+ return checkBounds("throttle", value, MIN_THROTTLE, MAX_THROTTLE);
5510
+ }
5511
+ function getLostConnectionTimeout(value) {
5512
+ return checkBounds(
5513
+ "lostConnectionTimeout",
5514
+ value,
5515
+ MIN_LOST_CONNECTION_TIMEOUT,
5516
+ MAX_LOST_CONNECTION_TIMEOUT,
5517
+ RECOMMENDED_MIN_LOST_CONNECTION_TIMEOUT
5518
+ );
5496
5519
  }
5497
5520
  function prepareAuthentication(clientOptions, roomId) {
5498
5521
  const { publicApiKey, authEndpoint } = clientOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/core",
3
- "version": "1.1.0-beta3",
3
+ "version": "1.1.0-beta4",
4
4
  "description": "Shared code and foundational internals for Liveblocks",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",