LazyArg.ts (877B)
1 /** 2 * @license 3 * Copyright 2022 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 import type {JSHandle} from '../api/JSHandle.js'; 8 import type PuppeteerUtil from '../injected/injected.js'; 9 10 /** 11 * @internal 12 */ 13 export interface PuppeteerUtilWrapper { 14 puppeteerUtil: Promise<JSHandle<PuppeteerUtil>>; 15 } 16 17 /** 18 * @internal 19 */ 20 export class LazyArg<T, Context = PuppeteerUtilWrapper> { 21 static create = <T>( 22 get: (context: PuppeteerUtilWrapper) => Promise<T> | T, 23 ): T => { 24 // We don't want to introduce LazyArg to the type system, otherwise we would 25 // have to make it public. 26 return new LazyArg(get) as unknown as T; 27 }; 28 29 #get: (context: Context) => Promise<T> | T; 30 private constructor(get: (context: Context) => Promise<T> | T) { 31 this.#get = get; 32 } 33 34 async get(context: Context): Promise<T> { 35 return await this.#get(context); 36 } 37 }