auto-account-machine/browser-automation-ts/src/factory/BrowserFactory.ts
2025-11-21 17:59:49 +08:00

65 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 浏览器工厂类
* 使用泛型和类型安全
*/
import { IBrowserProvider } from '../core/interfaces/IBrowserProvider';
import { BrowserProviderType } from '../core/types';
type ProviderConstructor = new (config: any) => IBrowserProvider;
export class BrowserFactory {
private static providers = new Map<string, ProviderConstructor>();
/**
* 注册ProviderTypeScript类型检查
*/
static register<T extends IBrowserProvider>(
type: BrowserProviderType,
ProviderClass: new (config: any) => T
): void {
this.providers.set(type, ProviderClass);
console.log(`✅ Provider "${type}" registered`);
}
/**
* 创建Provider实例
*/
static create<T extends IBrowserProvider = IBrowserProvider>(
type: BrowserProviderType,
config: any = {}
): T {
const ProviderClass = this.providers.get(type);
if (!ProviderClass) {
const available = Array.from(this.providers.keys()).join(', ');
throw new Error(
`Unknown provider: "${type}"\nAvailable: ${available}`
);
}
return new ProviderClass(config) as T;
}
/**
* 获取所有已注册的Provider
*/
static getAvailableProviders(): BrowserProviderType[] {
return Array.from(this.providers.keys()) as BrowserProviderType[];
}
/**
* 检查Provider是否已注册
*/
static has(type: BrowserProviderType): boolean {
return this.providers.has(type);
}
/**
* 注销Provider
*/
static unregister(type: BrowserProviderType): void {
this.providers.delete(type);
}
}