@liveblocks/client 0.14.0-beta.1 → 0.14.0

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.
@@ -68,6 +68,7 @@ export declare class LiveList<T> extends AbstractCrdt {
68
68
  * @param index The index of the element to delete
69
69
  */
70
70
  delete(index: number): void;
71
+ clear(): void;
71
72
  /**
72
73
  * Returns an Array of all the elements in the LiveList.
73
74
  */
@@ -283,6 +283,28 @@ class LiveList extends AbstractCrdt_1.AbstractCrdt {
283
283
  }
284
284
  }
285
285
  }
286
+ clear() {
287
+ if (this._doc) {
288
+ let ops = [];
289
+ let reverseOps = [];
290
+ for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
291
+ item[0]._detach();
292
+ const childId = item[0]._id;
293
+ if (childId) {
294
+ ops.push({ id: childId, type: live_1.OpType.DeleteCrdt });
295
+ reverseOps.push(...item[0]._serialize(this._id, item[1]));
296
+ }
297
+ }
298
+ __classPrivateFieldSet(this, _LiveList_items, [], "f");
299
+ this._doc.dispatch(ops, reverseOps, [this]);
300
+ }
301
+ else {
302
+ for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
303
+ item[0]._detach();
304
+ }
305
+ __classPrivateFieldSet(this, _LiveList_items, [], "f");
306
+ }
307
+ }
286
308
  /**
287
309
  * Returns an Array of all the elements in the LiveList.
288
310
  */
@@ -1,5 +1,5 @@
1
1
  export { LiveObject } from "./LiveObject";
2
2
  export { LiveMap } from "./LiveMap";
3
3
  export { LiveList } from "./LiveList";
4
- export type { Others, Presence, Room, Client, User } from "./types";
4
+ export type { Others, Presence, Room, Client, User, BroadcastOptions, } from "./types";
5
5
  export { createClient } from "./client";
package/lib/cjs/room.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback, StorageCallback, StorageUpdate } from "./types";
1
+ import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback, StorageCallback, StorageUpdate, BroadcastOptions } from "./types";
2
2
  import { ClientMessage, Op } from "./live";
3
3
  import { LiveMap } from "./LiveMap";
4
4
  import { LiveObject } from "./LiveObject";
