zoukankan      html  css  js  c++  java
  • wather.js 封装

    /** @format */
    /* eslint-disable @typescript-eslint/no-explicit-any */
    /* eslint-disable @typescript-eslint/explicit-module-boundary-types */

    // 观察者
    class Watcher {
        public list: any;
        constructor() {
          this.list = {};
        }
        // 订阅
        public $on(key: string, fn: any): void {
          if (!this.list[key]) {
            this.list[key] = [];
          }
          this.list[key].push(fn);
        }
        // 发布
        public $emit(key: string, param?: any): void | boolean {
          const fns = this.list[key];
          // 检测是否有注册  注册后是否有注入方法
          if (!fns || fns.length === 0) {
            return false;
          }
          // 依次执行 并传入参数
          fns.forEach((fn: any) => {
            fn(param);
          });
        }
        // 清空
        public $clear(key: string) {
          this.list[key] = [];
        }
        public $clearAll() {
          this.list = {};
        }
      }
      
      export default new Watcher();  
  • 相关阅读:
    02 基本介绍
    01 概述 网络分层
    04 可扩展
    Java 注解2
    03 高可用
    重拾安卓_00_资源帖
    JavaUtil_09_通用工具类-01_Hutool
    java支付宝开发-01-沙箱环境接入
    java支付宝开发-00-资源帖
    svn_学习_01_TortoiseSVN使用教程
  • 原文地址:https://www.cnblogs.com/wenqylh/p/14964946.html
Copyright © 2011-2022 走看看