/
presence_store.js
171 lines (138 loc) · 4.97 KB
/
presence_store.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var _ = require('underscore')
var logging = require('minilog')('radar:presence_store')
function PresenceStore (scope) {
this.scope = scope
this.map = {}
this.cache = {}
this.socketUserMap = {}
this.userTypes = {}
}
require('util').inherits(PresenceStore, require('events').EventEmitter)
// Cache the client data without adding
PresenceStore.prototype.cacheAdd = function (clientSessionId, message) {
this.cache[clientSessionId] = message
}
PresenceStore.prototype.cacheRemove = function (clientSessionId) {
var val = this.cache[clientSessionId]
delete this.cache[clientSessionId]
return val
}
PresenceStore.prototype.add = function (clientSessionId, userId, userType, message) {
var self = this
var events = []
logging.debug('#presence - store.add', userId, clientSessionId, message, this.scope)
this.cacheRemove(clientSessionId)
if (!this.map[userId]) {
events.push('user_added')
this.map[userId] = {}
this.userTypes[userId] = userType
}
if (!this.map[userId][clientSessionId]) {
events.push('client_added')
this.map[userId][clientSessionId] = message
this.socketUserMap[clientSessionId] = userId
} else {
var previous = this.map[userId][clientSessionId]
if (message.clientData && !_.isEqual(message.clientData, previous.clientData)) {
events.push('client_updated')
this.map[userId][clientSessionId] = message
}
}
events.forEach(function (event) {
logging.debug('#presence - store.emit', event, message, self.scope)
self.emit(event, message)
})
}
PresenceStore.prototype.remove = function (clientSessionId, userId, message) {
var self = this
var events = []
logging.debug('#presence - store.remove', userId, clientSessionId, message, this.scope)
this.cacheRemove(clientSessionId)
// When non-existent, return
if (!this.map[userId] || !this.map[userId][clientSessionId]) {
return
}
events.push('client_removed')
delete this.map[userId][clientSessionId]
delete this.socketUserMap[clientSessionId]
// Empty user
if (Object.keys(this.map[userId]).length === 0) {
events.push('user_removed')
delete this.map[userId]
delete this.userTypes[userId]
}
events.forEach(function (ev) {
logging.debug('#presence - store.emit', ev, message, self.scope)
self.emit(ev, message)
})
}
PresenceStore.prototype.removeClient = function (clientSessionId, message) {
var userId = this.socketUserMap[clientSessionId]
this.cacheRemove(clientSessionId)
// When non-existent, return
if (!userId) {
logging.warn('#presence - store.removeClient: cannot find data for',
clientSessionId, this.scope)
return
}
logging.debug('#presence - store.removeClient', userId, clientSessionId, message, this.scope)
delete this.map[userId][clientSessionId]
delete this.socketUserMap[clientSessionId]
logging.debug('#presence - store.emit', 'client_removed', message, this.scope)
this.emit('client_removed', message)
}
PresenceStore.prototype.removeUserIfEmpty = function (userId, message) {
if (this.userExists(userId) && this.userEmpty(userId)) {
logging.debug('#presence - store.removeUserIfEmpty', userId, message, this.scope)
delete this.map[userId]
delete this.userTypes[userId]
logging.debug('#presence - store.emit', 'user_removed', message, this.scope)
this.emit('user_removed', message)
}
}
PresenceStore.prototype.userOf = function (clientSessionId) {
return this.socketUserMap[clientSessionId]
}
PresenceStore.prototype.get = function (clientSessionId, userId) {
return (this.map[userId] && this.map[userId][clientSessionId])
}
PresenceStore.prototype.users = function () {
return Object.keys(this.map)
}
PresenceStore.prototype.sockets = function (userId) {
return ((this.map[userId] && Object.keys(this.map[userId])) || [])
}
PresenceStore.prototype.forEachClient = function (callback) {
var store = this
this.users().forEach(function (userId) {
store.sockets(userId).forEach(function (clientSessionId) {
if (callback) callback(userId, clientSessionId, store.get(clientSessionId, userId))
})
})
}
PresenceStore.prototype.userEmpty = function (userId) {
return !!(this.map[userId] && Object.keys(this.map[userId]).length === 0)
}
PresenceStore.prototype.userTypeOf = function (userId) {
return this.userTypes[userId]
}
PresenceStore.prototype.userExists = function (userId) {
return !!this.map[userId]
}
// This returns a list of clientSessionIds, which is not costly. The code that calls
// this code uses each clientSessionId in a separate chained call, the sum of which is
// costly.
PresenceStore.prototype.clientSessionIdsForSentryId = function (sentryId) {
var map = this.map
var clientSessionIds = []
Object.keys(map).forEach(function (userId) {
Object.keys(map[userId]).forEach(function (clientSessionId) {
var data = map[userId][clientSessionId]
if (data && data.sentry === sentryId) {
clientSessionIds.push(clientSessionId)
}
})
})
return clientSessionIds
}
module.exports = PresenceStore