@inditextech/weave-store-azure-web-pubsub 4.2.0-SNAPSHOT.307.1 → 4.2.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.
package/dist/client.d.ts CHANGED
@@ -216,6 +216,9 @@ type MessageHandler = (encoder: Encoder, decoder: Decoder, client: WeaveStoreAzu
216
216
  type WeaveStoreAzureWebPubsubSyncHandlerDestroyRoomStatusKeys = keyof typeof WEAVE_STORE_AZURE_WEB_PUBSUB_DESTROY_ROOM_STATUS;
217
217
  type WeaveStoreAzureWebPubsubSyncHandlerDestroyRoomStatus = (typeof WEAVE_STORE_AZURE_WEB_PUBSUB_DESTROY_ROOM_STATUS)[WeaveStoreAzureWebPubsubSyncHandlerDestroyRoomStatusKeys];
218
218
  type WeaveStoreAzureWebPubSubSyncClientOptions = {
219
+ clientConnection: {
220
+ timeoutMs: number;
221
+ };
219
222
  heartbeat: {
220
223
  checkWindowTimeMs: number;
221
224
  checkIntervalMs: number;
@@ -270,7 +273,7 @@ declare class WeaveStoreAzureWebPubSubSyncClient extends Emittery {
270
273
  getClientId(): string;
271
274
  saveLastSyncResponse(): void;
272
275
  simulateWebsocketError(): void;
273
- disconnect(): void;
276
+ disconnect(): Promise<void>;
274
277
  setFetchClient(fetchClient?: FetchClient): void;
275
278
  fetchConnectionUrl(connectionUrlExtraParams?: Record<string, string>): Promise<string>;
276
279
  createWebSocket(connectionUrlExtraParams?: Record<string, string>): Promise<ReconnectingWebSocket>;
@@ -300,7 +303,7 @@ declare class WeaveStoreAzureWebPubsub extends WeaveStore {
300
303
  getClientId(): string | null;
301
304
  switchToRoom(roomId: string, roomData: Uint8Array | FetchInitialState | undefined): Promise<void>;
302
305
  connect(extraParams?: Record<string, string>): Promise<void>;
303
- disconnect(): void;
306
+ disconnect(): Promise<void>;
304
307
  simulateWebsocketError(): void;
305
308
  destroy(): void;
306
309
  handleAwarenessChange(emit?: boolean): void;
package/dist/client.js CHANGED
@@ -3738,14 +3738,17 @@ const WEAVE_STORE_AZURE_WEB_PUBSUB_DESTROY_ROOM_STATUS = {
3738
3738
  ["NOT_CONNECTED"]: "not-connected",
3739
3739
  ["DESTROYED"]: "destroyed"
3740
3740
  };
3741
- const WEAVE_STORE_AZURE_WEB_PUBSUB_SYNC_CLIENT_DEFAULT_OPTIONS = { heartbeat: {
3742
- checkWindowTimeMs: 1e4,
3743
- checkIntervalMs: 5e3
3744
- } };
3741
+ const WEAVE_STORE_AZURE_WEB_PUBSUB_SYNC_CLIENT_DEFAULT_OPTIONS = {
3742
+ clientConnection: { timeoutMs: 6e4 },
3743
+ heartbeat: {
3744
+ checkWindowTimeMs: 6e4,
3745
+ checkIntervalMs: 15e3
3746
+ }
3747
+ };
3745
3748
  const WEAVE_STORE_AZURE_WEB_PUBSUB_SYNC_HOST_DEFAULT_OPTIONS = {
3746
- heartbeat: { sendIntervalMs: 2500 },
3749
+ heartbeat: { sendIntervalMs: 5e3 },
3747
3750
  resync: {
3748
- checkIntervalMs: 5e3,
3751
+ checkIntervalMs: 6e4,
3749
3752
  attemptsLimit: 12
3750
3753
  }
3751
3754
  };
@@ -3891,7 +3894,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
3891
3894
  simulateWebsocketError() {
3892
3895
  if (this._ws) this._ws._ws.close(4e3, new Error("Simulated error for testing"));
3893
3896
  }
3894
- disconnect() {
3897
+ async disconnect() {
3895
3898
  if (this._ws !== null) {
3896
3899
  const encoder = createEncoder();
3897
3900
  writeVarUint(encoder, messageAwareness);
@@ -3919,7 +3922,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
3919
3922
  }
3920
3923
  }
3921
3924
  const controller = new AbortController();
3922
- const timeout = setTimeout(() => controller.abort(), 5e3);
3925
+ const timeout = setTimeout(() => controller.abort(), this._synClientOptions.clientConnection.timeoutMs);
3923
3926
  const res = await this._fetchClient(connectionURL.toString(), { signal: controller.signal }).finally(() => clearTimeout(timeout));
3924
3927
  if (res.ok) {
3925
3928
  const data = await res.json();
@@ -3940,6 +3943,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
3940
3943
  });
3941
3944
  url = await this.fetchConnectionUrl(connectionUrlExtraParams);
3942
3945
  } catch (ex) {
3946
+ console.error(ex);
3943
3947
  error = ex;
3944
3948
  } finally {
3945
3949
  if (error) this.instance.handleConnectionStatusChange(WEAVE_STORE_CONNECTION_STATUS.ERROR);
@@ -3965,19 +3969,21 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
3965
3969
  }
3966
3970
  this.setAndEmitStatusInfo(WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS.ERROR);
3967
3971
  });
3968
- websocket.onmessage = (event) => {
3972
+ websocket.onmessage = async (event) => {
3969
3973
  if (event.data === null) return;
3970
3974
  const message = JSON.parse(event.data.toString());
3971
3975
  if (message.type === MessageType.System) return;
3972
3976
  const messageData = message.data;
3973
3977
  if (messageData.type === "heartbeat") {
3974
- this._lastHeartbeatTime = Date.now();
3978
+ const actualDate = Date.now();
3979
+ this._lastHeartbeatTime = actualDate;
3975
3980
  return;
3976
3981
  }
3977
3982
  if (messageData.type === "resync") {
3978
3983
  const encoder$1 = createEncoder();
3979
3984
  writeVarUint(encoder$1, messageSyncStep1);
3980
- writeUpdate(encoder$1, yjs_default.encodeStateAsUpdate(this.doc));
3985
+ const stateVector = yjs_default.encodeStateVector(this.doc);
3986
+ writeUpdate(encoder$1, yjs_default.encodeStateAsUpdate(this.doc, stateVector));
3981
3987
  sendToControlGroup(this, this.topic, MessageDataType.Sync, toUint8Array(encoder$1));
3982
3988
  return;
3983
3989
  }
@@ -4000,14 +4006,13 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
4000
4006
  removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((x) => x !== this.doc.clientID), this);
4001
4007
  }
4002
4008
  };
4003
- websocket.onopen = () => {
4009
+ websocket.onopen = async () => {
4004
4010
  this._lastHeartbeatTime = Date.now();
4005
4011
  this.setAndEmitStatusInfo(WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS.CONNECTED);
4006
4012
  this._wsConnected = true;
4007
4013
  this._initialized = true;
4008
4014
  this._connectionRetries = this._connectionRetries++;
4009
4015
  console.log("✅ [Azure Web PubSub] connected");
4010
- this.setupCheckHeartbeat();
4011
4016
  console.log(`🔌 [Azure Web PubSub] join room <${this.topic}>`);
4012
4017
  joinGroup(this, this.topic);
4013
4018
  console.log(`✅ [Azure Web PubSub] room <${this.topic}> joined`);
@@ -4030,6 +4035,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
4030
4035
  const u82 = toUint8Array(encoder);
4031
4036
  sendToControlGroup(this, this.topic, MessageDataType.Awareness, u82);
4032
4037
  }
4038
+ this.setupCheckHeartbeat();
4033
4039
  };
4034
4040
  this.setAndEmitStatusInfo(WEAVE_STORE_AZURE_WEB_PUBSUB_CONNECTION_STATUS.CONNECTING);
4035
4041
  return websocket;
@@ -4039,10 +4045,11 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
4039
4045
  this.emit("status", this._status);
4040
4046
  }
