-
Notifications
You must be signed in to change notification settings - Fork 11.4k
/
Copy pathRoomManager.coffee
123 lines (93 loc) · 2.85 KB
/
RoomManager.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
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
Meteor.startup ->
ChatMessage.find().observe
removed: (record) ->
recordBefore = ChatMessage.findOne {ts: {$lt: record.ts}}, {sort: {ts: -1}}
if recordBefore?
ChatMessage.update {_id: recordBefore._id}, {$set: {tick: new Date}}
@RoomManager = new class
defaultTime = 600000 # 10 minutes
openedRooms = {}
subscription = null
msgStream = new Meteor.Stream 'messages'
deleteMsgStream = new Meteor.Stream 'delete-message'
onlineUsers = new ReactiveVar {}
Dep = new Tracker.Dependency
init = ->
subscription = Meteor.subscribe('subscription')
return subscription
close = (rid) ->
if openedRooms[rid]
if openedRooms[rid].sub?
for sub in openedRooms[rid].sub
sub.stop()
msgStream.removeListener rid
deleteMsgStream.removeListener rid
openedRooms[rid].ready = false
openedRooms[rid].active = false
delete openedRooms[rid].timeout
delete openedRooms[rid].dom
ChatMessage.remove rid: rid
computation = Tracker.autorun ->
for rid, record of openedRooms when record.active is true
record.sub = [
Meteor.subscribe 'room', rid
# Meteor.subscribe 'messages', rid
]
record.ready = record.sub[0].ready()
# record.ready = record.sub[0].ready() and record.sub[1].ready()
Dep.changed()
setRoomExpireExcept = (except) ->
if openedRooms[except]?.timeout?
clearTimeout openedRooms[except].timeout
delete openedRooms[except].timeout
for rid of openedRooms
if rid isnt except and not openedRooms[rid].timeout?
openedRooms[rid].timeout = setTimeout close, defaultTime, rid
open = (rid) ->
if not openedRooms[rid]?
openedRooms[rid] =
active: false
ready: false
setRoomExpireExcept rid
if subscription.ready()
# if ChatSubscription.findOne { rid: rid }, { reactive: false }
if openedRooms[rid].active isnt true
openedRooms[rid].active = true
msgStream.on rid, (msg) ->
ChatMessage.upsert { _id: msg._id }, msg
deleteMsgStream.on rid, (msg) ->
ChatMessage.remove _id: msg._id
computation.invalidate()
return {
ready: ->
Dep.depend()
return openedRooms[rid].ready
}
getDomOfRoom = (rid) ->
room = openedRooms[rid]
if not room?
return
if not room.dom?
room.dom = document.createElement 'div'
room.dom.classList.add 'room-container'
Blaze.renderWithData Template.room, { _id: rid }, room.dom
return room.dom
existsDomOfRoom = (rid) ->
room = openedRooms[rid]
return room?.dom?
updateUserStatus = (user, status) ->
onlineUsersValue = onlineUsers.curValue
if status is 'offline'
delete onlineUsersValue[user.username]
else
onlineUsersValue[user.username] = status
onlineUsers.set onlineUsersValue
open: open
close: close
init: init
getDomOfRoom: getDomOfRoom
existsDomOfRoom: existsDomOfRoom
msgStream: msgStream
openedRooms: openedRooms
updateUserStatus: updateUserStatus
onlineUsers: onlineUsers