UNPKG

2.13 kBPlain TextView Raw
1/**
2 * @license
3 * Copyright 2023 Google Inc.
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7import type * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
8
9import type {ElementHandle} from '../api/ElementHandle.js';
10import {JSHandle} from '../api/JSHandle.js';
11import {UnsupportedOperation} from '../common/Errors.js';
12
13import {BidiDeserializer} from './Deserializer.js';
14import type {BidiRealm} from './Realm.js';
15
16/**
17 * @internal
18 */
19export class BidiJSHandle<T = unknown> extends JSHandle<T> {
20 static from<T>(
21 value: Bidi.Script.RemoteValue,
22 realm: BidiRealm,
23 ): BidiJSHandle<T> {
24 return new BidiJSHandle(value, realm);
25 }
26
27 readonly #remoteValue: Bidi.Script.RemoteValue;
28
29 override readonly realm: BidiRealm;
30
31 #disposed = false;
32
33 constructor(value: Bidi.Script.RemoteValue, realm: BidiRealm) {
34 super();
35 this.#remoteValue = value;
36 this.realm = realm;
37 }
38
39 override get disposed(): boolean {
40 return this.#disposed;
41 }
42
43 override async jsonValue(): Promise<T> {
44 return await this.evaluate(value => {
45 return value;
46 });
47 }
48
49 override asElement(): ElementHandle<Node> | null {
50 return null;
51 }
52
53 override async dispose(): Promise<void> {
54 if (this.#disposed) {
55 return;
56 }
57 this.#disposed = true;
58 await this.realm.destroyHandles([this]);
59 }
60
61 get isPrimitiveValue(): boolean {
62 switch (this.#remoteValue.type) {
63 case 'string':
64 case 'number':
65 case 'bigint':
66 case 'boolean':
67 case 'undefined':
68 case 'null':
69 return true;
70
71 default:
72 return false;
73 }
74 }
75
76 override toString(): string {
77 if (this.isPrimitiveValue) {
78 return 'JSHandle:' + BidiDeserializer.deserialize(this.#remoteValue);
79 }
80
81 return 'JSHandle@' + this.#remoteValue.type;
82 }
83
84 override get id(): string | undefined {
85 return 'handle' in this.#remoteValue ? this.#remoteValue.handle : undefined;
86 }
87
88 remoteValue(): Bidi.Script.RemoteValue {
89 return this.#remoteValue;
90 }
91
92 override remoteObject(): never {
93 throw new UnsupportedOperation('Not available in WebDriver BiDi');
94 }
95}