/
live-search.coffee
58 lines (43 loc) · 1.47 KB
/
live-search.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
window.OrderByScore = class OrderByScore
constructor: () ->
@scores = []
@scores_hit = {}
get_pos: (score) ->
pos = _(@scores).sortedIndex score
unless @scores[pos]
@scores[pos] = score
else if (@scores[pos] != score) and (@scores[pos+1] != score)
@scores.splice(pos, 0, score)
@scores_hit[score] or= 0
@scores_hit[score]++
#console.log "get_pos", score, pos#, @scores
pos
remove_score: (score) ->
@scores_hit[score]--
if @scores_hit[score] == 0
@scores = _(@scores).without score
window.SearchStream = class SearchStream
instances = {}
constructor: (search_term, opts) ->
if @constructor.name is "SearchStream"
@search_term = search_term
@ee = new EventEmitter2()
my_timer = null
search_me = =>
unless SearchStream.socket?.emit?
my_timer = setInterval(search_me, 100) unless my_timer
else
clearInterval(my_timer) if my_timer
SearchStream.socket.emit "search", @search_term, =>
@ee.emit "finished"
_.defer search_me
else
return instances[search_term] or= new SearchStream(search_term, opts)
on: (msg, cb) -> @ee.on msg, cb
emit: (msg, data...) -> @ee.emit msg, data...
@com_init: (socket) ->
#console.log "handling init"
socket.on "search_result", (res) =>
@get(res.search_term)?.ee.emit "data", res.data
SearchStream.socket = socket
@get: (search_term) -> instances[search_term]