/
clientGroup.js
123 lines (93 loc) · 3.43 KB
/
clientGroup.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
var proxy = require('./wrap.js');
var nowUtil = require('./nowUtil.js').nowUtil;
var EventEmitter = require('events').EventEmitter;
exports.ClientGroup = function ClientGroup(nowCore, socket, groupName){
var groupScopes = {};
var nowScope = {};
var isSuperGroup = false;
function connected(func) {
this.on('connect', func);
};
function disconnected(func) {
this.on('disconnect', func);
};
var store = {
set: function(key, val, callback, changes){
nowUtil.debug("everyoneStore", key + " => " + val);
var currScopes = [];
// Put all relevant nowCore.scopes in array so we traverse only once
for(var i in groupScopes) {
currScopes.push(groupScopes[i]);
}
currScopes.push(nowCore.defaultScopes[groupName]);
nowUtil.mergeChanges(currScopes, changes);
if(isSuperGroup){
var obj = {};
obj[key] = val;
nowUtil.multiMergeFunctionsToMulticallers(nowCore.groups, obj);
}
var data = nowUtil.decycle(val, "now."+key, [function(fqn, func){
var multiCaller = generateMultiCaller(fqn);
nowUtil.createVarAtFqn(fqn, nowScope, multiCaller);
return nowUtil.serializeFunction(fqn, func);
}]);
var clients = socket.clients;
for(var clientId in groupScopes){
clients[clientId].send({type: 'replaceVar', data: {key: key, value: data[0]}});
}
callback();
},
remove: function(key){
// console.log("remove " + key);
}
}
function setAsSuperGroup(){
isSuperGroup = true;
}
function addUser(clientId){
if(nowCore.scopes.hasOwnProperty(clientId)){
if(!isSuperGroup){
nowUtil.multiMergeFunctionsToMulticallers([this], nowCore.scopes[clientId]);
nowCore.clientGroups[clientId].push(this);
}
groupScopes[clientId] = nowCore.scopes[clientId];
nowUtil.mergeScopes(nowCore.scopes[clientId], nowCore.defaultScopes[groupName]);
this.count++;
this.emit.apply({_events: this._events, now: nowCore.proxies[clientId], user:{clientId: clientId}}, ['connect', clientId]);
} else {
throw new Error("Invalid client id");
}
};
function removeUser(clientId){
if(groupScopes.hasOwnProperty(clientId)){
this.count--;
this.emit.apply({_events: this._events, now: nowCore.proxies[clientId], user:{clientId: clientId}}, ['disconnect', clientId]);
delete groupScopes[clientId];
}
};
function generateMultiCaller(fqn){
nowUtil.debug("generateMultiCaller", fqn);
return function multicall(){
for(var clientId in groupScopes){
nowUtil.debug("Multicaller", "Calling "+fqn+" on client " + clientId);
var clientScope = groupScopes[clientId];
var theFunction = nowUtil.getVarFromFqn(fqn, clientScope);
if(theFunction !== false) {
theFunction.apply({now: clientScope, user: {clientId: clientId}}, arguments);
} else {
nowUtil.debug("Multicaller", "No function found for client " + clientId);
}
}
};
};
this.count = 0;
this.now = proxy.wrap(store, nowScope);
this.connected = connected;
this.disconnected = disconnected;
this.addUser = addUser;
this.removeUser = removeUser;
this.nowScope = nowScope;
this.generateMultiCaller = generateMultiCaller;
this.setAsSuperGroup = setAsSuperGroup;
}
exports.ClientGroup.prototype = new EventEmitter();