@liveblocks/core 0.19.3-beta2 → 0.19.3-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.
package/dist/index.d.ts CHANGED
@@ -659,6 +659,7 @@ declare type Connection = {
659
659
  state: "failed";
660
660
  };
661
661
  declare type ConnectionState = Connection["state"];
662
+ declare type StorageStatus = "not-loaded" | "loading" | "synchronizing" | "synchronized";
662
663
  interface History {
663
664
  /**
664
665
  * Undoes the last operation executed by the current client.
@@ -736,9 +737,6 @@ declare type HistoryEvent = {
736
737
  canUndo: boolean;
737
738
  canRedo: boolean;
738
739
  };
739
- declare type PendingHasModificationsEvent = {
740
- hasPendingStorageModifications: boolean;
741
- };
742
740
  declare type BroadcastOptions = {
743
741
  /**
744
742
  * Whether or not event is queued if the connection is currently closed.
@@ -858,16 +856,18 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
858
856
  */
859
857
  (type: "history", listener: Callback<HistoryEvent>): () => void;
860
858
  /**
861
- * Subscribe to pending storage modifications updates.
859
+ * Subscribe to storage status changes.
862
860
  *
863
861
  * @returns Unsubscribe function.
864
862
  *
865
863
  * @example
866
- * room.subscribe("pending-storage-modifications", ({ hasPendingStorageModifications }) => {
867
- * // Do something
864
+ * room.subscribe("storage-status", (status) => {
865
+ * switch(status) {
866
+ * case
867
+ * }
868
868
  * });
869
869
  */
870
- (type: "pending-storage-modifications", listener: Callback<PendingHasModificationsEvent>): () => void;
870
+ (type: "storage-status", listener: Callback<StorageStatus>): () => void;
871
871
  };
872
872
  /**
873
873
  * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
@@ -982,9 +982,9 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
982
982
  */
983
983
  batch<T>(fn: () => T): T;
984
984
  /**
985
- * Whether or not the room has storage modifications that have not been acknowledged by the server.
985
+ * Get the storage status.
986
986
  */
987
- hasPendingStorageModifications(): boolean;
987
+ getStorageStatus(): StorageStatus;
988
988
  /**
989
989
  * Close room connection and try to reconnect
990
990
  */
