-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.js
49 lines (34 loc) · 1.37 KB
/
User.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
const v4 = require('uuid').v4;
const Immutable = require('immutable');
function User(socket, data) {
this._id = v4();
this.socket = socket;
this.data = data; // For your User model, e.g. { _id: ..., username: "test123", password: "fas98dhf3892hfdsf", ... }
this.sendEvent = eventPayload => {
this.socket.emit('lively_event', eventPayload);
};
this.sendError = (actionPayload, error) => this.socket.emit('lively_error', {actionPayload, error});
this.subscriptions = Immutable.List();
this.subscribeTo = (document) => {
const modelName = document.constructor.modelName;
const _id = document._id;
const subscription = `${modelName}#${_id}`;
this.subscriptions = this.subscriptions.push(subscription);
this.socket.join(subscription);
};
this.unsubscribeFrom = (document) => {
const modelName = document.constructor.modelName;
const _id = document._id;
const subscription = `${modelName}#${_id}`;
this.subscriptions = this.subscriptions.filter(sub => sub !== subscription);
this.socket.leave(subscription);
};
this.clearSubscriptions = () => {
this.subscriptions.forEach((subscription) => {
this.socket.leave(subscription);
});
this.subscriptions = this.subscriptions.clear();
};
return this;
}
module.exports = User;