-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlegato.coffee
33 lines (27 loc) · 878 Bytes
/
legato.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
_ = require 'lodash'
@___ = ___ = -> console.log '[legato]', arguments...; arguments[0]
@____ = (arg) -> -> ___ arg, arguments...; arguments[0]
for lib in 'amixer midi osc firmata'.split ' '
@[lib] = require './'+lib
routes = [] # [ [path, cb]* ]
@dispatch = dispatch = (path, val) ->
for [path_, cb] in routes
if path.match path_
(_.bind cb, path:path, val:val) val
@in = (prefix, input) ->
___ 'in+ ', prefix
input (path, val) -> dispatch prefix+path, val
@on = (path, cb) ->
path_ = '^' + (path.replace /\:([^\/]*)/g, '([^/]*)') + '$'
___ 'route+', path, ' ->', path_
routes.push [path_, cb]
@throttle = (time, fn) -> _.throttle fn, time
@delay = (time, fn) -> _.delay fn, time
@closet = []
@init = =>
global.__legato_deinit?()
global.__legato_deinit = =>
cb() for cb in @closet
@closet.length = routes.length = 0
___ 'init'
@