Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make initChannelListener a method of ScriptEvaluator #567

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 49 additions & 55 deletions src/bidiMapper/domains/script/scriptEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,6 @@ import {IEventManager} from '../events/EventManager.js';

import {Realm} from './realm.js';

async function initChannelListener(
channel: Script.Channel,
channelHandle: string | undefined,
eventManager: IEventManager,
realm: Realm
) {
const channelId = channel.value.channel;

// TODO(#294): Remove this loop after the realm is destroyed.
// Rely on the CDP throwing exception in such a case.
for (;;) {
const message = await realm.cdpClient.sendCommand(
'Runtime.callFunctionOn',
{
functionDeclaration: String(
async (channelHandle: {getMessage: () => Promise<unknown>}) =>
channelHandle.getMessage()
),
arguments: [
{
objectId: channelHandle,
},
],
awaitPromise: true,
executionContextId: realm.executionContextId,
generateWebDriverValue: true,
}
);

eventManager.registerPromiseEvent(
realm
.cdpToBidiValue(message, channel.value.ownership ?? 'none')
.then((data) => ({
method: Script.EventNames.MessageEvent,
params: {
channel: channelId,
data,
source: {
realm: realm.realmId,
context: realm.browsingContextId,
},
},
})),
realm.browsingContextId,
Script.EventNames.MessageEvent
);
}
}

// As `script.evaluate` wraps call into serialization script, `lineNumber`
// should be adjusted.
const CALL_FUNCTION_STACKTRACE_LINE_OFFSET = 1;
Expand Down Expand Up @@ -495,12 +446,7 @@ export class ScriptEvaluator {
const channelHandle = createChannelHandleResult.result.objectId;

// Long-poll the message queue asynchronously.
initChannelListener(
argumentValue,
channelHandle,
this.#eventManager,
realm
);
this.#initChannelListener(argumentValue, channelHandle, realm);

const sendMessageArgResult = await realm.cdpClient.sendCommand(
'Runtime.callFunctionOn',
Expand Down Expand Up @@ -571,6 +517,54 @@ export class ScriptEvaluator {
return result;
}

async #initChannelListener(
channel: Script.Channel,
channelHandle: string | undefined,
realm: Realm
) {
const channelId = channel.value.channel;

// TODO(#294): Remove this loop after the realm is destroyed.
// Rely on the CDP throwing exception in such a case.
for (;;) {
const message = await realm.cdpClient.sendCommand(
'Runtime.callFunctionOn',
{
functionDeclaration: String(
async (channelHandle: {getMessage: () => Promise<unknown>}) =>
channelHandle.getMessage()
),
arguments: [
{
objectId: channelHandle,
},
],
awaitPromise: true,
executionContextId: realm.executionContextId,
generateWebDriverValue: true,
}
);

this.#eventManager.registerPromiseEvent(
realm
.cdpToBidiValue(message, channel.value.ownership ?? 'none')
.then((data) => ({
method: Script.EventNames.MessageEvent,
params: {
channel: channelId,
data,
source: {
realm: realm.realmId,
context: realm.browsingContextId,
},
},
})),
realm.browsingContextId,
Script.EventNames.MessageEvent
);
}
}

async #serializeCdpExceptionDetails(
cdpExceptionDetails: Protocol.Runtime.ExceptionDetails,
lineOffset: number,
Expand Down