All files event.js

0% Statements 0/23
0% Branches 0/6
0% Functions 0/7
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                                 
"use strict";
/*
 * @Author: lijianzhang
 * @Date: 2018-08-29 22:38:06
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2018-08-29 23:56:02
 */
exports.__esModule = true;
var Event = /** @class */ (function () {
    function Event() {
        this.listenerMap = new Map();
    }
    Event.prototype.on = function (type, fn) {
        var _this = this;
        var set = this.listenerMap.get(type);
        if (set) {
            set.add(fn);
        }
        else {
            this.listenerMap.set(type, new Set([fn]));
        }
        return function () { return _this.unListener(type, fn); };
    };
    Event.prototype.unListener = function (type, fn) {
        var set = this.listenerMap.get(type);
        if (set)
            set["delete"](fn);
    };
    Event.prototype.emit = function (type, data) {
        var fns = this.listenerMap.get(type);
        if (fns) {
            fns.forEach(function (listener) {
                listener(data);
            });
        }
    };
    return Event;
}());
exports.Event = Event;
exports["default"] = new Event();