Skip to content

Commit

Permalink
feat: Added needsUpdate function/event (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Nov 19, 2022
1 parent 6b700b7 commit f4e8ee0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/conn/events/eventTypes.ts
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/conn/events/index.ts
Expand Up @@ -19,5 +19,6 @@ import './registerAuthenticatedEvent';
import './registerLogoutEvent';
import './registerMainLoadedEvent';
import './registerMainReadyEvent';
import './registerNeedsUpdateEvent';
import './registerQRCodeIdleEvent';
import './registerRequireAuthEvent';
33 changes: 33 additions & 0 deletions 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);
}
}
1 change: 1 addition & 0 deletions src/conn/functions/index.ts
Expand Up @@ -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';
29 changes: 29 additions & 0 deletions 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;
}

0 comments on commit f4e8ee0

Please sign in to comment.