Skip to content

Commit

Permalink
feat(handle): NodeHandle -> HostHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperStrike committed Oct 22, 2023
1 parent 70e12fd commit c7e6c07
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ process.exit(await runner.runTests());

Check [Options](#options) for full option list.

## Handle
## HostHandle

APIs similar to [`JSHandle` in Playwright](https://playwright.dev/docs/api/class-jshandle).

Expand Down
10 changes: 5 additions & 5 deletions src/WS/WSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import RouteHandler, { RouteHandlerCallback, RouteMatcher, RouteOptions } from './route/RouteHandler.js';
import RouteRequest from './route/RouteRequest.js';
import Route from './route/Route.js';
import NodeHandle from './handle/NodeHandle.js';
import HostHandle from './handle/HostHandle.js';

export default class WSClient {
private readonly uuid: string;
Expand All @@ -25,8 +25,8 @@ export default class WSClient {
resolve();
}, { once: true });
});
this.pageHandle = new NodeHandle(0, this.ws);
this.contextHandle = new NodeHandle(1, this.ws);
this.pageHandle = new HostHandle(0, this.ws);
this.contextHandle = new HostHandle(1, this.ws);
}

private routes: RouteHandler[] = [];
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class WSClient {
await route.innerContinue();
}

readonly pageHandle: NodeHandle<playwright.Page>;
readonly pageHandle: HostHandle<playwright.Page>;

readonly contextHandle: NodeHandle<playwright.BrowserContext>;
readonly contextHandle: HostHandle<playwright.BrowserContext>;
}
24 changes: 12 additions & 12 deletions src/WS/handle/NodeHandle.ts → src/WS/handle/HostHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type Unboxed<Arg> =
? RegExp
: Arg extends Error
? Error
: Arg extends NodeHandle<infer T>
: Arg extends HostHandle<infer T>
? T
: Arg extends [infer A0, ...infer Rest]
? [Unboxed<A0>, ...Unboxed<Rest>]
Expand Down Expand Up @@ -47,7 +47,7 @@ const finalizationRegistry = new FinalizationRegistry(({ id, ws }: {
}));
});

