summaryrefslogtreecommitdiff
path: root/node_modules/webworker-threads/src/events.ls
blob: 4e604b7fb1459155b2aa470621e12a6661ebb5b1 (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
function DispatchEvents (thread)
    thread = this <<< {
        on: (e, f, q) ->
            if q = thread._on[e] then q.push f else thread._on[e] = [f]
            return thread
        once: (e, f, q) ->
            not (q = thread._on[e]) and (q = thread._on[e] = [])
            if q.once then q.once.push f else q.once = [f]
            return thread
        remove-all-listeners: (e) ->
            if arguments_.length then delete! thread._on[e] else thread._on = {}
            return thread
        dispatch-events: (event, args, q, i, len) ->
            if q = thread._on[event] => try
                i = 0
                len = q.length
                while i < len
                    q[i++].apply thread, args
                if q = q.once
                    q.once = ``undefined``
                    i = 0
                    len = q.length
                    while i < len
                        q[i++].apply thread, args
            catch
              __postError { message: e, filename: '', lineno: 0 }
        _on: {}
    }
    return @dispatch-events