faet(types): adapter registry to modular adapters
added an adapter registry to have a modular way to instntiate adapters for scalability
This commit is contained in:
37
src/adapters/AdapterRegistry.ts
Normal file
37
src/adapters/AdapterRegistry.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user