Skip to content

Commit

Permalink
fix: Migrated State store to WA-JS
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 28, 2021
1 parent afdb3b4 commit e040978
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 59 deletions.
1 change: 0 additions & 1 deletion src/api/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ export { downloadFileToBase64 } from './download-file';
export { stickerSelect, resizeImg } from './select-sticker';
export { scrapeImg } from './scrape-img-qr';
export { scrapeLogin } from './scrape-login';
export { scrapeDesconnected } from './scrape-desconnect';
export { evaluateAndReturn } from './evaluate-and-return';
37 changes: 0 additions & 37 deletions src/api/helpers/scrape-desconnect.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/layers/host.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export class HostLayer {
public async getConnectionState(): Promise<SocketState> {
return await evaluateAndReturn(this.page, () => {
//@ts-ignore
return Store.State.default.state;
return WPP.whatsapp.State.state as SocketState;
});
}

Expand Down
20 changes: 9 additions & 11 deletions src/lib/wapi/functions/phoneWatchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,28 @@ async function sendPing() {
// Check only if the interface is in CHAT and not disconnected
if (
window.Store.Stream.mode !== 'MAIN' ||
window.Store.State.default.state === 'TIMEOUT'
WPP.whatsapp.State.state === 'TIMEOUT'
) {
return;
}

// Start phoneWatchdog if ping fails
if (!pong) {
window.Store.State.default.phoneWatchdog.activate();
window.Store.State.default.phoneWatchdog.poke(250);
WPP.whatsapp.State.phoneWatchdog.activate();
WPP.whatsapp.State.phoneWatchdog.poke(250);
return;
}

// Reset ping state
pong = false;

// Send a ping request
return window.Store.State.default
.sendBasic({
tag: window.Store.State.default.tag('ping'),
data: ['admin', 'test'],
})
.then(() => {
pong = true;
});
return WPP.whatsapp.State.sendBasic({
tag: WPP.whatsapp.State.tag('ping'),
data: ['admin', 'test'],
}).then(() => {
pong = true;
});
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib/wapi/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ declare global {
}
const WPP: typeof wajs;
}

declare global {
interface Window {
Store: any;
}
}
8 changes: 4 additions & 4 deletions src/lib/wapi/listeners/add-on-state-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
export function addOnStateChange() {
let initialized = false;
let getData = () => {
return window.Store.State.default.state;
return WPP.whatsapp.State.state;
};

window.WAPI.onStateChange = function (callback) {
window.WAPI.waitForStore('State', () => {
window.Store.State.default.on('change:state', () => callback(getData()));
WPP.whatsapp.State.on('change:state', () => callback(getData()));
if (!initialized) {
initialized = true;
callback(getData());
Expand All @@ -36,11 +36,11 @@ export function addOnStateChange() {
export function addOnStreamChange() {
let initialized = false;
let getData = () => {
return window.Store.State.default.stream;
return WPP.whatsapp.State.stream;
};
window.WAPI.onStreamChange = function (callback) {
window.WAPI.waitForStore('State', () => {
window.Store.State.default.on('change:stream', () => callback(getData()));
WPP.whatsapp.State.on('change:stream', () => callback(getData()));
if (!initialized) {
initialized = true;
callback(getData());
Expand Down
4 changes: 0 additions & 4 deletions src/lib/wapi/store/store-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export const storeObjects = [
conditions: (module) =>
module.default && module.default.killServiceWorker ? module : null,
},
{
id: 'State',
conditions: (module) => (module.STATE && module.STREAM ? module : null),
},
{
id: 'WapDelete',
conditions: (module) =>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ if (typeof window.WAPI === 'undefined') {
};

window.WAPI.takeOver = async function () {
await Store.State.default.takeover();
await WPP.whatsapp.State.takeover();
return true;
};

Expand Down

0 comments on commit e040978

Please sign in to comment.