1 | function DispatchEvents (thread)
|
2 | thread = this <<< {
|
3 | on: (e, f, q) ->
|
4 | if q = thread._on[e] then q.push f else thread._on[e] = [f]
|
5 | return thread
|
6 | once: (e, f, q) ->
|
7 | not (q = thread._on[e]) and (q = thread._on[e] = [])
|
8 | if q.once then q.once.push f else q.once = [f]
|
9 | return thread
|
10 | remove-all-listeners: (e) ->
|
11 | if e then delete! thread._on[e] else thread._on = {}
|
12 | return thread
|
13 | dispatch-events: (event, args, q, i, len) ->
|
14 | if q = thread._on[event] => try
|
15 | i = 0
|
16 | len = q.length
|
17 | while i < len
|
18 | q[i++].apply thread, args
|
19 | if q = q.once
|
20 | q.once = ``undefined``
|
21 | i = 0
|
22 | len = q.length
|
23 | while i < len
|
24 | q[i++].apply thread, args
|
25 | catch
|
26 | __postError { message: e, filename: '', lineno: 0 }
|
27 | _on: {}
|
28 | }
|
29 | return @dispatch-events
|