/
environment.js
280 lines (228 loc) · 6.12 KB
/
environment.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Requires
var Q = require('q');
var _ = require('underscore');
var Document = require('./models/document').Document;
/*
* Environment represents a synchronized environment
* between multiple users
*/
function Environment(envId, creator, service) {
_.bindAll(this);
// Represents a synchronized file
this.users = {};
this.envId = envId;
this.locked = false;
this.modified = false;
this.service = service;
this.doc = new Document(null, creator.userId, this.service);
}
Environment.prototype.setup = function() {
var that = this;
return this.loadCache().then(function() {
return that;
});
};
Environment.prototype.hasUser = function(user) {
return user.key() in this.users;
};
Environment.prototype.addUser = function(user) {
if(this.hasUser(user)) {
return Q(true);
}
// Set member (prototypal inheritance)
function member() {}
member.prototype = user;
this.users[user.key()] = new member();
user.on('exit', this.userExit);
return Q(true);
};
Environment.prototype.userExit = function(user) {
if(!this.hasUser(user)) {
return Q(true);
}
// Remove user from environment
this.removeUser(user);
var base = Q();
if (this.doc.path != null) {
// Close the file
base = user.close(this.doc.path)
}
var that = this;
// then notify other users of departure
return base.then(function() {
return that.pingOthers(user);
});
};
Environment.prototype.removeUser = function(user) {
delete this.users[user.key()];
};
Environment.prototype.getUser = function(user) {
return this.users[user.key()];
};
Environment.prototype.modifiedState = function(state) {
if (this.modified == state) return;
this.modified = state;
return this.notifyAll("modified", {
'state': this.modified
});
};
Environment.prototype.usersInfo = function() {
// Get infornmation on every use in the environment
return _.map(this.users, function(user) {
return user.info();
});
};
Environment.prototype.updateCache = _.debounce(function(data) {
// Only write to cache every 60s max
return this.doc.update();
}, 60 * 1e3);
Environment.prototype.loadCache = function() {
return this.doc.load();
};
Environment.prototype._notifyUser = function(user, action, data) {
var metaInfo = {
'environment': this.envId
};
// Optionnal action
if(action) metaInfo.action = action;
// Add meta data and remove token (if present)
var finalData = _.omit(
_.extend(metaInfo, data),
'token'
);
return user.socket.json.send(finalData);
};
Environment.prototype.notifyAll = function(action, data) {
var users = _.values(this.users);
return this.notify(users, action, data);
};
Environment.prototype.notifyOthers = function(user, action, data) {
// Filter out itself
var users = _.values(
_.omit(this.users, user.key())
);
return this.notify(users, action, data);
};
Environment.prototype.notify = function(users, action, data) {
if(!_.isArray(users)) {
users = [users];
}
var that = this;
_.each(
users,
function(user) {
return that._notifyUser(user, action, data);
}
);
};
/*
* Utility functions
*/
Environment.prototype.getSyncData = function() {
return {
content: this.doc.getContent(),
participants: this.usersInfo(),
state: this.modified,
path: this.doc.path
};
};
/*
* Internal actions
*/
Environment.prototype.syncAll = function() {
return this.notifyAll('sync',
this.getSyncData()
);
};
Environment.prototype.pingOthers = function(user) {
// List all current users with their respective info
return this.notifyOthers(user, 'participants',
{
participants: this.usersInfo()
}
);
};
/*
* Actions
*/
Environment.prototype.sync = function(user, payload) {
// Synchronize content
return this.notify(user, 'sync',
this.getSyncData()
);
};
Environment.prototype.save = function(user, payload) {
var that = this;
if (payload.path) {
this.doc.setPath(payload.path);
}
// Synchronize content
this.doc.save().then(function() {
that.modifiedState(false);
that.syncAll();
});
};
Environment.prototype.load = function(user, payload) {
var that = this;
if (this.doc.path == payload.path) {
return this.sync(user, payload);
}
user.open(payload.path).then(function(authorized) {
if(!authorized) {
throw new Error("User was not allowed to open file");
}
that.doc.setPath(payload.path);
return that.doc.load();
}).then(function() {
that.syncAll();
});
};
Environment.prototype.ping = function(user, payload) {
// List all current users with their respective info
return this.notify(user, 'participants',
{
participants: this.usersInfo()
}
);
};
Environment.prototype.selectCursor = function(user, payload) {
var member = this.getUser(user);
member.select(
payload.start.x,
payload.start.y,
payload.end.x,
payload.end.y
);
return this.notifyOthers(user, null, payload);
};
Environment.prototype.moveCursor = function(user, payload) {
var member = this.getUser(user);
member.move(
payload.cursor.x,
payload.cursor.y
);
return this.notifyOthers(user, null, payload);
};
Environment.prototype.patch = function(user, payload) {
var patched = this.doc.patch(
payload.patch,
payload.hashs.before,
payload.hashs.after
);
// Failed patching
if(patched == null) {
return this.syncAll();
}
this.modifiedState(patched);
return this.notifyOthers(user, null, payload);
};
Environment.prototype.close = function(user, payload) {
return this.userExit(user);
};
function createEnvironment(path, user, service) {
var env = new Environment(path, user, service);
return env.setup();
}
// Exports
exports.Environment = Environment;
exports.createEnvironment = createEnvironment;