This repository was archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathMediator.coffee
More file actions
141 lines (119 loc) · 4.15 KB
/
Mediator.coffee
File metadata and controls
141 lines (119 loc) · 4.15 KB
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
class Mediator
constructor: (obj, @cascadeChannels=false) ->
@channels = {}
if obj instanceof Object then @installTo obj
else if obj is true then @cascadeChannels=true
# ## Subscribe to a topic
#
# Parameters:
#
# - (String) topic - The topic name
# - (Function) callback - The function that gets called if an other module
# publishes to the specified topic
# - (Object) context - The context the function(s) belongs to
on: (channel, fn, context=@) ->
@channels[channel] ?= []
that = @
if channel instanceof Array
@on id, fn, context for id in channel
else if typeof channel is "object"
@on k,v,fn for k,v of channel
else
return false unless typeof channel is "string"
subscription = { context: context, callback: fn or -> }
(
attach: -> that.channels[channel].push subscription; @
detach: -> Mediator._rm that, channel, subscription.callback; @
pipe: -> that.pipe.apply that, [channel, arguments...]; @
).attach()
# ## Unsubscribe from a topic
#
# Parameters:
#
# - (String) topic - The topic name
# - (Function) callback - The function that gets called if an other module
# publishes to the specified topic
off: (ch, cb) ->
switch typeof ch
when "string"
Mediator._rm @,ch,cb if typeof cb is "function"
Mediator._rm @,ch if typeof cb is "undefined"
when "function" then Mediator._rm @,id,ch for id of @channels
when "undefined" then Mediator._rm @,id for id of @channels
when "object" then Mediator._rm @,id,null,ch for id of @channels
@
_getTasks = (data, channel, originalChannel, ctx) ->
subscribers = ctx.channels[channel] or []
for sub in subscribers then do (sub) ->
(next) ->
try
if util.hasArgument sub.callback, 3
sub.callback.apply sub.context, [data, originalChannel, next]
else
next null, sub.callback.apply sub.context, [data, originalChannel]
catch e
next e
# ## Publish an event
#
# Parameters:
# - (String) topic - The topic name
# - (Object) data - The data that gets published
# - (Funtction) - callback method
emit: (channel, data, cb=(->), originalChannel=channel) ->
if typeof data is "function"
cb = data
data = undefined
return false unless typeof channel is "string"
tasks = _getTasks data, channel, originalChannel, @
util.runSeries tasks,((errors, results) ->
if errors
e = new Error (x.message for x in errors when x?).join '; '
cb e), true
if @cascadeChannels and (chnls = channel.split '/').length > 1
o = originalChannel if @emitOriginalChannels
@emit chnls[0...-1].join('/'), data, cb, o
@
# ## Send a task
#
# Parameters:
# - (String) topic - The topic name
# - (Object) data - The data that gets published
# - (Function) - callback method
send: (channel, data, cb=->) ->
if typeof data is "function"
cb = data
data = undefined
return false unless typeof channel is "string"
tasks = _getTasks data, channel, channel, @
util.runFirst tasks,((errors, result) ->
if errors
e = new Error (x.message for x in errors when x?).join '; '
cb e
else
cb null, result), true
@
# ## Install Pub/Sub functions to an object
installTo: (obj,force) ->
if typeof obj is "object"
for k,v of @
if force then obj[k] = v
else obj[k] ?= v
@
pipe: (src, target, mediator) ->
if target instanceof Mediator
mediator = target; target = src
return @pipe src, target, @ unless mediator?
# prevent cycles
return @ if mediator is @ and src is target
@on src, -> mediator.emit.apply mediator, [target, arguments...]
@
@_rm: (o, ch, cb, ctxt) ->
return unless o.channels[ch]?
o.channels[ch] = (s for s in o.channels[ch] when (
if cb?
s.callback isnt cb
else if ctxt?
s.context isnt ctxt
else
s.context isnt o
))