@liveblocks/core 1.0.11 → 1.0.12-yjs1

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.ts CHANGED
@@ -568,8 +568,23 @@ declare type BaseUserMeta = {
568
568
  declare type Callback<T> = (event: T) => void;
569
569
  declare type UnsubscribeCallback = () => void;
570
570
  declare type Observable<T> = {
571
+ /**
572
+ * Register a callback function to be called whenever the event source emits
573
+ * an event.
574
+ */
571
575
  subscribe(callback: Callback<T>): UnsubscribeCallback;
576
+ /**
577
+ * Register a one-time callback function to be called whenever the event
578
+ * source emits an event. After the event fires, the callback is
579
+ * auto-unsubscribed.
580
+ */
572
581
  subscribeOnce(callback: Callback<T>): UnsubscribeCallback;
582
+ /**
583
+ * Returns a promise that will resolve when an event is emitted by this
584
+ * event source. Optionally, specify a predicate that has to match. The first
585
+ * event matching that predicate will then resolve the promise.
586
+ */
587
+ waitUntil(predicate?: (event: T) => boolean): Promise<T>;
573
588
  };
574
589
 
575
590
  interface IWebSocketEvent {
@@ -968,6 +983,17 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
968
983
  */
969
984
  addToHistory: boolean;
970
985
  }): void;
986
+ /**
987
+ *
988
+ * Sends YJS document updates to liveblocks server
989
+ *
990
+ * @param {string} data the doc update to send to the server, base64 encoded uint8array
991
+ */
992
+ updateDoc(data: string): void;
993
+ /**
994
+ * Sends a request for the current document from liveblocks server
995
+ */
996
+ getDoc(stateVector?: string): void;
971
997
  /**
972
998
  * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
973
999
  * @param {any} event the event to broadcast. Should be serializable to JSON
@@ -1022,6 +1048,7 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1022
1048
  */
1023
1049
  readonly storageDidLoad: Observable<void>;
1024
1050
  readonly storageStatus: Observable<StorageStatus>;
1051
+ readonly docUpdated: Observable<string[]>;
1025
1052
  };
1026
1053
  /**
1027
1054
  * Batches modifications made during the given function.
@@ -1359,12 +1386,14 @@ declare enum ClientMsgCode {
1359
1386
  UPDATE_PRESENCE = 100,
1360
1387
  BROADCAST_EVENT = 103,
1361
1388
  FETCH_STORAGE = 200,
1362
- UPDATE_STORAGE = 201
1389
+ UPDATE_STORAGE = 201,
1390
+ FETCH_DOC = 300,
1391
+ UPDATE_DOC = 301
1363
1392
  }
1364
1393
  /**
1365
1394
  * Messages that can be sent from the client to the server.
1366
1395
  */