4041
4047
  setupCheckHeartbeat() {
4042
- this._checkHeartbeatId = setInterval(() => {
4048
+ this._checkHeartbeatId = setInterval(async () => {
4043
4049
  const now = Date.now();
4044
4050
  if (now - this._lastHeartbeatTime > this._synClientOptions.heartbeat.checkWindowTimeMs) {
4045
- this.disconnect();
4051
+ if (this._checkHeartbeatId) clearInterval(this._checkHeartbeatId);
4052
+ await this.disconnect();
4046
4053
  this.createWebSocket();
4047
4054
  }
4048
4055
  }, this._synClientOptions.heartbeat.checkIntervalMs);
@@ -4057,7 +4064,7 @@ var WeaveStoreAzureWebPubSubSyncClient = class extends Emittery {
4057
4064
  }
4058
4065
  };
4059
4066
  function safeSend(data) {
4060
- const MAX_BYTES = 64 * 1024;
4067
+ const MAX_BYTES = 512 * 1024;
4061
4068
  const bytes = new TextEncoder().encode(data);
4062
4069
  if (bytes.byteLength > MAX_BYTES) return false;
4063
4070
  return true;
@@ -4094,7 +4101,7 @@ function chunkString(str, size$1) {
4094
4101
  }
4095
4102
  function sendToControlGroupChunked(client, group, type, u8) {
4096
4103
  const base64Data = uint8ToBase64(u8);
4097
- const CHUNK_SIZE = 60 * 1024;
4104
+ const CHUNK_SIZE = 512 * 1024;
4098
4105
  const chunks = chunkString(base64Data, CHUNK_SIZE);
4099
4106
  const payloadId = v4();
4100
4107
  for (let i = 0; i < chunks.length; i++) {
@@ -4191,7 +4198,7 @@ var WeaveStoreAzureWebPubsub = class extends WeaveStore {
4191
4198
  async switchToRoom(roomId, roomData) {
4192
4199
  this.instance.emitEvent("onRoomSwitchingStart", { room: roomId });
4193
4200
  this.actualStatus = WEAVE_STORE_CONNECTION_STATUS.SWITCHING_ROOM;
4194
- this.disconnect();
4201
+ await this.disconnect();
4195
4202
  this.restartDocument();
4196
4203
  await this.instance.switchRoom();
4197
4204
  this.roomId = roomId;
@@ -4207,8 +4214,8 @@ var WeaveStoreAzureWebPubsub = class extends WeaveStore {
4207
4214
  this.instance.emitEvent("onStoreRoomChanged", { room: this.roomId });
4208
4215
  await this.provider.connect(extraParams);
4209
4216
  }
4210
- disconnect() {
4211
- this.provider.disconnect();
4217
+ async disconnect() {
4218
+ await this.provider.disconnect();
4212
4219
  }
4213
4220
  simulateWebsocketError() {
4214
4221
  this.provider.simulateWebsocketError();