source: imaps-frontend/node_modules/utila/lib/Emitter.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.4 KB
Line 
1// Generated by CoffeeScript 1.6.3
2var Emitter, array;
3
4array = require('./array');
5
6module.exports = Emitter = (function() {
7 function Emitter() {
8 this._listeners = {};
9 this._listenersForAnyEvent = [];
10 this._disabledEmitters = {};
11 }
12
13 Emitter.prototype.on = function(eventName, listener) {
14 if (this._listeners[eventName] == null) {
15 this._listeners[eventName] = [];
16 }
17 this._listeners[eventName].push(listener);
18 return this;
19 };
20
21 Emitter.prototype.once = function(eventName, listener) {
22 var cb, ran,
23 _this = this;
24 ran = false;
25 cb = function() {
26 if (ran) {
27 return;
28 }
29 ran = true;
30 listener();
31 return setTimeout(function() {
32 return _this.removeEvent(eventName, cb);
33 }, 0);
34 };
35 this.on(eventName, cb);
36 return this;
37 };
38
39 Emitter.prototype.onAnyEvent = function(listener) {
40 this._listenersForAnyEvent.push(listener);
41 return this;
42 };
43
44 Emitter.prototype.removeEvent = function(eventName, listener) {
45 if (this._listeners[eventName] == null) {
46 return this;
47 }
48 array.pluckOneItem(this._listeners[eventName], listener);
49 return this;
50 };
51
52 Emitter.prototype.removeListeners = function(eventName) {
53 if (this._listeners[eventName] == null) {
54 return this;
55 }
56 this._listeners[eventName].length = 0;
57 return this;
58 };
59
60 Emitter.prototype.removeAllListeners = function() {
61 var listeners, name, _ref;
62 _ref = this._listeners;
63 for (name in _ref) {
64 listeners = _ref[name];
65 listeners.length = 0;
66 }
67 return this;
68 };
69
70 Emitter.prototype._emit = function(eventName, data) {
71 var listener, _i, _j, _len, _len1, _ref, _ref1;
72 _ref = this._listenersForAnyEvent;
73 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
74 listener = _ref[_i];
75 listener.call(this, data, eventName);
76 }
77 if (this._listeners[eventName] == null) {
78 return;
79 }
80 _ref1 = this._listeners[eventName];
81 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
82 listener = _ref1[_j];
83 listener.call(this, data);
84 }
85 };
86
87 Emitter.prototype._throttleEmitterMethod = function(fnName, time) {
88 var lastCallArgs, originalFn, pend, pending, runIt, timer,
89 _this = this;
90 if (time == null) {
91 time = 1000;
92 }
93 originalFn = this[fnName];
94 if (typeof originalFn !== 'function') {
95 throw Error("this class does not have a method called '" + fnName + "'");
96 }
97 lastCallArgs = null;
98 pending = false;
99 timer = null;
100 this[fnName] = function() {
101 lastCallArgs = arguments;
102 return pend();
103 };
104 pend = function() {
105 if (pending) {
106 clearTimeout(timer);
107 }
108 timer = setTimeout(runIt, time);
109 return pending = true;
110 };
111 return runIt = function() {
112 pending = false;
113 return originalFn.apply(_this, lastCallArgs);
114 };
115 };
116
117 Emitter.prototype._disableEmitter = function(fnName) {
118 if (this._disabledEmitters[fnName] != null) {
119 throw Error("" + fnName + " is already a disabled emitter");
120 }
121 this._disabledEmitters[fnName] = this[fnName];
122 return this[fnName] = function() {};
123 };
124
125 Emitter.prototype._enableEmitter = function(fnName) {
126 var fn;
127 fn = this._disabledEmitters[fnName];
128 if (fn == null) {
129 throw Error("" + fnName + " is not a disabled emitter");
130 }
131 this[fnName] = fn;
132 return delete this._disabledEmitters[fnName];
133 };
134
135 return Emitter;
136
137})();
Note: See TracBrowser for help on using the repository browser.