This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
154 lines (137 loc) · 4.34 KB
/
client.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
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from (note: done by srallen)
* DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__, or convert again using --optional-chaining (note: done by srallen)
* DS205: Consider reworking code to avoid use of IIFEs
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
class SugarClient {
static initClass() {
this.host = null;
this.Primus = null;
}
constructor(userId, authToken) {
this.primusUrl = this.primusUrl.bind(this);
this.connect = this.connect.bind(this);
this.disconnect = this.disconnect.bind(this);
this.receiveData = this.receiveData.bind(this);
this.subscribeTo = this.subscribeTo.bind(this);
this.unsubscribeFrom = this.unsubscribeFrom.bind(this);
this.on = this.on.bind(this);
this.off = this.off.bind(this);
this.emit = this.emit.bind(this);
this.__subscribeToChannels = this.__subscribeToChannels.bind(this);
this.__subscribeTo = this.__subscribeTo.bind(this);
this.createEvent = this.createEvent.bind(this);
this.userId = userId;
this.authToken = authToken;
this.events = { };
this.subscriptions = { };
this.initializePrimus();
}
initializePrimus() {
if (SugarClient.Primus == null) { throw 'SugarClient.Primus is not defined'; }
if (SugarClient.host == null) { throw 'SugarClient.host is not defined'; }
this.primus = SugarClient.Primus.connect(SugarClient.host, {
websockets: true,
network: true,
manual: true,
ping: 10000
}
);
this.primus.on('outgoing::url', this.primusUrl);
return this.primus.on('data', this.receiveData);
}
host() {
return SugarClient.host;
}
primusUrl(baseUrl) {
if (this.userId && this.authToken) {
return baseUrl.query = `user_id=${ this.userId }&auth_token=${ this.authToken }`;
}
}
connect() {
this.disconnect();
return this.primus.open();
}
disconnect() {
let key;
let userKeys = [];
userKeys = ((() => {
const result = [];
for (key in this.subscriptions) {
const _ = this.subscriptions[key];
if (key.match(/^(session|user):/i)) {
result.push(key);
}
}
return result;
})());
for (key of userKeys) { delete this.subscriptions[key]; }
this.userKey = (this.loggedIn = null);
return this.primus.end();
}
receiveData(data) {
if (data.type === 'connection') {
if (console && console.info) {
console.info('[CONNECTED] ', data);
}
this.loggedIn = data.loggedIn;
this.userKey = data.userKey;
this.subscriptions[this.userKey] = true;
return setTimeout(this.__subscribeToChannels, 100);
} else {
return this.emit(data);
}
}
subscribeTo(channel) {
if (this.subscriptions[channel]) { return false; }
this.subscriptions[channel] = true;
return this.__subscribeTo(channel);
}
unsubscribeFrom(channel) {
if (!this.subscriptions[channel]) { return; }
delete this.subscriptions[channel];
return this.primus.write({action: 'Unsubscribe', params: { channel }});
}
on(type, callback) {
if (!this.events[type]) { this.events[type] = []; }
return this.events[type].push(callback);
}
off(type, callback) {
if (callback && this.events[type]) {
return this.events[type] = this.events[type].filter(cb => cb !== callback);
} else {
return delete this.events[type];
}
}
emit(data) {
const callbacks = this.events[data.type] || [];
return callbacks.map((callback) => callback(data));
}
__subscribeToChannels() {
return (() => {
const result = [];
for (let channel in this.subscriptions) {
const _ = this.subscriptions[channel];
result.push(this.__subscribeTo(channel));
}
return result;
})();
}
__subscribeTo(channel) {
return this.primus.write({action: 'Subscribe', params: { channel }});
}
createEvent(type, channel, data) {
return this.primus.write({
action: 'Event',
params: { type, channel, data }});
}
}
SugarClient.initClass();
if (typeof module !== 'undefined' && module !== null) {
module.exports = SugarClient;
}