Location>code7788 >text

Register a callback singleton class

Popularity:789 ℃/2025-03-28 09:03:09

Register a callback class

import { reactive } from "vue";

 // Define the function type in the Map
 type UpdateFunction = (value: string) => void;

 class RegistryManager {
     // Private static instances ensure global uniqueness
     private static instance: RegistryManager;

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

     // Private constructor, prohibit external instantiation
     private constructor() {}

     /**
      * Get a singleton instance
      */
     public static getInstance(): RegistryManager {
         if (!) {
              = new RegistryManager();
         }
         return ;
     }

     /**
      * Register the callback function
      * @param category category key
      * @param key callback key
      * @param callback callback function
      */
     register(category: string, key: string, callback: UpdateFunction): void {
         if (!(category)) {
             (category, reactive(new Map<string, UpdateFunction>()));
         }
         (category)?.set(key, callback);
     }

     /**
      * Delete the entire category
      * @param category category key
      */
     removeCategory(category: string): boolean {
         return (category);
     }

     /**
      * Delete specific callbacks in the category
      * @param category category key
      * @param key callback key
      */
     removeCallback(category: string, key: string): boolean {
         return (category)?.delete(key) || false;
     }

     /**
      * Get all callback keys in the category
      * @param category category key
      */
     getCallbackKeys(category: string): string[] {
         return ((category)?.keys() || []);
     }

     /**
      * Get a specific callback function
      * @param category category key
      * @param key callback key
      */
     getCallback(category: string, key: string): UpdateFunction | undefined {
         return (category)?.get(key);
     }

     /**
      * Check whether the classification exists
      * @param category category key
      */
     hasCategory(category: string): boolean {
         return (category);
     }

     /**
      * Check whether the callback exists
      * @param category category key
      * @param key callback key
      */
     hasCallback(category: string, key: string): boolean {
         return (category)?.has(key) || false;
     }

     /**
      * Execute specific callbacks
      * @param category category key
      * @param key callback key
      * @param value The value passed to the callback
      */
     executeCallback(category: string, key: string, value: string): boolean {
         const callback = (category, key);
         if (callback) {
             callback(value);
             return true;
         }
         return false;
     }

     /**
      * Batch execution of all callbacks in the classification
      * @param category category key
      * @param value The value passed to the callback
      */
     executeAllCallbacks(category: string, value: string): void {
         const callbacks = (category);
         if (callbacks) {
             (callback => callback(value));
         }
     }

     /**
      * Clear the entire registry
      */
     clear(): void {
         ();
     }
 }

 // Get global unique instance
 export const registryManager = ();

Function description

  1. Registration function:

    • register(category, key, callback): Register a new callback function to the specified category
  2. Delete function:

    • removeCategory(category): Delete the entire category
    • removeCallback(category, key): Delete specific callbacks in the category
    • clear(): Clear the entire registry
  3. Query function:

    • getCallbackKeys(category): Get all callback keys in the category
    • getCallback(category, key): Get a specific callback function
    • hasCategory(category): Check whether the classification exists
    • hasCallback(category, key): Check whether the callback exists
  4. Execute functions:

    • executeCallback(category, key, value): Execute a specific callback
    • executeAllCallbacks(category, value): Batch execution of all callbacks in the classification
  5. Reactive Characteristics:

    • Using VuereactivePackage Map to ensure responsive updates

Example of usage

// Register callback
 ('user', 'updateName', (value) => {
     (`Name updated to: ${value}`);
 });

 ('user', 'updateEmail', (value) => {
     (`Email updated to: ${value}`);
 });

 // Execute a single callback
 ('user', 'updateName', 'John Doe');

 // Execute all user callbacks
 ('user', 'new@');

 // Delete specific callbacks
 ('user', 'updateEmail');

 // Delete the entire category
 ('user');