@open-wa/wa-automate 4.25.0 → 4.25.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.
@@ -39,6 +39,8 @@ export declare class Client {
39
39
  private _refreshing;
40
40
  private _l;
41
41
  private _prio;
42
+ private _pageListeners;
43
+ private _registeredPageListeners;
42
44
  private _queues;
43
45
  /**
44
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.
@@ -104,6 +106,7 @@ export declare class Client {
104
106
  *
105
107
  */
106
108
  private registerListener;
109
+ private registerPageEventListener;
107
110
  /**
108
111
  * Listens to a log out event
109
112
  *
@@ -111,7 +114,11 @@ export declare class Client {
111
114
  * @param fn callback
112
115
  * @fires `true`
113
116
  */
114
- onLogout(fn: (loggedOut?: boolean) => any): Promise<boolean>;
117
+ onLogout(fn: (loggedOut?: boolean) => any, priority?: number): Promise<boolean>;
118
+ /**
119
+ * Wait for the webhook queue to become idle. This is useful for ensuring webhooks are cleared before ending a process.
120
+ */
121
+ waitWhQIdle(): Promise<any>;
115
122
  /**
116
123
  * If you have set `onAnyMessage` or `onMessage` with the second parameter (PQueue options) then you may want to inspect their respective PQueue's.
117
124
  */
@@ -104,6 +104,8 @@ class Client {
104
104
  this._currentlyBeingKilled = false;
105
105
  this._refreshing = false;
106
106
  this._prio = Number.MAX_SAFE_INTEGER;
107
+ this._pageListeners = [];
108
+ this._registeredPageListeners = [];
107
109
  this._queues = {};
108
110
  /**
109
111
  * 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.
@@ -552,7 +554,21 @@ class Client {
552
554
  return res;
553
555
  });
554
556
  }
555
- // NON-STAMDARD LISTENERS
557
+ // NON-STANDARD LISTENERS
558
+ registerPageEventListener(_event, callback, priority) {
559
+ const event = _event;
560
+ this._pageListeners.push({
561
+ event,
562
+ callback,
563
+ priority
564
+ });
565
+ if (this._registeredPageListeners.includes(event))
566
+ return true;
567
+ this._page.on(event, (...args) => __awaiter(this, void 0, void 0, function* () {
568
+ 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)));
569
+ }));
570
+ this._registeredPageListeners.push(event);
571
+ }
556
572
  /**
557
573
  * Listens to a log out event
558
574
  *
@@ -560,17 +576,25 @@ class Client {
560
576
  * @param fn callback
561
577
  * @fires `true`
562
578
  */
563
- onLogout(fn) {
579
+ onLogout(fn, priority) {
564
580
  return __awaiter(this, void 0, void 0, function* () {
565
- yield this._page.on('request', request => {
566
- if (request.url() === "https://web.whatsapp.com/" && !this._refreshing)
567
- fn();
568
- });
569
- this.onStateChanged(state => {
570
- if (state === model_1.STATE.UNPAIRED) {
571
- fn();
581
+ this.registerPageEventListener('framenavigated', (frame) => __awaiter(this, void 0, void 0, function* () {
582
+ if (frame.url().includes('post_logout=1')) {
583
+ console.log("LOGGED OUT");
584
+ yield fn(true);
572
585
  }
573
- });
586
+ }), priority);
587
+ return true;
588
+ });
589
+ }
590
+ /**
591
+ * Wait for the webhook queue to become idle. This is useful for ensuring webhooks are cleared before ending a process.
592
+ */
593
+ waitWhQIdle() {
594
+ return __awaiter(this, void 0, void 0, function* () {
595
+ if (this._webhookQueue) {
596
+ return yield this._webhookQueue.onIdle();
597
+ }
574
598
  return true;
575
599
  });
576
600
  }
@@ -3309,7 +3333,7 @@ class Client {
3309
3333
  ...Object.keys(this._registeredWebhooks).map(webhookId => this._registeredWebhooks[webhookId]).filter(webhookEntry => webhookEntry.events.includes(event))
3310
3334
  ].map(({ id, url, requestConfig }) => axios_1.default(Object.assign({ method: 'post', url, data: this.prepEventData(_data, event, { webhook_id: id }) }, requestConfig)).catch(err => console.error(`WEBHOOK ERROR: `, url, err.message))));
3311
3335
  }));
3312
- }));
3336
+ }), 10000);
3313
3337
  }
3314
3338
  }
3315
3339
  });
package/dist/cli/index.js CHANGED
@@ -108,6 +108,7 @@ function start() {
108
108
  client.onLogout(() => __awaiter(this, void 0, void 0, function* () {
109
109
  console.error('!!!! CLIENT LOGGED OUT !!!!');
110
110
  if (cliConfig && !cliConfig.noKillOnLogout) {
111
+ yield client.waitWhQIdle();
111
112
  console.error("Shutting down.");
112
113
  process.exit();
113
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.25.0",
3
+ "version": "4.25.1",
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",