summaryrefslogtreecommitdiff
path: root/html/content/lib/emitter/dist/emitter.js
blob: 6b2a6fa245468011e5f4046e96c3ab5c93be23bc (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.emitter = factory());
}(this, (function () { 'use strict';

    function emitter(context, options) {
        var _events = {}, o = options || {}, _constantEvents = {}, noop = function(){};

        function invoke(handler, args){
            window.setTimeout(function () {
                handler.apply(context, args);
            });
        }

        function emit(event) {
            var handlers = (_events[event] || (_events[event] = []));
            if (!handlers){
                return;
            }
            var args = Array.prototype.slice.call(arguments, 1);
            if (o.emitting) {
                o.emitting({ event: event, data: args, context: context });
            }
            for (var i = 0, l = handlers.length; i < l; i++) {
                invoke(handlers[i], args);
            }
        }

        function replay (event) {
            var args = Array.prototype.slice.call(arguments, 1);
            _constantEvents[event] = args;
            emit.apply(null, arguments);
        }

        function on(event, fn) {
            if (_constantEvents[event]){
                invoke(fn, _constantEvents[event]);
                return noop;
            }
            var handlers = (_events[event] || (_events[event] = []));
            handlers.push(fn);
            return function () {
                 off(event, fn);
            };
        }

        function off(event, fn) {
            var handlers = _events[event], i, h;
            if (!handlers) {
                return;
            }
            if (Array.isArray(event)) {
                for (i = 0, l = event.length; i < l; i++) {
                    off(event[i], fn);
                }
                return;
            }
            i = handlers.length;
            while(i--) {
                h = handlers[i];
                if (fn === h){
                    handlers.splice(i, 1);
                    break;
                }
            }
        }

        function once(event, fn) {
            var unsub = on(event, function () {
                fn.apply(context, arguments);
                unsub();
            });
            return unsub;
        }

        return {
            on: on,
            off: off,
            emit: emit,
            once: once,
            replay: replay
        };
    }

    return emitter;

})));