From ec89f3826e689a0e434dd52cc433bddd78129dce Mon Sep 17 00:00:00 2001 From: db123 Date: Wed, 29 Apr 2026 23:10:36 +0330 Subject: [PATCH] faet(types): adapter registry to modular adapters added an adapter registry to have a modular way to instntiate adapters for scalability --- src/adapters/AdapterRegistry.ts | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/adapters/AdapterRegistry.ts diff --git a/src/adapters/AdapterRegistry.ts b/src/adapters/AdapterRegistry.ts new file mode 100644 index 0000000..4b7ba8d --- /dev/null +++ b/src/adapters/AdapterRegistry.ts @@ -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 = 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); + } +}