UNPKG

1.05 kBtext/coffeescriptView Raw
1Message = require './message'
2_ = require 'underscore'
3
4module.exports = class
5
6 ###
7 ###
8
9 constructor: (@router) ->
10 @_pluginsByName = {}
11 @_using = [];
12
13 using: () -> @_using
14
15 ###
16 ###
17
18 add: (plugin) ->
19
20 if plugin instanceof Array
21 for plg in plugin
22 @add plg
23 return
24
25 @_using.push plugin
26
27 ## create the module
28 mod = plugin @router
29
30 ## assign by the name
31 @_pluginsByName[mod.name] = mod
32
33 ## extending the prototype of the message
34 _.extend @router._messageBuilder, mod.message
35
36
37 ## extend the router
38 _.extend @router, mod.router
39
40 ## set the dispatcher
41 @router.directors[mod.name] = mod.director if mod.director
42
43
44 ###
45 ###
46
47 get: (name) -> @_pluginsByName[name]
48
49
50 ###
51 Used incase the listener needs to be handler for a particular reason, e.g: push -pull /some/route would be a binding.
52 ###
53
54 newListener: (listener) -> @_emit 'newListener', listener
55
56
57 ###
58 ###
59
60 _emit: (type, data) ->
61 for pluginName of @_pluginsByName
62 plugin = @_pluginsByName[pluginName]
63 plugin[type](data) if plugin[type]
64