@softtynet/zca-js 2.1.19 → 2.1.20

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.
@@ -142,8 +142,25 @@ export class Listener extends EventEmitter {
142
142
  logger(this.ctx).verbose(`Rotating endpoint to ${this.wsURL}`);
143
143
  }
144
144
  start({ retryOnClose = false } = {}) {
145
- if (this.ws)
146
- throw new ZaloApiError("Already started");
145
+ if (this.retryTimeout) {
146
+ clearTimeout(this.retryTimeout);
147
+ this.retryTimeout = undefined;
148
+ }
149
+ if (this.ws) {
150
+ const state = this.ws.readyState;
151
+ if (state === WebSocket.CONNECTING || state === WebSocket.OPEN) {
152
+ logger(this.ctx).warn("Listener is already active or connecting. Skipping duplicate start().");
153
+ return;
154
+ }
155
+ try {
156
+ this.ws.removeAllListeners();
157
+ this.ws.close();
158
+ }
159
+ catch (_a) {
160
+ // ignore
161
+ }
162
+ this.ws = null;
163
+ }
147
164
  this.wsURL = makeURL(this.ctx, this.urls[this.rotateCount], {
148
165
  t: Date.now(),
149
166
  });
@@ -185,7 +202,12 @@ export class Listener extends EventEmitter {
185
202
  }
186
203
  this.emit("reconnecting", event.code);
187
204
  this.retryTimeout = setTimeout(() => {
188
- this.start({ retryOnClose: true });
205
+ try {
206
+ this.start({ retryOnClose: true });
207
+ }
208
+ catch (error) {
209
+ logger(this.ctx).error("Failed to auto-restart listener on retry timeout:", error);
210
+ }
189
211
  }, retry);
190
212
  }
191
213
  else {
@@ -154,8 +154,25 @@ class Listener extends EventEmitter {
154
154
  utils.logger(this.ctx).verbose(`Rotating endpoint to ${this.wsURL}`);
155
155
  }
156
156
  start({ retryOnClose = false } = {}) {
157
- if (this.ws)
158
- throw new ZaloApiError.ZaloApiError("Already started");
157
+ if (this.retryTimeout) {
158
+ clearTimeout(this.retryTimeout);
159
+ this.retryTimeout = undefined;
160
+ }
161
+ if (this.ws) {
162
+ const state = this.ws.readyState;
163
+ if (state === WebSocket.CONNECTING || state === WebSocket.OPEN) {
164
+ utils.logger(this.ctx).warn("Listener is already active or connecting. Skipping duplicate start().");
165
+ return;
166
+ }
167
+ try {
168
+ this.ws.removeAllListeners();
169
+ this.ws.close();
170
+ }
171
+ catch (_a) {
172
+ // ignore
173
+ }
174
+ this.ws = null;
175
+ }
159
176
  this.wsURL = utils.makeURL(this.ctx, this.urls[this.rotateCount], {
160
177
  t: Date.now(),
161
178
  });
@@ -197,7 +214,12 @@ class Listener extends EventEmitter {
197
214
  }
198
215
  this.emit("reconnecting", event.code);
199
216
  this.retryTimeout = setTimeout(() => {
200
- this.start({ retryOnClose: true });
217
+ try {
218
+ this.start({ retryOnClose: true });
219
+ }
220
+ catch (error) {
221
+ utils.logger(this.ctx).error("Failed to auto-restart listener on retry timeout:", error);
222
+ }
201
223
  }, retry);
202
224
  }
203
225
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softtynet/zca-js",
3
- "version": "2.1.19",
3
+ "version": "2.1.20",
4
4
  "description": "Unofficial Zalo API for JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",