2022-09-19 10:49:13 +00:00
|
|
|
/**
|
2024-01-03 10:11:33 +00:00
|
|
|
* @license
|
|
|
|
* Copyright 2022 Google Inc.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2022-09-19 10:49:13 +00:00
|
|
|
*/
|
|
|
|
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {JSHandle} from '../api/JSHandle.js';
|
2023-09-15 11:00:20 +00:00
|
|
|
import type PuppeteerUtil from '../injected/injected.js';
|
2023-01-27 19:58:40 +00:00
|
|
|
|
2022-09-15 06:22:20 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-06-06 12:39:54 +00:00
|
|
|
export interface PuppeteerUtilWrapper {
|
|
|
|
puppeteerUtil: Promise<JSHandle<PuppeteerUtil>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class LazyArg<T, Context = PuppeteerUtilWrapper> {
|
2023-01-27 19:58:40 +00:00
|
|
|
static create = <T>(
|
2023-06-06 12:39:54 +00:00
|
|
|
get: (context: PuppeteerUtilWrapper) => Promise<T> | T
|
2023-01-27 19:58:40 +00:00
|
|
|
): T => {
|
|
|
|
// We don't want to introduce LazyArg to the type system, otherwise we would
|
|
|
|
// have to make it public.
|
|
|
|
return new LazyArg(get) as unknown as T;
|
|
|
|
};
|
|
|
|
|
2023-06-06 12:39:54 +00:00
|
|
|
#get: (context: Context) => Promise<T> | T;
|
|
|
|
private constructor(get: (context: Context) => Promise<T> | T) {
|
2022-09-15 06:22:20 +00:00
|
|
|
this.#get = get;
|
|
|
|
}
|
|
|
|
|
2023-06-06 12:39:54 +00:00
|
|
|
async get(context: Context): Promise<T> {
|
2023-09-01 07:49:33 +00:00
|
|
|
return await this.#get(context);
|
2022-09-15 06:22:20 +00:00
|
|
|
}
|
|
|
|
}
|