@spacemolt/lib 14.2.0 → 14.2.1

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/account.d.ts CHANGED
@@ -156,6 +156,8 @@ export declare class Account {
156
156
  private readonly stateListeners;
157
157
  private userClosing;
158
158
  private reconnecting;
159
+ /** The close seen on the current reconnect attempt's socket, if any — the loop reads it to decide whether the attempt really succeeded. */
160
+ private attemptClose;
159
161
  private mutationLane;
160
162
  private marketSubscribedState;
161
163
  private subscribedMarketBaseId;
package/dist/index.js CHANGED
@@ -4227,6 +4227,9 @@ function normalizeReconnect(opt) {
4227
4227
  maxDelayMs: o.maxDelayMs ?? 30000
4228
4228
  };
4229
4229
  }
4230
+ function isTerminalClose(err) {
4231
+ return err.code === CLOSE_CODE.SESSION_REPLACED || err.code === CLOSE_CODE.AUTH_TIMEOUT;
4232
+ }
4230
4233
  function retryAfterMs(err) {
4231
4234
  const retryAfter = err.details?.retry_after;
4232
4235
  const fromDetails = typeof retryAfter === "number" ? retryAfter : undefined;
@@ -4271,6 +4274,7 @@ class Account {
4271
4274
  stateListeners = new Set;
4272
4275
  userClosing = false;
4273
4276
  reconnecting = false;
4277
+ attemptClose = null;
4274
4278
  mutationLane = Promise.resolve();
4275
4279
  marketSubscribedState = false;
4276
4280
  subscribedMarketBaseId;
@@ -4922,6 +4926,10 @@ class Account {
4922
4926
  this.pendingAuth = null;
4923
4927
  auth.onError(err);
4924
4928
  }
4929
+ if (this.reconnecting) {
4930
+ this.attemptClose = err;
4931
+ return;
4932
+ }
4925
4933
  if (this.shouldReconnect(err)) {
4926
4934
  this.reconnectLoop(err);
4927
4935
  } else if (!this.userClosing) {
@@ -4933,9 +4941,7 @@ class Account {
4933
4941
  return false;
4934
4942
  if (!this.credentialsProvider)
4935
4943
  return false;
4936
- if (err.code === CLOSE_CODE.SESSION_REPLACED || err.code === CLOSE_CODE.AUTH_TIMEOUT)
4937
- return false;
4938
- return true;
4944
+ return !isTerminalClose(err);
4939
4945
  }
4940
4946
  async reconnectLoop(err) {
4941
4947
  if (this.reconnecting || !this.reconnectConfig || !this.credentialsProvider)
@@ -4944,6 +4950,10 @@ class Account {
4944
4950
  const cfg = this.reconnectConfig;
4945
4951
  const retryAfterMs = retryAfterMsFromClose(err);
4946
4952
  for (let attempt = 1;attempt <= cfg.maxRetries; attempt++) {
4953
+ if (this.userClosing) {
4954
+ this.reconnecting = false;
4955
+ return;
4956
+ }
4947
4957
  notifyListeners(this.reconnectingListeners, "onReconnecting", attempt);
4948
4958
  const backoff = attempt === 1 && retryAfterMs !== undefined ? retryAfterMs : Math.min(cfg.baseDelayMs * 2 ** (attempt - 1), cfg.maxDelayMs);
4949
4959
  await delay(backoff);
@@ -4951,15 +4961,22 @@ class Account {
4951
4961
  this.reconnecting = false;
4952
4962
  return;
4953
4963
  }
4964
+ this.attemptClose = null;
4954
4965
  try {
4955
4966
  await this.reconnectOnce();
4956
- this.reconnecting = false;
4957
- notifyListeners(this.reconnectedListeners, "onReconnected");
4958
- return;
4967
+ if (!this.attemptClose) {
4968
+ this.reconnecting = false;
4969
+ notifyListeners(this.reconnectedListeners, "onReconnected");
4970
+ return;
4971
+ }
4959
4972
  } catch {}
4973
+ if (this.attemptClose && isTerminalClose(this.attemptClose))
4974
+ break;
4960
4975
  }
4976
+ const finalErr = this.attemptClose && isTerminalClose(this.attemptClose) ? this.attemptClose : err;
4961
4977
  this.reconnecting = false;
4962
- notifyListeners(this.disconnectedListeners, "onDisconnected", err);
4978
+ if (!this.userClosing)
4979
+ notifyListeners(this.disconnectedListeners, "onDisconnected", finalErr);
4963
4980
  }
4964
4981
  async reconnectOnce() {
4965
4982
  if (!this.credentialsProvider)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spacemolt/lib",
3
- "version": "14.2.0",
3
+ "version": "14.2.1",
4
4
  "description": "The definitive TypeScript library for SpaceMolt. WebSocket-v2-first, multi-account, with local state caches. Internals are regenerated from the server's OpenAPI spec.",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",