From f4e8ee07ace65aa1b4a931d6cef19bc901ae41ad Mon Sep 17 00:00:00 2001 From: Cleiton Carvalho Date: Fri, 18 Nov 2022 23:30:48 -0300 Subject: [PATCH] feat: Added needsUpdate function/event (#763) --- src/conn/events/eventTypes.ts | 11 +++++++ src/conn/events/index.ts | 1 + src/conn/events/registerNeedsUpdateEvent.ts | 33 +++++++++++++++++++++ src/conn/functions/index.ts | 1 + src/conn/functions/needsUpdate.ts | 29 ++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 src/conn/events/registerNeedsUpdateEvent.ts create mode 100644 src/conn/functions/needsUpdate.ts diff --git a/src/conn/events/eventTypes.ts b/src/conn/events/eventTypes.ts index 2f29fb7fc4..c991dc046c 100644 --- a/src/conn/events/eventTypes.ts +++ b/src/conn/events/eventTypes.ts @@ -52,6 +52,17 @@ export interface ConnEventTypes { * ``` */ 'conn.main_ready': undefined; + /** + * Triggered when a whatsapp web update is requested + * + * @example + * ```javascript + * WPP.on('conn.needs_update', () => { + * // Your code + * }); + * ``` + */ + 'conn.needs_update': undefined; 'conn.qrcode_idle': undefined; 'conn.require_auth': undefined; } diff --git a/src/conn/events/index.ts b/src/conn/events/index.ts index 2701b67203..4e5c47c6ae 100644 --- a/src/conn/events/index.ts +++ b/src/conn/events/index.ts @@ -19,5 +19,6 @@ import './registerAuthenticatedEvent'; import './registerLogoutEvent'; import './registerMainLoadedEvent'; import './registerMainReadyEvent'; +import './registerNeedsUpdateEvent'; import './registerQRCodeIdleEvent'; import './registerRequireAuthEvent'; diff --git a/src/conn/events/registerNeedsUpdateEvent.ts b/src/conn/events/registerNeedsUpdateEvent.ts new file mode 100644 index 0000000000..a75c30fc3b --- /dev/null +++ b/src/conn/events/registerNeedsUpdateEvent.ts @@ -0,0 +1,33 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { internalEv } from '../../eventEmitter'; +import * as webpack from '../../webpack'; +import { Stream } from '../../whatsapp'; + +webpack.onInjected(register); + +function register() { + const trigger = async () => { + internalEv.emit('conn.needs_update'); + }; + + if (Stream.needsUpdate) { + trigger(); + } else { + Stream.on('change:needsUpdate', trigger); + } +} diff --git a/src/conn/functions/index.ts b/src/conn/functions/index.ts index 3a218b06e5..e299bd3cd5 100644 --- a/src/conn/functions/index.ts +++ b/src/conn/functions/index.ts @@ -26,6 +26,7 @@ export { isMultiDevice } from './isMultiDevice'; export { isRegistered } from './isRegistered'; export { logout } from './logout'; export { markAvailable, markUnavailable } from './markAvailable'; +export { needsUpdate } from './needsUpdate'; export { refreshQR } from './refreshQR'; export { setKeepAlive } from './setKeepAlive'; export { setMultiDevice } from './setMultiDevice'; diff --git a/src/conn/functions/needsUpdate.ts b/src/conn/functions/needsUpdate.ts new file mode 100644 index 0000000000..6e0d4bcc6c --- /dev/null +++ b/src/conn/functions/needsUpdate.ts @@ -0,0 +1,29 @@ +/*! + * Copyright 2022 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Stream } from '../../whatsapp'; + +/** + * Check if whatsapp web is asking for update + * + * @example + * ```javascript + * const needsUpdate = WPP.conn.needsUpdate(); + * ``` + */ +export function needsUpdate(): boolean { + return Stream.needsUpdate; +}