export default class NodeHandle<T = unknown> extends Serializer.Handle {
export default class HostHandle<T = unknown> extends Serializer.Handle {
/**
* Share the handle ID with an object so that the matching handles will keep referencing the node
* target until all ID users are disposed or garbage-collected.
Expand Down Expand Up @@ -116,19 +116,19 @@ export default class NodeHandle<T = unknown> extends Serializer.Handle {
nodeFunction: NodeFunctionOn<O, Arg, R>,
arg: Arg,
createHandle: true,
): Promise<NodeHandle<R>>;
): Promise<HostHandle<R>>;
private async innerEvaluate<R, Arg, O>(
nodeFunction: NodeFunctionOn<O, Arg, R>,
arg: Arg,
createHandle: boolean,
): Promise<R | NodeHandle<R>> {
): Promise<R | HostHandle<R>> {
return this.act({
action: 'evaluate',
fn: String(nodeFunction),
arg: JSON.stringify(Serializer.serializeValue(arg)),
h: createHandle,
}, (result) => {
if (createHandle) return new NodeHandle(result as number, this.ws);
if (createHandle) return new HostHandle(result as number, this.ws);
return result as R;
});
}
Expand All @@ -142,11 +142,11 @@ export default class NodeHandle<T = unknown> extends Serializer.Handle {
evaluateHandle<R, Arg, O extends T = T>(
nodeFunction: NodeFunctionOn<O, Arg, R>,
arg: Arg,
): Promise<NodeHandle<R>>;
): Promise<HostHandle<R>>;
evaluateHandle<R, O extends T = T>(
nodeFunction: NodeFunctionOn<O, void, R>,
arg?: unknown,
): Promise<NodeHandle<R>>;
): Promise<HostHandle<R>>;
async evaluateHandle<R, Arg, O>(nodeFunction: NodeFunctionOn<O, Arg, R>, arg: Arg) {
return this.innerEvaluate(nodeFunction, arg, true);
}
Expand Down Expand Up @@ -174,22 +174,22 @@ export default class NodeHandle<T = unknown> extends Serializer.Handle {
this.disposed = true;
}

async getProperties(): Promise<Map<string, NodeHandle>> {
async getProperties(): Promise<Map<string, HostHandle>> {
return this.act({
action: 'get-properties',
}, (result) => {
const propertiesMap: Map<string, NodeHandle> = new Map();
const propertiesMap: Map<string, HostHandle> = new Map();
(result as [string, number][]).forEach(([name, handleID]) => {
propertiesMap.set(name, new NodeHandle(handleID, this.ws));
propertiesMap.set(name, new HostHandle(handleID, this.ws));
});
return propertiesMap;
});
}

async getProperty(propertyName: string): Promise<NodeHandle> {
async getProperty(propertyName: string): Promise<HostHandle> {
return this.act({
action: 'get-property',
name: propertyName,
}, (result) => new NodeHandle(result as number, this.ws));
}, (result) => new HostHandle(result as number, this.ws));
}
}
10 changes: 5 additions & 5 deletions src/WS/route/RouteRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type playwright from 'playwright-core';
import NodeHandle from '../handle/NodeHandle.js';
import HostHandle from '../handle/HostHandle.js';
import { RouteRequestMeta } from '../message.js';
import { FallbackOverrides } from './Route.js';

Expand All @@ -10,8 +10,8 @@ export default class RouteRequest {
private readonly ws: WebSocket,
) {
const { frame, serviceWorker } = requestMeta;
if (frame !== null) NodeHandle.share(frame, ws, this);
if (serviceWorker !== null) NodeHandle.share(serviceWorker, ws, this);
if (frame !== null) HostHandle.share(frame, ws, this);
if (serviceWorker !== null) HostHandle.share(serviceWorker, ws, this);
}

private fallbackOverrides: FallbackOverrides = {};
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class RouteRequest {
if (this.requestMeta.frame === null) {
throw new Error('Service Worker requests do not have an associated frame');
}
return new NodeHandle<playwright.Frame>(this.requestMeta.frame, this.ws);
return new HostHandle<playwright.Frame>(this.requestMeta.frame, this.ws);
}

headerValue(name: string) {
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class RouteRequest {

serviceWorker() {
if (this.requestMeta.serviceWorker === null) return null;
return new NodeHandle<playwright.Worker>(this.requestMeta.serviceWorker, this.ws);
return new HostHandle<playwright.Worker>(this.requestMeta.serviceWorker, this.ws);
}

url() {
Expand Down
10 changes: 5 additions & 5 deletions test/handle/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from '../default.setup.js';
import NodeHandle from '../../src/WS/handle/NodeHandle.js';
import HostHandle from '../../src/WS/handle/HostHandle.js';
import { pageHandle } from '../../src/index.js';

describe('handle', () => {
Expand Down Expand Up @@ -94,7 +94,7 @@ describe('handle', () => {
return page.context();
});
expect(evaluated).toBe(true);
expect(contextHandle).toBeInstanceOf(NodeHandle);
expect(contextHandle).toBeInstanceOf(HostHandle);
const browserVersion = await contextHandle.evaluate((context) => (
context.browser()?.version()
));
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('handle', () => {
expect(props).toHaveLength(1);
const [key, value] = props[0];
expect(key).toBe('p');
expect(value).toBeInstanceOf(NodeHandle);
expect(value).toBeInstanceOf(HostHandle);
await expect(value.jsonValue()).resolves.toEqual([1, 2]);
});

Expand All @@ -132,8 +132,8 @@ describe('handle', () => {
objHandle.getProperty('p'),
objHandle.getProperty('not-exist'),
]);
expect(propHandle).toBeInstanceOf(NodeHandle);
expect(notExistPropHandle).toBeInstanceOf(NodeHandle);
expect(propHandle).toBeInstanceOf(HostHandle);
expect(notExistPropHandle).toBeInstanceOf(HostHandle);
await Promise.all([
expect(propHandle.jsonValue()).resolves.toEqual([1, 2]),
expect(notExistPropHandle.jsonValue()).resolves.toBe(undefined),
Expand Down

0 comments on commit c7e6c07

Please sign in to comment.