1367
- declare type ClientMsg<TPresence extends JsonObject, TRoomEvent extends Json> = BroadcastEventClientMsg<TRoomEvent> | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg;
1396
+ declare type ClientMsg<TPresence extends JsonObject, TRoomEvent extends Json> = BroadcastEventClientMsg<TRoomEvent> | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg | FetchDocClientMsg | UpdateDocClientMsg;
1368
1397
  declare type BroadcastEventClientMsg<TRoomEvent extends Json> = {
1369
1398
  type: ClientMsgCode.BROADCAST_EVENT;
1370
1399
  event: TRoomEvent;
@@ -1402,6 +1431,14 @@ declare type UpdateStorageClientMsg = {
1402
1431
  declare type FetchStorageClientMsg = {
1403
1432
  readonly type: ClientMsgCode.FETCH_STORAGE;
1404
1433
  };
1434
+ declare type FetchDocClientMsg = {
1435
+ readonly type: ClientMsgCode.FETCH_DOC;
1436
+ readonly vector?: string;
1437
+ };
1438
+ declare type UpdateDocClientMsg = {
1439
+ readonly type: ClientMsgCode.UPDATE_DOC;
1440
+ readonly data: string;
1441
+ };
1405
1442
 
1406
1443
  declare type IdTuple<T> = [id: string, value: T];
1407
1444
  declare enum CrdtType {
@@ -1451,12 +1488,14 @@ declare enum ServerMsgCode {
1451
1488
  ROOM_STATE = 104,
1452
1489
  INITIAL_STORAGE_STATE = 200,
1453
1490
  UPDATE_STORAGE = 201,
1454
- REJECT_STORAGE_OP = 299
1491
+ REJECT_STORAGE_OP = 299,
1492
+ FETCH_DOC = 300,
1493
+ DOC_UPDATE = 301
1455
1494
  }
1456
1495
  /**
1457
1496
  * Messages that can be sent from the server to the client.
1458
1497
  */
1459
- declare type ServerMsg<TPresence extends JsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg<TUserMeta> | UserLeftServerMsg | BroadcastedEventServerMsg<TRoomEvent> | RoomStateServerMsg<TUserMeta> | InitialDocumentStateServerMsg | UpdateStorageServerMsg | RejectedStorageOpServerMsg;
1498
+ declare type ServerMsg<TPresence extends JsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg<TUserMeta> | UserLeftServerMsg | BroadcastedEventServerMsg<TRoomEvent> | RoomStateServerMsg<TUserMeta> | InitialDocumentStateServerMsg | UpdateStorageServerMsg | RejectedStorageOpServerMsg | FetchDoc;
1460
1499
  /**
1461
1500
  * Sent by the WebSocket server and broadcasted to all clients to announce that
1462
1501
  * a User updated their presence. For example, when a user moves their cursor.
@@ -1539,6 +1578,10 @@ declare type UserLeftServerMsg = {
1539
1578
  readonly type: ServerMsgCode.USER_LEFT;
1540
1579
  readonly actor: number;
1541
1580
  };
1581
+ declare type FetchDoc = {
1582
+ readonly type: ServerMsgCode.FETCH_DOC;
1583
+ readonly data: string[];
1584
+ };
1542
1585
  /**
1543
1586
  * Sent by the WebSocket server and broadcasted to all clients to announce that
1544
1587
  * a User broadcasted an Event to everyone in the Room.
@@ -1787,4 +1830,4 @@ declare type EnsureJson<T> = [
1787
1830
  [K in keyof T]: EnsureJson<T[K]>;
1788
1831
  };
1789
1832
 
1790
- export { AckOp, AppOnlyAuthToken, AuthToken, BaseUserMeta, BroadcastEventClientMsg, BroadcastOptions, BroadcastedEventServerMsg, Client, ClientMsg, ClientMsgCode, ConnectionStatus, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, DevToolsTreeNode as DevTools, protocol as DevToolsMsg, EnsureJson, FetchStorageClientMsg, History, IWebSocket, IWebSocketCloseEvent, IWebSocketEvent, IWebSocketInstance, IWebSocketMessageEvent, IdTuple, Immutable, InitialDocumentStateServerMsg, Json, JsonArray, JsonObject, JsonScalar, LiveList, LiveListUpdate, LiveMap, LiveMapUpdate, LiveNode, LiveObject, LiveObjectUpdate, LiveStructure, Lson, LsonObject, NodeMap, Op, OpCode, Others, ParentToChildNodeMap, PlainLson, PlainLsonFields, PlainLsonList, PlainLsonMap, PlainLsonObject, RejectedStorageOpServerMsg, Resolve, Room, RoomAuthToken, RoomInitializers, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, StorageStatus, StorageUpdate, ToImmutable, ToJson, UpdateObjectOp, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, User, UserJoinServerMsg, UserLeftServerMsg, WebsocketCloseCodes, asArrayWithLegacyMethods, asPos, assert, assertNever, b64decode, createClient, deprecate, deprecateIf, errorIf, freeze, isAppOnlyAuthToken, isAuthToken, isChildCrdt, isJsonArray, isJsonObject, isJsonScalar, isPlainObject, isRoomAuthToken, isRootCrdt, legacy_patchImmutableObject, lsonToJson, makePosition, nn, patchLiveObjectKey, shallow, throwUsageError, toPlainLson, tryParseJson };
1833
+ export { AckOp, AppOnlyAuthToken, AuthToken, BaseUserMeta, BroadcastEventClientMsg, BroadcastOptions, BroadcastedEventServerMsg, Client, ClientMsg, ClientMsgCode, ConnectionStatus, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, DevToolsTreeNode as DevTools, protocol as DevToolsMsg, EnsureJson, FetchDocClientMsg, FetchStorageClientMsg, History, IWebSocket, IWebSocketCloseEvent, IWebSocketEvent, IWebSocketInstance, IWebSocketMessageEvent, IdTuple, Immutable, InitialDocumentStateServerMsg, Json, JsonArray, JsonObject, JsonScalar, LiveList, LiveListUpdate, LiveMap, LiveMapUpdate, LiveNode, LiveObject, LiveObjectUpdate, LiveStructure, Lson, LsonObject, NodeMap, Op, OpCode, Others, ParentToChildNodeMap, PlainLson, PlainLsonFields, PlainLsonList, PlainLsonMap, PlainLsonObject, RejectedStorageOpServerMsg, Resolve, Room, RoomAuthToken, RoomInitializers, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, StorageStatus, StorageUpdate, ToImmutable, ToJson, UpdateDocClientMsg, UpdateObjectOp, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, User, UserJoinServerMsg, UserLeftServerMsg, WebsocketCloseCodes, asArrayWithLegacyMethods, asPos, assert, assertNever, b64decode, createClient, deprecate, deprecateIf, errorIf, freeze, isAppOnlyAuthToken, isAuthToken, isChildCrdt, isJsonArray, isJsonObject, isJsonScalar, isPlainObject, isRoomAuthToken, isRootCrdt, legacy_patchImmutableObject, lsonToJson, makePosition, nn, patchLiveObjectKey, shallow, throwUsageError, toPlainLson, tryParseJson };
package/dist/index.js CHANGED
@@ -54,6 +54,19 @@ var __async = (__this, __arguments, generator) => {
54
54
  function makeEventSource() {
55
55
  const _onetimeObservers = /* @__PURE__ */ new Set();
56
56
  const _observers = /* @__PURE__ */ new Set();
57
+ let _buffer = null;
58
+ function pause() {
59
+ _buffer = [];
60
+ }
61
+ function unpause() {
62
+ if (_buffer === null) {
63
+ return;
64
+ }
65
+ for (const event of _buffer) {
66
+ notify(event);
67
+ }
68
+ _buffer = null;
69
+ }
57
70
  function subscribe(callback) {
58
71
  _observers.add(callback);
59
72
  return () => _observers.delete(callback);
@@ -62,6 +75,25 @@ function makeEventSource() {
62
75
  _onetimeObservers.add(callback);
63
76
  return () => _onetimeObservers.delete(callback);
64
77
  }
78
+ function waitUntil(predicate) {
79
+ return __async(this, null, function* () {
80
+ let unsub;
81
+ return new Promise((res) => {
82
+ unsub = subscribe((event) => {
83
+ if (predicate === void 0 || predicate(event)) {
84
+ res(event);
85
+ }
86
+ });
87
+ }).finally(() => unsub == null ? void 0 : unsub());
88
+ });
89
+ }
90
+ function notifyOrBuffer(event) {
91
+ if (_buffer !== null) {
92
+ _buffer.push(event);
93
+ } else {
94
+ notify(event);
95
+ }
96
+ }
65
97
  function notify(event) {
66
98
  _onetimeObservers.forEach((callback) => callback(event));
67
99
  _onetimeObservers.clear();
@@ -71,16 +103,24 @@ function makeEventSource() {
71
103
  _onetimeObservers.clear();
72
104
  _observers.clear();
73
105
  }
106
+ function count() {
107
+ return _onetimeObservers.size + _observers.size;
108
+ }
74
109
  return {
75
110
  // Private/internal control over event emission
76
- notify,
111
+ notify: notifyOrBuffer,
77
112
  subscribe,
78
113
  subscribeOnce,
79
114
  clear,
115
+ count,
116
+ waitUntil,
117
+ pause,
118
+ unpause,
80
119
  // Publicly exposable subscription API
81
120
  observable: {
82
121
  subscribe,
83
- subscribeOnce
122
+ subscribeOnce,
123
+ waitUntil
84
124
  }
85
125
  };
86
126
  }
@@ -117,7 +157,7 @@ var onMessageFromPanel = eventSource.observable;
117
157
  // src/devtools/index.ts
118
158
  var VERSION = true ? (
119
159
  /* istanbul ignore next */
120
- "1.0.11"
160
+ "1.0.12-yjs1"
121
161
  ) : "dev";
122
162
  var _devtoolsSetupHasRun = false;
123
163
  function setupDevTools(getAllRooms) {
@@ -3007,6 +3047,8 @@ var ClientMsgCode = /* @__PURE__ */ ((ClientMsgCode2) => {
3007
3047
  ClientMsgCode2[ClientMsgCode2["BROADCAST_EVENT"] = 103] = "BROADCAST_EVENT";
3008
3048
  ClientMsgCode2[ClientMsgCode2["FETCH_STORAGE"] = 200] = "FETCH_STORAGE";
3009
3049
  ClientMsgCode2[ClientMsgCode2["UPDATE_STORAGE"] = 201] = "UPDATE_STORAGE";
3050
+ ClientMsgCode2[ClientMsgCode2["FETCH_DOC"] = 300] = "FETCH_DOC";
3051
+ ClientMsgCode2[ClientMsgCode2["UPDATE_DOC"] = 301] = "UPDATE_DOC";
3010
3052
  return ClientMsgCode2;
3011
3053
  })(ClientMsgCode || {});
3012
3054
 
@@ -3020,6 +3062,8 @@ var ServerMsgCode = /* @__PURE__ */ ((ServerMsgCode2) => {
3020
3062
  ServerMsgCode2[ServerMsgCode2["INITIAL_STORAGE_STATE"] = 200] = "INITIAL_STORAGE_STATE";
3021
3063
  ServerMsgCode2[ServerMsgCode2["UPDATE_STORAGE"] = 201] = "UPDATE_STORAGE";
3022
3064
  ServerMsgCode2[ServerMsgCode2["REJECT_STORAGE_OP"] = 299] = "REJECT_STORAGE_OP";
3065
+ ServerMsgCode2[ServerMsgCode2["FETCH_DOC"] = 300] = "FETCH_DOC";
3066
+ ServerMsgCode2[ServerMsgCode2["DOC_UPDATE"] = 301] = "DOC_UPDATE";
3023
3067
  return ServerMsgCode2;
3024
3068
  })(ServerMsgCode || {});
3025
3069
 
@@ -3380,7 +3424,8 @@ function createRoom(options, config) {
3380
3424
  storage: makeEventSource(),
3381
3425
  history: makeEventSource(),
3382
3426
  storageDidLoad: makeEventSource(),
3383
- storageStatus: makeEventSource()
3427
+ storageStatus: makeEventSource(),
3428
+ docUpdated: makeEventSource()
3384
3429
  };
3385
3430
  const effects = config.mockedEffects || {
3386
3431
  authenticateAndConnect(auth, createWebSocket) {
@@ -3900,6 +3945,10 @@ function createRoom(options, config) {
3900
3945
  }
3901
3946
  break;
3902
3947
  }
3948
+ case 300 /* FETCH_DOC */: {
3949
+ eventHub.docUpdated.notify(message.data);
3950
+ break;
3951
+ }
3903
3952
  case 104 /* ROOM_STATE */: {
3904
3953
  updates.others.push(onRoomStateMessage(message));
3905
3954
  break;
@@ -4153,6 +4202,13 @@ ${Array.from(traces).join("\n\n")}`
4153
4202
  }
4154
4203
  return messages;
4155
4204
  }
4205
+ function updateDoc(data) {
4206
+ context.buffer.messages.push({
4207
+ type: 301 /* UPDATE_DOC */,
4208
+ data
4209
+ });
4210
+ tryFlushing();
4211
+ }
4156
4212
  function broadcastEvent(event, options2 = {
4157
4213
  shouldQueueEventIfNotReady: false
4158
4214
  }) {
@@ -4204,6 +4260,10 @@ ${Array.from(traces).join("\n\n")}`
4204
4260
  };
4205
4261
  });
4206
4262
  }
4263
+ function getDoc(vector = "") {
4264
+ context.buffer.messages.push({ type: 300 /* FETCH_DOC */, vector });
4265
+ tryFlushing();
4266
+ }
4207
4267
  function undo() {
4208
4268
  if (context.activeBatch) {
4209
4269
  throw new Error("undo is not allowed during a batch");
@@ -4328,7 +4388,8 @@ ${Array.from(traces).join("\n\n")}`
4328
4388
  storage: eventHub.storage.observable,
4329
4389
  history: eventHub.history.observable,
4330
4390
  storageDidLoad: eventHub.storageDidLoad.observable,
4331
- storageStatus: eventHub.storageStatus.observable
4391
+ storageStatus: eventHub.storageStatus.observable,
4392
+ docUpdated: eventHub.docUpdated.observable
4332
4393
  };
4333
4394
  function transition(event) {
4334
4395
  switch (event.type) {
@@ -4399,6 +4460,7 @@ ${Array.from(traces).join("\n\n")}`
4399
4460
  reconnect,
4400
4461
  // Presence
4401
4462
  updatePresence,
4463
+ updateDoc,
4402
4464
  broadcastEvent,
4403
4465
  // Storage
4404
4466
  batch,
@@ -4410,6 +4472,7 @@ ${Array.from(traces).join("\n\n")}`
4410
4472
  pause: pauseHistory,
4411
4473
  resume: resumeHistory
4412
4474
  },
4475
+ getDoc,
4413
4476
  getStorage,
4414
4477
  getStorageSnapshot,
4415
4478
  getStorageStatus,
@@ -4524,7 +4587,7 @@ function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
4524
4587
  // @ts-ignore (__PACKAGE_VERSION__ will be injected by the build script)
4525
4588
  true ? (
4526
4589
  /* istanbul ignore next */
4527
- "1.0.11"
4590
+ "1.0.12-yjs1"
4528
4591
  ) : "dev"}`
4529
4592
  );
4530
4593
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/core",
3
- "version": "1.0.11",
3
+ "version": "1.0.12-yjs1",
4
4
  "description": "Shared code and foundational internals for Liveblocks",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",