Files
VideoPlayer/src/adapters/AdapterRegistry.ts
db123 ec89f3826e faet(types): adapter registry to modular adapters
added an adapter registry to have a modular way to instntiate adapters for scalability
2026-04-29 23:10:36 +03:30

38 lines
942 B
TypeScript

/**
* dynamically map a name to an adapter and call instantiate it
*
* author: db123 at db
* creation: 29/4/2026
*/
import { AdapterAPI } from '../types';
import { AdapterConfig } from '../types/AdapterAPI';
type AdapterConstructor = new (
video: HTMLVideoElement,
options?: AdapterConfig
) => AdapterAPI;
export class AdapterRegistry {
private adapters: Map<string, AdapterConstructor> = new Map();
registerAdapter(type: string, constructor: AdapterConstructor): void {
this.adapters.set(type, constructor);
}
createAdapter(
type: string,
video: HTMLVideoElement,
options?: AdapterConfig
): AdapterAPI {
if (!this.adapters.has(type)) {
throw new Error('Adapter not found in registry.' + type);
}
const construct = this.adapters.get(type);
if (!construct) {
throw new Error('Adapter does not have constructor.' + type);
}
return new construct(video, options);
}
}