Skip to content

Commit

Permalink
fix: Fixed onMessage method (fix #1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 18, 2022
1 parent 9b66878 commit e8549a1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/api/layers/listener.layer.ts
Expand Up @@ -52,6 +52,8 @@ export class ListenerLayer extends ProfileLayer {
constructor(public page: Page, session?: string, options?: CreateConfig) {
super(page, session, options);

this.listenerEmitter.setMaxListeners(0);

this.listenerEmitter.on(ExposedFn.onInterfaceChange, (state) => {
this.log('http', `Current state: ${state.mode} (${state.displayInfo})`);
});
Expand Down Expand Up @@ -92,9 +94,17 @@ export class ListenerLayer extends ProfileLayer {
if (!has) {
this.log('debug', `Exposing ${func} function`);
await this.page
.exposeFunction(func, (...args) =>
this.listenerEmitter.emit(func, ...args)
)
.exposeFunction(func, (...args) => {
Promise.resolve().then(() => {
this.log(
'debug',
`Emitting ${func} event (${this.listenerEmitter.listenerCount(
func
)} registered)`
);
this.listenerEmitter.emit(func, ...args);
});
})
.catch(() => {});
}
}
Expand Down Expand Up @@ -263,6 +273,7 @@ export class ListenerLayer extends ProfileLayer {
event: string | symbol,
listener: (...args: any[]) => void
) {
this.log('debug', `Registering ${event.toString()} event`);
this.listenerEmitter.on(event, listener);
return {
dispose: () => {
Expand Down

0 comments on commit e8549a1

Please sign in to comment.