Skip to content

Commit

Permalink
refactor: Reorganized conn events
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Feb 27, 2022
1 parent bc72d11 commit b3f331b
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/conn/eventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ const debug = Debug('WA-JS:event:conn');
export type UnsubscribeFn = () => void;

export interface EventTypes {
change: AuthCode;
idle: undefined;
auth_code_change: AuthCode | null;
logout: undefined;
qrcode_idle: undefined;
}

export const eventEmitter = new Emittery<EventTypes, EventTypes>({
Expand Down
4 changes: 2 additions & 2 deletions src/conn/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* limitations under the License.
*/

import './registerChangeEvent';
import './registerIdleEvent';
import './registerAuthCodeChangeEvent';
import './registerQRCodeIdleEvent';
import './registerLogoutEvent';
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import { eventEmitter } from '../eventEmitter';

const debug = Debug('WA-JS:conn');

webpack.onInjected(() => registerChangeEvent());
webpack.onInjected(registerAuthCodeChangeEvent);

function registerChangeEvent() {
Conn.on('change:ref', async () => {
function registerAuthCodeChangeEvent() {
const trigger = async () => {
const authCode = await getAuthCode().catch(() => null);
if (authCode) {
eventEmitter.emit('change', authCode);
}
});
eventEmitter.emit('auth_code_change', authCode);
};

trigger();
Conn.on('change:ref', trigger);

debug('change event registered');
}
2 changes: 1 addition & 1 deletion src/conn/events/registerLogoutEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { eventEmitter } from '../eventEmitter';

const debug = Debug('WA-JS:conn');

webpack.onInjected(() => registerLogoutEvent());
webpack.onInjected(registerLogoutEvent);

function registerLogoutEvent() {
Cmd.on('logout', () => eventEmitter.emit('logout'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import { eventEmitter } from '../eventEmitter';

const debug = Debug('WA-JS:conn');

webpack.onInjected(() => registerIdleEvent());
webpack.onInjected(registerQRCodeIdleEvent);

function registerIdleEvent() {
State.on('change:state', async () => {
function registerQRCodeIdleEvent() {
const trigger = async () => {
const idle = isIdle();
if (idle) {
eventEmitter.emit('idle');
eventEmitter.emit('qrcode_idle');
}
});
};
trigger();
State.on('change:state', trigger);

debug('idle event registered');
}
4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export * as config from './config';
export * as blocklist from './blocklist';
export * as chat from './chat';
export * as conn from './conn';
/**
* @deprecated
*/
export * as auth from './conn';
export * as contact from './contact';
export * as group from './group';
export * as labels from './labels';
Expand Down

0 comments on commit b3f331b

Please sign in to comment.