zoukankan      html  css  js  c++  java
  • Event Aggregator

    /**
     * Created with JetBrains WebStorm.
     * User: 宇乔
     * Date: 13-8-2
     * Time: 下午3:01
     * To change this template use File | Settings | File Templates.
     */
    
    function Event(name) {
    
        var handlers = [];
    
        this.getName = function () {
            return name;
        }
    
        this.addHandler = function (handler) {
            handlers.push(handler);
        }
        this.removeHandler = function (handler) {
            handlers.forEach(function (item, i) {
                if (item == handler) {
                    handler.splice(i, 1);
                }
            })
        }
    
        this.fire = function (eventArgs) {
            handlers.forEach(function (h) {
                h(eventArgs);
            })
        }
    }
    
    
    function EventAggregator() {
        var events = [];
    
        function getEvent(name) {
            var fn;
            events.forEach(function (item) {
                if (item.getName() == name) {
                    fn = item;
                    return;
                }
            });
            return fn;
        }
    
        this.subscribe = function (eventName, handler) {
    
            var event = getEvent(eventName);
    
            if (!event) {
                event = new Event(eventName);
                events.push(event);
            }
            event.addHandler(handler);
        }
    
        this.publish = function (eventName, eventArgs) {
    
            var event = getEvent(eventName);
            if (!event) {
                event = new Event(eventName);
                events.push(event);
            }
            event.fire(eventArgs);
        }
    }
    

      

  • 相关阅读:
    LeetCode 55
    LeetCode 337
    LeetCode 287
    LeetCode 274
    LeetCode 278
    LeetCode 264
    LeetCode 189
    LeetCode 206
    LeetCode 142
    LeetCode 88
  • 原文地址:https://www.cnblogs.com/Mr-Joe/p/3232729.html
Copyright © 2011-2022 走看看