@@ -126,7 +126,7 @@ export declare function makeStateMachine(state: State, context: Context, mockedE
126
126
  updatePresence: <T_4 extends Presence>(overrides: Partial<T_4>, options?: {
127
127
  addToHistory: boolean;
128
128
  } | undefined) => void;
129
- broadcastEvent: (event: any) => void;
129
+ broadcastEvent: (event: any, options?: BroadcastOptions) => void;
130
130
  batch: (callback: () => void) => void;
131
131
  undo: () => void;
132
132
  redo: () => void;
package/lib/cjs/room.js CHANGED
@@ -58,6 +58,9 @@ function makeOthers(presenceMap) {
58
58
  get count() {
59
59
  return array.length;
60
60
  },
61
+ [Symbol.iterator]() {
62
+ return array[Symbol.iterator]();
63
+ },
61
64
  map(callback) {
62
65
  return array.map(callback);
63
66
  },
@@ -475,7 +478,9 @@ See v0.13 release notes for more information.
475
478
  state.socket = socket;
476
479
  }
477
480
  function authenticationFailure(error) {
478
- console.error(error);
481
+ if (process.env.NODE_ENV !== "production") {
482
+ console.error("Call to authentication endpoint failed", error);
483
+ }
479
484
  updateConnection({ state: "unavailable" });
480
485
  state.numberOfRetry++;
481
486
  state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
@@ -642,14 +647,20 @@ See v0.13 release notes for more information.
642
647
  updateConnection({ state: "failed" });
643
648
  const error = new LiveblocksError(event.reason, event.code);
644
649
  for (const listener of state.listeners.error) {
645
- console.error(`Liveblocks WebSocket connection closed. Reason: ${error.message} (code: ${error.code})`);
650
+ if (process.env.NODE_ENV !== "production") {
651
+ console.error(`Connection to Liveblocks websocket server closed. Reason: ${error.message} (code: ${error.code})`);
652
+ }
646
653
  listener(error);
647
654
  }
648
655
  }
649
656
  else if (event.wasClean === false) {
650
- updateConnection({ state: "unavailable" });
651
657
  state.numberOfRetry++;
652
- state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
658
+ const delay = getRetryDelay();
659
+ if (process.env.NODE_ENV !== "production") {
660
+ console.warn(`Connection to Liveblocks websocket server closed (code: ${event.code}). Retrying in ${delay}ms.`);
661
+ }
662
+ updateConnection({ state: "unavailable" });
663
+ state.timeoutHandles.reconnect = effects.scheduleReconnect(delay);
653
664
  }
654
665
  else {
655
666
  updateConnection({ state: "closed" });
@@ -813,8 +824,10 @@ See v0.13 release notes for more information.
813
824
  function getOthers() {
814
825
  return state.others;
815
826
  }
816
- function broadcastEvent(event) {
817
- if (state.socket == null) {
827
+ function broadcastEvent(event, options = {
828
+ shouldQueueEventIfNotReady: false,
829
+ }) {
830
+ if (state.socket == null && options.shouldQueueEventIfNotReady == false) {
818
831
  return;
819
832
  }
820
833
  state.buffer.messages.push({
@@ -28,6 +28,14 @@ export declare type LiveListUpdates<TItem = any> = {
28
28
  type: "LiveList";
29
29
  node: LiveList<TItem>;
30
30
  };
31
+ export declare type BroadcastOptions = {
32
+ /**
33
+ * Whether or not event is queued if the connection is currently closed.
34
+ *
35
+ * ❗ We are not sure if we want to support this option in the future so it might be deprecated to be replaced by something else
36
+ */
37
+ shouldQueueEventIfNotReady: boolean;
38
+ };
31
39
  export declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
32
40
  export declare type StorageCallback = (updates: StorageUpdate[]) => void;
33
41
  export declare type Client = {
@@ -65,6 +73,10 @@ export interface Others<TPresence extends Presence = Presence> {
65
73
  * Number of other users in the room.
66
74
  */
67
75
  readonly count: number;
76
+ /**
77
+ * Returns a new Iterator object that contains the users.
78
+ */
79
+ [Symbol.iterator](): IterableIterator<User<TPresence>>;
68
80
  /**
69
81
  * Returns the array of connected users in room.
70
82
  */
@@ -423,7 +435,7 @@ export declare type Room = {
423
435
  * }
424
436
  * });
425
437
  */
426
- broadcastEvent: (event: any) => void;
438
+ broadcastEvent: (event: any, options?: BroadcastOptions) => void;
427
439
  /**
428
440
  * Get the room's storage asynchronously.
429
441
  * The storage's root is a {@link LiveObject}.
@@ -68,6 +68,7 @@ export declare class LiveList<T> extends AbstractCrdt {
68
68
  * @param index The index of the element to delete
69
69
  */
70
70
  delete(index: number): void;
71
+ clear(): void;
71
72
  /**
72
73
  * Returns an Array of all the elements in the LiveList.
73
74
  */
@@ -280,6 +280,28 @@ export class LiveList extends AbstractCrdt {
280
280
  }
281
281
  }
282
282
  }
283
+ clear() {
284
+ if (this._doc) {
285
+ let ops = [];
286
+ let reverseOps = [];
287
+ for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
288
+ item[0]._detach();
289
+ const childId = item[0]._id;
290
+ if (childId) {
291
+ ops.push({ id: childId, type: OpType.DeleteCrdt });
292
+ reverseOps.push(...item[0]._serialize(this._id, item[1]));
293
+ }
294
+ }
295
+ __classPrivateFieldSet(this, _LiveList_items, [], "f");
296
+ this._doc.dispatch(ops, reverseOps, [this]);
297
+ }
298
+ else {
299
+ for (const item of __classPrivateFieldGet(this, _LiveList_items, "f")) {
300
+ item[0]._detach();
301
+ }
302
+ __classPrivateFieldSet(this, _LiveList_items, [], "f");
303
+ }
304
+ }
283
305
  /**
284
306
  * Returns an Array of all the elements in the LiveList.
285
307
  */
@@ -1,5 +1,5 @@
1
1
  export { LiveObject } from "./LiveObject";
2
2
  export { LiveMap } from "./LiveMap";
3
3
  export { LiveList } from "./LiveList";
4
- export type { Others, Presence, Room, Client, User } from "./types";
4
+ export type { Others, Presence, Room, Client, User, BroadcastOptions, } from "./types";
5
5
  export { createClient } from "./client";
package/lib/esm/room.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback, StorageCallback, StorageUpdate } from "./types";
1
+ import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback, StorageCallback, StorageUpdate, BroadcastOptions } from "./types";
2
2
  import { ClientMessage, Op } from "./live";
3
3
  import { LiveMap } from "./LiveMap";
4
4
  import { LiveObject } from "./LiveObject";
@@ -126,7 +126,7 @@ export declare function makeStateMachine(state: State, context: Context, mockedE
126
126
  updatePresence: <T_4 extends Presence>(overrides: Partial<T_4>, options?: {
127
127
  addToHistory: boolean;
128
128
  } | undefined) => void;
129
- broadcastEvent: (event: any) => void;
129
+ broadcastEvent: (event: any, options?: BroadcastOptions) => void;
130
130
  batch: (callback: () => void) => void;
131
131
  undo: () => void;
132
132
  redo: () => void;
package/lib/esm/room.js CHANGED
@@ -36,6 +36,9 @@ function makeOthers(presenceMap) {
36
36
  get count() {
37
37
  return array.length;
38
38
  },
39
+ [Symbol.iterator]() {
40
+ return array[Symbol.iterator]();
41
+ },
39
42
  map(callback) {
40
43
  return array.map(callback);
41
44
  },
@@ -453,7 +456,9 @@ See v0.13 release notes for more information.
453
456
  state.socket = socket;
454
457
  }
455
458
  function authenticationFailure(error) {
456
- console.error(error);
459
+ if (process.env.NODE_ENV !== "production") {
460
+ console.error("Call to authentication endpoint failed", error);
461
+ }
457
462
  updateConnection({ state: "unavailable" });
458
463
  state.numberOfRetry++;
459
464
  state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
@@ -620,14 +625,20 @@ See v0.13 release notes for more information.
620
625
  updateConnection({ state: "failed" });
621
626
  const error = new LiveblocksError(event.reason, event.code);
622
627
  for (const listener of state.listeners.error) {
623
- console.error(`Liveblocks WebSocket connection closed. Reason: ${error.message} (code: ${error.code})`);
628
+ if (process.env.NODE_ENV !== "production") {
629
+ console.error(`Connection to Liveblocks websocket server closed. Reason: ${error.message} (code: ${error.code})`);
630
+ }
624
631
  listener(error);
625
632
  }
626
633
  }
627
634
  else if (event.wasClean === false) {
628
- updateConnection({ state: "unavailable" });
629
635
  state.numberOfRetry++;
630
- state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
636
+ const delay = getRetryDelay();
637
+ if (process.env.NODE_ENV !== "production") {
638
+ console.warn(`Connection to Liveblocks websocket server closed (code: ${event.code}). Retrying in ${delay}ms.`);
639
+ }
640
+ updateConnection({ state: "unavailable" });
641
+ state.timeoutHandles.reconnect = effects.scheduleReconnect(delay);
631
642
  }
632
643
  else {
633
644
  updateConnection({ state: "closed" });
@@ -791,8 +802,10 @@ See v0.13 release notes for more information.
791
802
  function getOthers() {
792
803
  return state.others;
793
804
  }
794
- function broadcastEvent(event) {
795
- if (state.socket == null) {
805
+ function broadcastEvent(event, options = {
806
+ shouldQueueEventIfNotReady: false,
807
+ }) {
808
+ if (state.socket == null && options.shouldQueueEventIfNotReady == false) {
796
809
  return;
797
810
  }
798
811
  state.buffer.messages.push({
@@ -28,6 +28,14 @@ export declare type LiveListUpdates<TItem = any> = {
28
28
  type: "LiveList";
29
29
  node: LiveList<TItem>;
30
30
  };
31
+ export declare type BroadcastOptions = {
32
+ /**
33
+ * Whether or not event is queued if the connection is currently closed.
34
+ *
35
+ * ❗ We are not sure if we want to support this option in the future so it might be deprecated to be replaced by something else
36
+ */
37
+ shouldQueueEventIfNotReady: boolean;
38
+ };
31
39
  export declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
32
40
  export declare type StorageCallback = (updates: StorageUpdate[]) => void;
33
41
  export declare type Client = {
@@ -65,6 +73,10 @@ export interface Others<TPresence extends Presence = Presence> {
65
73
  * Number of other users in the room.
66
74
  */
67
75
  readonly count: number;
76
+ /**
77
+ * Returns a new Iterator object that contains the users.
78
+ */
79
+ [Symbol.iterator](): IterableIterator<User<TPresence>>;
68
80
  /**
69
81
  * Returns the array of connected users in room.
70
82
  */
@@ -423,7 +435,7 @@ export declare type Room = {
423
435
  * }
424
436
  * });
425
437
  */
426
- broadcastEvent: (event: any) => void;
438
+ broadcastEvent: (event: any, options?: BroadcastOptions) => void;
427
439
  /**
428
440
  * Get the room's storage asynchronously.
429
441
  * The storage's root is a {@link LiveObject}.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/client",
3
- "version": "0.14.0-beta.1",
3
+ "version": "0.14.0",
4
4
  "description": "",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./lib/esm/index.js",