-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmitter.cs
187 lines (171 loc) · 6.59 KB
/
Emitter.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Text;
namespace TestConsole
{
public class Emitter
{
//public Dictionary<string, List<Func<object, object>>> _callbacks;
public Dictionary<string, List<Action> > _callbacks;
//Dictionary<string, List<Func<object, object>>> coalesce(Dictionary<string, List<Func<object, object>>> FirstObject, Dictionary<string, List<Func<object, object>>> SecondObject)
//{
// return Util.Coalesce<Dictionary<string, List<Func<object, object>>>>(FirstObject, SecondObject);
//}
Dictionary<string, List<Action>> coalesce(Dictionary<string, List<Action>> FirstObject, Dictionary<string, List<Action>> SecondObject)
{
return Util.Coalesce< Dictionary< string, List<Action> > >(FirstObject, SecondObject);
}
List<Func<object, object>> coalesce(List<Func<object, object>> FirstObject, List<Func<object, object>> SecondObject)
{
return Util.Coalesce<List<Func<object, object>>>(FirstObject, SecondObject);
}
List<Action> coalesce(List<Action> FirstObject, List<Action> SecondObject)
{
return Util.Coalesce<List<Action>>(FirstObject, SecondObject);
}
//public virtual Emitter on(string evnt, Func<object, object> fn)
//{
// addEventListener(evnt, fn);
// return this;
//}
public virtual Emitter on(string evnt, Action fn)
{
addEventListener(evnt, fn);
return this;
}
//public virtual Emitter addEventListener(string evnt, Func<object, object> fn)
//{
// this._callbacks = coalesce(this._callbacks, new Dictionary<string, List<Func<object, object>>>());
// (this._callbacks['$' + evnt] = Util.Coalesce(this._callbacks['$' + evnt], new List<Func<object, object>>())).Add(fn);
// return this;
//}
public virtual Emitter addEventListener(string evnt, Action fn)
{
this._callbacks = coalesce(this._callbacks, new Dictionary<string, List<Action>>());
(this._callbacks['$' + evnt] = coalesce(this._callbacks["$" + evnt], new List<Action>())).Add(fn);
return this;
}
//Not implemented yet
//public virtual Emitter once(string evnt, Func<object, object> fn)
//{
// Action on = () =>
// {
// this.off(evnt, on);
// //fn.apply(this, arguments);
// };
// on.fn = fn;
// this.on(event, on);
// return this;
//}
/*
Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener = function(event, fn)
*/
internal Emitter off(string evnt)
{
_callbacks.Remove(evnt);
return this;
}
public virtual Emitter off(string evnt = null, Action fn = null)
{
return removeListener( evnt, fn);
//return this;
}
public virtual Emitter removeListener(string evnt = null, Action fn = null)
{
return removeAllListeners(evnt, fn);
//return this;
}
public virtual Emitter removeAllListeners(string evnt = null, Action fn = null)
{
return removeEventListener( evnt , fn );
//return this;
}
//public virtual Emitter removeEventListener(string evnt = null, Func<object, object> fn = null)
public virtual Emitter removeEventListener(string evnt = null, Action fn = null)
{
this._callbacks = coalesce (this._callbacks , new Dictionary<string, List<Action>>());
// all
//if (0 == arguments.length) {
if ( evnt == null || fn == null)
{
this._callbacks = new Dictionary<string, List<Action>>();
return this;
}
// specific event
var callbacks = this._callbacks["$" + evnt];
if ( ! ( callbacks != null ) )
return this;
// remove all handlers
//if (1 == arguments.length) {
if (fn == null)
{
this._callbacks.Remove("$" + evnt);
return this;
}
// remove specific handler
//var cb;
for (var i = 0; i < callbacks.Count; i++) {
var cb = callbacks[i];
if (cb == fn )
{
callbacks.RemoveAt(i);
break;
}
}
// Remove event specific arrays for event types that no
// one is subscribed for to avoid memory leak.
if (callbacks.Count == 0) {
//delete this._callbacks['$' + evnt];
this._callbacks.Remove("$" + evnt);
}
return this;
}
public virtual Emitter emit(string evnt, params object[] arguments)
{
this._callbacks = coalesce ( this._callbacks, new Dictionary<string, List<Action>>());
//https://stackoverflow.com/questions/2125714/explanation-of-slice-call-in-javascript
//var args = (new object[0]).slice().call(arguments, 1);
var args = arguments;
var callbacks = this._callbacks['$' + evnt];
if ( callbacks != null ) {
callbacks = callbacks.slice(0);
for (int i = 0, len = callbacks.Count; i<len; ++i) {
callbacks[i].apply(this, args);
}
}
return this;
//return emit( evnt, null);
}
//public virtual Emitter emit(string evnt,object o)
//{
// return this;
//}
//public virtual List<Func<object, object>> listeners(string evnt)
//{
// return coalesce(this._callbacks['$' + evnt], new List<Func<object, object>>());
//}
public virtual List<Action> listeners(string evnt)
{
return coalesce(this._callbacks['$' + evnt], new List<Action>());
}
public virtual int hasListeners(string evnt)
{
return this.listeners(evnt).Count;
}
public void off()
{
throw new NotImplementedException();
}
public virtual object mixin(object obj)
{
//for (var key in Emitter.prototype)
//{
// obj[key] = Emitter.prototype[key];
//}
return obj;
}
}
}