package/dist/index.js CHANGED
@@ -2659,7 +2659,7 @@ var WebsocketCloseCodes = /* @__PURE__ */ ((WebsocketCloseCodes2) => {
2659
2659
 
2660
2660
  // src/room.ts
2661
2661
  function isRoomEventName(value) {
2662
- return value === "my-presence" || value === "others" || value === "event" || value === "error" || value === "connection" || value === "history";
2662
+ return value === "my-presence" || value === "others" || value === "event" || value === "error" || value === "connection" || value === "history" || value === "pending-storage-modifications";
2663
2663
  }
2664
2664
  var BACKOFF_RETRY_DELAYS = [250, 500, 1e3, 2e3, 4e3, 8e3, 1e4];
2665
2665
  var BACKOFF_RETRY_DELAYS_SLOW = [2e3, 3e4, 6e4, 3e5];
@@ -2726,7 +2726,7 @@ function makeStateMachine(state, config, mockedEffects) {
2726
2726
  storage: makeEventSource(),
2727
2727
  history: makeEventSource(),
2728
2728
  storageDidLoad: makeEventSource(),
2729
- pendingStorageModifications: makeEventSource()
2729
+ storageStatus: makeEventSource()
2730
2730
  };
2731
2731
  const effects = mockedEffects || {
2732
2732
  authenticate(auth, createWebSocket) {
@@ -2894,7 +2894,6 @@ function makeStateMachine(state, config, mockedEffects) {
2894
2894
  return op;
2895
2895
  }
2896
2896
  });
2897
- const hasPendingStorageModifs = hasPendingStorageModifications();
2898
2897
  for (const op of ops) {
2899
2898
  if (op.type === "presence") {
2900
2899
  const reverse = {
@@ -2946,11 +2945,7 @@ function makeStateMachine(state, config, mockedEffects) {
2946
2945
  }
2947
2946
  }
2948
2947
  }
2949
- if (hasPendingStorageModifs && hasPendingStorageModifications() === false) {
2950
- eventHub.pendingStorageModifications.notify({
2951
- hasPendingStorageModifications: false
2952
- });
2953
- }
2948
+ notifyStorageStatus();
2954
2949
  return {
2955
2950
  ops,
2956
2951
  reverse: output.reverse,
@@ -3046,8 +3041,8 @@ function makeStateMachine(state, config, mockedEffects) {
3046
3041
  );
3047
3042
  case "history":
3048
3043
  return eventHub.history.subscribe(callback);
3049
- case "pending-storage-modifications":
3050
- return eventHub.pendingStorageModifications.subscribe(
3044
+ case "storage-status":
3045
+ return eventHub.storageStatus.subscribe(
3051
3046
  callback
3052
3047
  );
3053
3048
  default:
@@ -3308,6 +3303,7 @@ function makeStateMachine(state, config, mockedEffects) {
3308
3303
  if (_getInitialStateResolver !== null) {
3309
3304
  _getInitialStateResolver();
3310
3305
  }
3306
+ notifyStorageStatus();
3311
3307
  eventHub.storageDidLoad.notify();
3312
3308
  break;
3313
3309
  }
@@ -3455,15 +3451,10 @@ function makeStateMachine(state, config, mockedEffects) {
3455
3451
  function tryFlushing() {
3456
3452
  const storageOps = state.buffer.storageOperations;
3457
3453
  if (storageOps.length > 0) {
3458
- const hasAlreadyPendingModifications = hasPendingStorageModifications();
3459
3454
  storageOps.forEach((op) => {
3460
3455
  state.offlineOperations.set(nn(op.opId), op);
3461
3456
  });
3462
- if (!hasAlreadyPendingModifications) {
3463
- eventHub.pendingStorageModifications.notify({
3464
- hasPendingStorageModifications: true
3465
- });
3466
- }
3457
+ notifyStorageStatus();
3467
3458
  }
3468
3459
  if (state.socket === null || state.socket.readyState !== state.socket.OPEN) {
3469
3460
  state.buffer.storageOperations = [];
@@ -3570,6 +3561,7 @@ function makeStateMachine(state, config, mockedEffects) {
3570
3561
  _getInitialStatePromise = new Promise(
3571
3562
  (resolve) => _getInitialStateResolver = resolve
3572
3563
  );
3564
+ notifyStorageStatus();
3573
3565
  }
3574
3566
  return _getInitialStatePromise;
3575
3567
  }
@@ -3690,9 +3682,6 @@ function makeStateMachine(state, config, mockedEffects) {
3690
3682
  _addToRealUndoStack(historyOps, batchUpdates);
3691
3683
  }
3692
3684
  }
3693
- function hasPendingStorageModifications() {
3694
- return state.offlineOperations.size > 0;
3695
- }
3696
3685
  function simulateSocketClose() {
3697
3686
  if (state.socket) {
3698
3687
  state.socket = null;
@@ -3701,6 +3690,23 @@ function makeStateMachine(state, config, mockedEffects) {
3701
3690
  function simulateSendCloseEvent(event) {
3702
3691
  onClose(event);
3703
3692
  }
3693
+ function getStorageStatus() {
3694
+ if (state.root !== void 0) {
3695
+ return state.offlineOperations.size === 0 ? "synchronized" : "synchronizing";
3696
+ }
3697
+ if (_getInitialStatePromise !== null) {
3698
+ return "loading";
3699
+ }
3700
+ return "not-loaded";
3701
+ }
3702
+ let _lastStorageStatus = getStorageStatus();
3703
+ function notifyStorageStatus() {
3704
+ const storageStatus = getStorageStatus();
3705
+ if (_lastStorageStatus !== storageStatus) {
3706
+ _lastStorageStatus = storageStatus;
3707
+ eventHub.storageStatus.notify(storageStatus);
3708
+ }
3709
+ }
3704
3710
  return {
3705
3711
  onClose,
3706
3712
  onMessage,
@@ -3725,9 +3731,9 @@ function makeStateMachine(state, config, mockedEffects) {
3725
3731
  canRedo,
3726
3732
  pauseHistory,
3727
3733
  resumeHistory,
3728
- hasPendingStorageModifications,
3729
3734
  getStorage,
3730
3735
  getStorageSnapshot,
3736
+ getStorageStatus,
3731
3737
  events: {
3732
3738
  customEvent: eventHub.customEvent.observable,
3733
3739
  others: eventHub.others.observable,
@@ -3737,7 +3743,7 @@ function makeStateMachine(state, config, mockedEffects) {
3737
3743
  storage: eventHub.storage.observable,
3738
3744
  history: eventHub.history.observable,
3739
3745
  storageDidLoad: eventHub.storageDidLoad.observable,
3740
- pendingStorageModifications: eventHub.pendingStorageModifications
3746
+ storageStatus: eventHub.storageStatus.observable
3741
3747
  },
3742
3748
  getConnectionState,
3743
3749
  isSelfAware: () => isConnectionSelfAware(state.connection.current),
@@ -3810,6 +3816,7 @@ function createRoom(options, config) {
3810
3816
  broadcastEvent: machine.broadcastEvent,
3811
3817
  getStorage: machine.getStorage,
3812
3818
  getStorageSnapshot: machine.getStorageSnapshot,
3819
+ getStorageStatus: machine.getStorageStatus,
3813
3820
  events: machine.events,
3814
3821
  batch: machine.batch,
3815
3822
  history: {
@@ -3820,7 +3827,6 @@ function createRoom(options, config) {
3820
3827
  pause: machine.pauseHistory,
3821
3828
  resume: machine.resumeHistory
3822
3829
  },
3823
- hasPendingStorageModifications: machine.hasPendingStorageModifications,
3824
3830
  __INTERNAL_DO_NOT_USE: {
3825
3831
  simulateCloseWebsocket: machine.simulateSocketClose,
3826
3832
  simulateSendCloseEvent: machine.simulateSendCloseEvent
@@ -3849,7 +3855,7 @@ function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
3849
3855
  const ws = WebSocketPolyfill || WebSocket;
3850
3856
  return (token) => {
3851
3857
  return new ws(
3852
- `${liveblocksServer}/?token=${token}&version=${true ? "0.19.3-beta2" : "dev"}`
3858
+ `${liveblocksServer}/?token=${token}&version=${true ? "0.19.3-beta4" : "dev"}`
3853
3859
  );
3854
3860
  };
3855
3861
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/core",
3
- "version": "0.19.3-beta2",
3
+ "version": "0.19.3-beta4",
4
4
  "description": "Shared code and foundational internals for Liveblocks",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",