-
Notifications
You must be signed in to change notification settings - Fork 72
/
misc.coffee
70 lines (62 loc) · 1.73 KB
/
misc.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
62
63
64
65
66
67
68
69
70
{debounce} = require 'underscore-plus'
module.exports =
paths: require './misc/paths'
blocks: require './misc/blocks'
cells: require './misc/cells'
words: require './misc/words'
weave: require './misc/weave'
colors: require './misc/colors'
scopes: require './misc/scopes'
bufferLines: (t, f) ->
if not f? then [t, f] = [null, t]
buffer = ['']
flush = if not t? then -> else debounce (->
if buffer[0] isnt ''
f buffer[0], false
buffer[0] = ''), t
(data) ->
lines = data.toString().split '\n'
buffer[0] += lines.shift()
buffer.push lines...
while buffer.length > 1
f buffer.shift(), true
flush()
time: (desc, p) ->
s = -> new Date().getTime()/1000
t = s()
p.then -> console.log "#{desc}: #{(s()-t).toFixed(2)}s"
.catch ->
p
hook: (obj, method, f) ->
souper = obj[method].bind obj
obj[method] = (a...) -> f souper, a...
once: (f) ->
done = false
(args...) ->
return if done
done = true
f.call @, args...
mutex: ->
wait = Promise.resolve()
(f) ->
current = wait
release = null
wait = new Promise((resolve) -> release = resolve).catch ->
current.then => f.call @, release
exclusive: (f) ->
lock = module.exports.mutex()
(args...) ->
lock (release) =>
result = f.call @, args...
release result
result
# takes a time period in seconds and formats it as hh:mm:ss
formatTimePeriod: (dt) ->
return unless dt > 1
h = Math.floor dt/(60*60)
m = Math.floor (dt -= h*60*60)/60
s = Math.round (dt - m*60)
parts = [h, m, s]
for i, dt of parts
parts[i] = if dt < 10 then "0#{dt}" else "#{dt}"
parts.join ':'