Skip to content

Commit

Permalink
moved onStateChanged off of middleware #82
Browse files Browse the repository at this point in the history
  • Loading branch information
smashah committed Feb 11, 2020
1 parent dcd83f9 commit 8371bc7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/api/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const getBase64 = async (url: string) => {
declare module WAPI {
const waitNewMessages: (rmCallback: boolean, callback: Function) => void;
const addAllNewMessagesListener: (callback: Function) => void;
const onStateChanged: (callback: Function) => void;
const onParticipantsChanged: (groupId: string, callback: Function) => any;
const sendMessage: (to: string, content: string) => void;
const reply: (to: string, content: string, quotedMsg: string | Message) => void;
Expand Down Expand Up @@ -138,7 +139,10 @@ export class Whatsapp {
public onStateChanged(fn: (state: string) => void) {
this.page.exposeFunction(ExposedFn.onStateChanged, (state: string) =>
fn(state)
);
).then(_ => this.page.evaluate(
() => {
WAPI.onStateChanged(s => window['onStateChanged'](s.state))
}));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var ExposedFn;
ExposedFn["OnMessage"] = "onMessage";
ExposedFn["OnAck"] = "onAck";
ExposedFn["OnParticipantsChanged"] = "onParticipantsChanged";
ExposedFn["OnStateChanged"] = "onStateChanged";
})(ExposedFn || (ExposedFn = {}));
/**
* Exposes [OnMessage] function
Expand All @@ -30,4 +29,4 @@ WAPI.waitNewAcknowledgements(function (data) {
window[ExposedFn.OnAck](message);
});
});
WAPI.onStateChanged(function (s) { return window[ExposedFn.OnStateChanged](s.state); });
// WAPI.onStateChanged(s => window[ExposedFn.OnStateChanged](s.state));
8 changes: 4 additions & 4 deletions src/lib/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ window.WAPI.getBufferedNewMessages = function (done) {

window.WAPI.sendImage = function (imgBase64, chatid, filename, caption, done) {
//var idUser = new window.Store.UserConstructor(chatid);
var idUser = new window.Store.UserConstructor(chatid, { intentionallyUsePrivateConstructor: true });
var idUser = new Store.WidFactory.createWid(chatId);
// create new chat
return Store.Chat.find(idUser).then((chat) => {
var mediaBlob = window.WAPI.base64ImageToFile(imgBase64, filename);
Expand All @@ -1409,7 +1409,7 @@ window.WAPI.sendImage = function (imgBase64, chatid, filename, caption, done) {

window.WAPI.sendVideoAsGif = function (imgBase64, chatid, filename, caption, done) {
//var idUser = new window.Store.UserConstructor(chatid);
var idUser = new window.Store.UserConstructor(chatid, { intentionallyUsePrivateConstructor: true });
var idUser = new Store.WidFactory.createWid(chatId);
// create new chat
return Store.Chat.find(idUser).then((chat) => {
var mediaBlob = window.WAPI.base64ImageToFile(imgBase64, filename);
Expand Down Expand Up @@ -1474,7 +1474,7 @@ window.WAPI.sendImageWithProduct = function (imgBase64, chatid, caption, bizNumb
caption
}

var idUser = new window.Store.UserConstructor(chatid, { intentionallyUsePrivateConstructor: true });
var idUser = new Store.WidFactory.createWid(chatId);

return Store.Chat.find(idUser).then((chat) => {
var mediaBlob = window.WAPI.base64ImageToFile(imgBase64, filename);
Expand Down Expand Up @@ -1690,7 +1690,7 @@ hydratedTitleText:"asdasd232"
const s = Store.Base2;
if(!s.BinaryProtocol)
window.Store.Base2.BinaryProtocol = new window.Store.bp.default(11);
var idUser = new window.Store.UserConstructor(chatId, { intentionallyUsePrivateConstructor: true });
var idUser = new Store.WidFactory.createWid(chatId);
var k = Store.createMessageKey({
...e,
to:idUser,
Expand Down
3 changes: 1 addition & 2 deletions src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ enum ExposedFn {
OnMessage = 'onMessage',
OnAck = 'onAck',
OnParticipantsChanged = 'onParticipantsChanged',
OnStateChanged = 'onStateChanged',
}

/**
Expand All @@ -40,4 +39,4 @@ WAPI.waitNewAcknowledgements(function (data) {
});
})

WAPI.onStateChanged(s => window[ExposedFn.OnStateChanged](s.state));
// WAPI.onStateChanged(s => window[ExposedFn.OnStateChanged](s.state));

0 comments on commit 8371bc7

Please sign in to comment.