注册回调单例类

注册回调类

typescript 复制代码
import { reactive } from "vue";

// 定义 Map 中的函数类型
type UpdateFunction = (value: string) => void;

class RegistryManager {
    // 私有静态实例,确保全局唯一
    private static instance: RegistryManager;

    private registry = reactive(new Map<string, Map<string, UpdateFunction>>());

    // 私有构造函数,禁止外部实例化
    private constructor() {}

    /**
     * 获取单例实例
     */
    public static getInstance(): RegistryManager {
        if (!RegistryManager.instance) {
            RegistryManager.instance = new RegistryManager();
        }
        return RegistryManager.instance;
    }

    /**
     * 注册回调函数
     * @param category 分类键
     * @param key 回调键
     * @param callback 回调函数
     */
    register(category: string, key: string, callback: UpdateFunction): void {
        if (!this.registry.has(category)) {
            this.registry.set(category, reactive(new Map<string, UpdateFunction>()));
        }
        this.registry.get(category)?.set(key, callback);
    }

    /**
     * 删除整个分类
     * @param category 分类键
     */
    removeCategory(category: string): boolean {
        return this.registry.delete(category);
    }

    /**
     * 删除分类中的特定回调
     * @param category 分类键
     * @param key 回调键
     */
    removeCallback(category: string, key: string): boolean {
        return this.registry.get(category)?.delete(key) || false;
    }

    /**
     * 获取分类中的所有回调键
     * @param category 分类键
     */
    getCallbackKeys(category: string): string[] {
        return Array.from(this.registry.get(category)?.keys() || []);
    }

    /**
     * 获取特定回调函数
     * @param category 分类键
     * @param key 回调键
     */
    getCallback(category: string, key: string): UpdateFunction | undefined {
        return this.registry.get(category)?.get(key);
    }

    /**
     * 检查分类是否存在
     * @param category 分类键
     */
    hasCategory(category: string): boolean {
        return this.registry.has(category);
    }

    /**
     * 检查回调是否存在
     * @param category 分类键
     * @param key 回调键
     */
    hasCallback(category: string, key: string): boolean {
        return this.registry.get(category)?.has(key) || false;
    }

    /**
     * 执行特定回调
     * @param category 分类键
     * @param key 回调键
     * @param value 传递给回调的值
     */
    executeCallback(category: string, key: string, value: string): boolean {
        const callback = this.getCallback(category, key);
        if (callback) {
            callback(value);
            return true;
        }
        return false;
    }

    /**
     * 批量执行分类中的所有回调
     * @param category 分类键
     * @param value 传递给回调的值
     */
    executeAllCallbacks(category: string, value: string): void {
        const callbacks = this.registry.get(category);
        if (callbacks) {
            callbacks.forEach(callback => callback(value));
        }
    }

    /**
     * 清空整个注册表
     */
    clear(): void {
        this.registry.clear();
    }
}

// 获取全局唯一实例
export const registryManager = RegistryManager.getInstance();

功能说明

  1. 注册功能:

    • register(category, key, callback): 注册一个新的回调函数到指定分类
  2. 删除功能:

    • removeCategory(category): 删除整个分类
    • removeCallback(category, key): 删除分类中的特定回调
    • clear(): 清空整个注册表
  3. 查询功能:

    • getCallbackKeys(category): 获取分类中所有回调键
    • getCallback(category, key): 获取特定回调函数
    • hasCategory(category): 检查分类是否存在
    • hasCallback(category, key): 检查回调是否存在
  4. 执行功能:

    • executeCallback(category, key, value): 执行特定回调
    • executeAllCallbacks(category, value): 批量执行分类中的所有回调
  5. 反应式特性:

    • 使用 Vue 的 reactive 包装 Map,确保响应式更新

使用示例

typescript 复制代码
// 注册回调
registryManager.register('user', 'updateName', (value) => {
    console.log(`Name updated to: ${value}`);
});

registryManager.register('user', 'updateEmail', (value) => {
    console.log(`Email updated to: ${value}`);
});

// 执行单个回调
registryManager.executeCallback('user', 'updateName', 'Dirk');

// 执行所有用户回调
registryManager.executeAllCallbacks('user', '123@qq.com');

// 删除特定回调
registryManager.removeCallback('user', 'updateEmail');

// 删除整个分类
registryManager.removeCategory('user');
相关推荐
idcu12 小时前
深入 Lyt.js 组件系统:L2 渲染引擎层的核心
前端·typescript
阿正的梦工坊15 小时前
【Typescript】08-keyof-typeof-索引访问类型
linux·ubuntu·typescript
小四的小六19 小时前
我用什么技术做了TLDR Scholar?
typescript·ai编程
阿正的梦工坊21 小时前
【Typescript】06-类型缩小与控制流分析
前端·javascript·typescript
阿正的梦工坊21 小时前
【Typescript】03-函数对象与接口
前端·javascript·typescript
濮水大叔2 天前
告别 Django Admin!这个 NodeJS 全栈框架让你在 DTO 中直接配置 Table/Form 渲染
前端·typescript·node.js
前端若水2 天前
技术选型:React 19 + TypeScript + TailwindCSS
前端·react.js·typescript
__log2 天前
NestJS vs Spring Boot:从架构哲学到实战选择的技术全景解析
spring boot·后端·架构·typescript
小杍随笔2 天前
【Rust + Tauri 2 + TypeScript + Tailwind CSS 4 桌面应用 UI 组件选型深度对比(2026版)】
css·rust·typescript