44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
/**
|
|
* Custom renderer adapter for Imhotep Playwright.
|
|
*
|
|
* Allows users to provide their own mount function for arbitrary
|
|
* render targets. This is the escape hatch for frameworks not
|
|
* explicitly supported by built-in adapters.
|
|
*/
|
|
|
|
import { Page } from 'playwright'
|
|
import type { SceneTarget } from 'imhotep-core/scene-target'
|
|
import { RendererAdapter } from './renderers.js'
|
|
|
|
export interface CustomAdapterOptions {
|
|
/** Unique identifier for this adapter instance. */
|
|
id?: string
|
|
/** User-provided mount function. */
|
|
mount: (page: Page, target: SceneTarget, input: unknown) => Promise<void>
|
|
/** Optional user-provided unmount function. */
|
|
unmount?: (page: Page) => Promise<void>
|
|
}
|
|
|
|
export function createCustomAdapter(options: CustomAdapterOptions): RendererAdapter {
|
|
return {
|
|
id: options.id ?? 'custom',
|
|
|
|
async mount(page: Page, target: SceneTarget, input: unknown): Promise<void> {
|
|
if (target.kind !== 'custom-renderer' && target.kind !== 'react-component' && target.kind !== 'vue-component') {
|
|
throw new Error(
|
|
`Custom adapter received unsupported target kind: ${target.kind}. ` +
|
|
`Expected 'custom-renderer', 'react-component', or 'vue-component'. ` +
|
|
`If you registered this adapter for a specific renderer, ensure the target matches.`
|
|
)
|
|
}
|
|
await options.mount(page, target, input)
|
|
},
|
|
|
|
async unmount(page: Page): Promise<void> {
|
|
if (options.unmount) {
|
|
await options.unmount(page)
|
|
}
|
|
},
|
|
}
|
|
}
|