@open-wa/wa-automate 4.28.12 → 4.28.13

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.
@@ -40,6 +40,7 @@ export declare class Client {
40
40
  private _prio;
41
41
  private _pageListeners;
42
42
  private _registeredPageListeners;
43
+ private _onLogoutCallbacks;
43
44
  private _queues;
44
45
  /**
45
46
  * This is used to track if a listener is already used via webhook. Before, webhooks used to be set once per listener. Now a listener can be set via multiple webhooks, or revoked from a specific webhook.
@@ -111,13 +112,18 @@ export declare class Client {
111
112
  *
112
113
  * @event
113
114
  * @param fn callback
115
+ * @param priority A priority of -1 will mean the callback will be triggered after all the non -1 callbacks
114
116
  * @fires `true`
115
117
  */
116
118
  onLogout(fn: (loggedOut?: boolean) => any, priority?: number): Promise<boolean>;
117
119
  /**
118
120
  * Wait for the webhook queue to become idle. This is useful for ensuring webhooks are cleared before ending a process.
119
121
  */
120
- waitWhQIdle(): Promise<any>;
122
+ waitWhQIdle(): Promise<true | void>;
123
+ /**
124
+ * Wait for all queues to be empty
125
+ */
126
+ waitAllQEmpty(): Promise<true | void[]>;
121
127
  /**
122
128
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
123
129
  */
@@ -90,6 +90,7 @@ class Client {
90
90
  this._prio = Number.MAX_SAFE_INTEGER;
91
91
  this._pageListeners = [];
92
92
  this._registeredPageListeners = [];
93
+ this._onLogoutCallbacks = [];
93
94
  this._queues = {};
94
95
  /**
95
96
  * This is used to track if a listener is already used via webhook. Before, webhooks used to be set once per listener. Now a listener can be set via multiple webhooks, or revoked from a specific webhook.
@@ -260,16 +261,17 @@ class Client {
260
261
  }));
261
262
  }
262
263
  if (((_h = this._createConfig) === null || _h === void 0 ? void 0 : _h.deleteSessionDataOnLogout) || ((_j = this._createConfig) === null || _j === void 0 ? void 0 : _j.killClientOnLogout)) {
263
- this.onLogout(() => {
264
- var _a, _b;
265
- if ((_a = this._createConfig) === null || _a === void 0 ? void 0 : _a.deleteSessionDataOnLogout)
264
+ this.onLogout(() => __awaiter(this, void 0, void 0, function* () {
265
+ var _k, _l;
266
+ yield this.waitAllQEmpty();
267
+ if ((_k = this._createConfig) === null || _k === void 0 ? void 0 : _k.deleteSessionDataOnLogout)
266
268
  (0, browser_1.deleteSessionData)(this._createConfig);
267
- if ((_b = this._createConfig) === null || _b === void 0 ? void 0 : _b.killClientOnLogout) {
269
+ if ((_l = this._createConfig) === null || _l === void 0 ? void 0 : _l.killClientOnLogout) {
268
270
  console.log("Session logged out. Killing client");
269
271
  logging_1.log.warn("Session logged out. Killing client");
270
272
  this.kill("LOGGED_OUT");
271
273
  }
272
- });
274
+ }), -1);
273
275
  }
274
276
  });
275
277
  }
@@ -567,27 +569,47 @@ class Client {
567
569
  });
568
570
  if (this._registeredPageListeners.includes(event))
569
571
  return true;
572
+ this._registeredPageListeners.push(event);
573
+ logging_1.log.info(`setting page listener: ${event}`, this._registeredPageListeners);
570
574
  this._page.on(event, (...args) => __awaiter(this, void 0, void 0, function* () {
571
- return yield Promise.all(this._pageListeners.filter(l => l.event === event).sort((a, b) => (b.priority || 0) - (a.priority || 0)).map(l => l.callback(...args)));
575
+ yield Promise.all(this._pageListeners.filter(l => l.event === event).filter(({ priority }) => priority !== -1).sort((a, b) => (b.priority || 0) - (a.priority || 0)).map(l => l.callback(...args)));
576
+ yield Promise.all(this._pageListeners.filter(l => l.event === event).filter(({ priority }) => priority == -1).sort((a, b) => (b.priority || 0) - (a.priority || 0)).map(l => l.callback(...args)));
577
+ return;
572
578
  }));
573
- this._registeredPageListeners.push(event);
574
579
  }
575
580
  /**
576
581
  * Listens to a log out event
577
582
  *
578
583
  * @event
579
584
  * @param fn callback
585
+ * @param priority A priority of -1 will mean the callback will be triggered after all the non -1 callbacks
580
586
  * @fires `true`
581
587
  */
582
588
  onLogout(fn, priority) {
583
589
  return __awaiter(this, void 0, void 0, function* () {
584
- this.registerPageEventListener('framenavigated', (frame) => __awaiter(this, void 0, void 0, function* () {
590
+ const event = 'framenavigated';
591
+ this._onLogoutCallbacks.push({
592
+ callback: fn,
593
+ priority
594
+ });
595
+ if (!this._queues[event])
596
+ this._queues[event] = new p_queue_1.default({
597
+ concurrency: 1,
598
+ intervalCap: 1,
599
+ carryoverConcurrencyCount: true
600
+ });
601
+ if (this._registeredPageListeners.includes(event))
602
+ return true;
603
+ this.registerPageEventListener(event, (frame) => __awaiter(this, void 0, void 0, function* () {
585
604
  if (frame.url().includes('post_logout=1')) {
586
605
  console.log("LOGGED OUT");
587
606
  logging_1.log.warn("LOGGED OUT");
588
- yield fn(true);
607
+ yield Promise.all(this._onLogoutCallbacks.filter(c => c.priority !== -1).map(({ callback }) => this._queues[event].add(() => callback(true))));
608
+ yield this._queues[event].onEmpty();
609
+ yield Promise.all(this._onLogoutCallbacks.filter(c => c.priority == -1).map(({ callback }) => this._queues[event].add(() => callback(true))));
610
+ yield this._queues[event].onEmpty();
589
611
  }
590
- }), priority);
612
+ }), priority || 1);
591
613
  return true;
592
614
  });
593
615
  }
@@ -602,6 +624,18 @@ class Client {
602
624
  return true;
603
625
  });
604
626
  }
627
+ /**
628
+ * Wait for all queues to be empty
629
+ */
630
+ waitAllQEmpty() {
631
+ return __awaiter(this, void 0, void 0, function* () {
632
+ return yield Promise.all([
633
+ this._webhookQueue,
634
+ ...Object.values(this._queues)
635
+ ].filter(q => q).map(q => q === null || q === void 0 ? void 0 : q.onEmpty()));
636
+ return true;
637
+ });
638
+ }
605
639
  /**
606
640
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
607
641
  */
package/dist/cli/index.js CHANGED
@@ -109,11 +109,11 @@ function start() {
109
109
  client.onLogout(() => __awaiter(this, void 0, void 0, function* () {
110
110
  console.error('!!!! CLIENT LOGGED OUT !!!!');
111
111
  if (cliConfig && !cliConfig.noKillOnLogout) {
112
- yield client.waitWhQIdle();
112
+ yield client.waitAllQEmpty();
113
113
  console.error("Shutting down.");
114
114
  process.exit();
115
115
  }
116
- }));
116
+ }), -1);
117
117
  if (cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.botPressUrl) {
118
118
  spinner.info('Setting Up Botpress handler');
119
119
  (0, server_1.setupBotPressHandler)(cliConfig, client);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.28.12",
3
+ "version": "4.28.13",
4
4
  "licenseCheckUrl": "https://openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",