-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitterShell.coffee
61 lines (44 loc) · 1.47 KB
/
emitterShell.coffee
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
# @ts-check
import $each from './each'
import $filter from './filter'
import $noop from './noop'
import $push from './push'
import $split from './split'
class EmitterShell
constructor: ->
###* @type import('../type/emitterShell').EmitterShell['bus'] ###
@bus = []
###* @type import('../type/emitterShell').EmitterShell['emit'] ###
emit: (key, args...) ->
[$type, $name] = $split key, '.'
unless $type then return
if $name then $list = $filter @bus, ($it) ->
$it[0] == $type and $it[1] == $name
else $list = $filter @bus, ($it) -> $it[0] == $type
$each $list, ($it) ->
$it[2] args...
if $it[3] == 'once' then $it[3] = 'expired'
return
@bus = $filter @bus, ($it) -> $it[3] != 'expired'
return
###* @type import('../type/emitterShell').EmitterShell['off'] ###
off: (key) ->
[$type, $name] = $split key, '.'
unless $type
@bus = []
return
if $name then @bus = $filter @bus, ($it) ->
!($it[0] == $type and $it[1] == $name)
else @bus = $filter @bus, ($it) -> $it[0] != $type
return
###* @type import('../type/emitterShell').EmitterShell['on'] ###
on: (key, callback) ->
[$type, $name] = $split key, '.'
$push @bus, [$type, $name, callback, 'always']
return
###* @type import('../type/emitterShell').EmitterShell['once'] ###
once: (key, callback) ->
[$type, $name] = $split key, '.'
$push @bus, [$type, $name, callback, 'once']
return
$noop EmitterShell