playsocketjs 1.1.2 → 1.1.4

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.
@@ -2298,10 +2298,7 @@ class PlaySocket {
2298
2298
  this.#sentUpdates.delete(message.uuid); // If this is our sent update, delete it so that timeout check knows it was received
2299
2299
  this.#crdtManager.importPropertyUpdate(message.update);
2300
2300
  if (this.#crdtManager.didPropertiesChange) this.#triggerEvent("storageUpdated", this.getStorage);
2301
- if (this.#roomVersion != message.version) {
2302
- this.#triggerEvent("error", "Detected missing update – requesting full state.");
2303
- this.#requestStateSync();
2304
- }
2301
+ if (this.#roomVersion != message.version) this.#requestStateSyncIfConnected();
2305
2302
  break;
2306
2303
 
2307
2304
  case 'client_disconnected':
@@ -2374,12 +2371,17 @@ class PlaySocket {
2374
2371
  /**
2375
2372
  * If the local state is out of sync, request the room state
2376
2373
  */
2377
- async #requestStateSync() {
2378
- if (this.#pendingStateRequest) return; // Return if already requested
2374
+ async #requestStateSyncIfConnected(notSent = false) {
2375
+ if (this.#pendingStateRequest || !this.#initialized || this.#socket?.readyState !== WebSocket.OPEN) return; // Return if already requested, destroyed or connection not open
2376
+
2377
+ // Trigger corresponding events
2378
+ if (!notSent) this.#triggerEvent("error", "Detected skipped update – requesting state.");
2379
+ else this.#triggerEvent("error", "Sent update did not go through – requesting state.");
2380
+
2379
2381
  try {
2380
- Promise.race([
2381
- new Promise(async (resolve) => {
2382
- this.#pendingStateRequest = { resolve };
2382
+ await Promise.race([
2383
+ new Promise(async (resolve, reject) => {
2384
+ this.#pendingStateRequest = { resolve, reject };
2383
2385
  this.#sendToServer({ type: 'request_state' });
2384
2386
  }),
2385
2387
  this.#createTimeout("State request")
@@ -2410,8 +2412,8 @@ class PlaySocket {
2410
2412
  * @private
2411
2413
  */
2412
2414
  #sendToServer(data) {
2413
- if (!this.#socket || this.#socket.readyState !== WebSocket.OPEN) {
2414
- return console.error(ERROR_PREFIX + "Cannot send message - not connected.");
2415
+ if (!this.#socket || this.#socket?.readyState !== WebSocket.OPEN) {
2416
+ return console.warn(WARNING_PREFIX + "Cannot send message - not connected.");
2415
2417
  }
2416
2418
  try {
2417
2419
  this.#socket.send(encode(data));
@@ -2500,10 +2502,7 @@ class PlaySocket {
2500
2502
  uuid: updateUuid
2501
2503
  });
2502
2504
  setTimeout(() => {
2503
- if (this.#initialized && this.#sentUpdates.has(updateUuid)) {
2504
- this.#triggerEvent("error", "Sent update did not go through – requesting state.");
2505
- this.#requestStateSync();
2506
- }
2505
+ if (this.#sentUpdates.has(updateUuid)) this.#requestStateSyncIfConnected(true);
2507
2506
  }, TIMEOUT_MS);
2508
2507
  if (this.#crdtManager.didPropertiesChange) this.#triggerEvent("storageUpdated", this.getStorage); // Always trigger callback after send in case msgs are sent in the callback (which would break the order)
2509
2508
  }
@@ -2527,10 +2526,7 @@ class PlaySocket {
2527
2526
  uuid: updateUuid
2528
2527
  });
2529
2528
  setTimeout(() => {
2530
- if (this.#initialized && this.#sentUpdates.has(updateUuid)) {
2531
- this.#triggerEvent("error", "Sent update did not go through – requesting state.");
2532
- this.#requestStateSync();
2533
- }
2529
+ if (this.#sentUpdates.has(updateUuid)) this.#requestStateSyncIfConnected(true);
2534
2530
  }, TIMEOUT_MS);
2535
2531
  if (this.#crdtManager.didPropertiesChange) this.#triggerEvent("storageUpdated", this.getStorage);
2536
2532
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "module": "dist/playsocket-client.js",
6
6
  "exports": {