@subiz/wsclient 3.1.8 → 3.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/realtime.js +30 -25
package/package.json
CHANGED
package/realtime.js
CHANGED
|
@@ -22,7 +22,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
|
|
|
22
22
|
var lastToken = ''
|
|
23
23
|
|
|
24
24
|
// kill force the connection to dead state
|
|
25
|
-
this.kill = function() {
|
|
25
|
+
this.kill = function () {
|
|
26
26
|
dead = true
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -31,9 +31,9 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
|
|
|
31
31
|
// exponential backoff algorithm. Polling loop will terminate if it encounter an
|
|
32
32
|
// un-retryable error
|
|
33
33
|
// the polling loop starts after the first subscribe call finished successfully
|
|
34
|
-
var polling = function(backoff) {
|
|
34
|
+
var polling = function (backoff) {
|
|
35
35
|
if (dead) return
|
|
36
|
-
callAPI('get', apiUrl + 'poll?token=' + lastToken, undefined, function(body, code) {
|
|
36
|
+
callAPI('get', apiUrl + 'poll?token=' + lastToken, undefined, function (body, code) {
|
|
37
37
|
if (dead) return
|
|
38
38
|
if (retryable(code)) return setTimeout(polling, calcNextBackoff(backoff), backoff + 1)
|
|
39
39
|
if (code !== 200) {
|
|
@@ -55,21 +55,26 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
|
|
|
55
55
|
})
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
var subQueue = flow.batch(50, 100, function(events) {
|
|
58
|
+
var subQueue = flow.batch(50, 100, function (events) {
|
|
59
59
|
if (dead) return ['dead']
|
|
60
60
|
if (events.length <= 0) return []
|
|
61
61
|
|
|
62
62
|
var out = []
|
|
63
63
|
return flow
|
|
64
|
-
.loop(function() {
|
|
65
|
-
return new Promise(function(rs) {
|
|
64
|
+
.loop(function () {
|
|
65
|
+
return new Promise(function (rs) {
|
|
66
66
|
var query = '?token=' + lastToken
|
|
67
|
-
credential.getAccessToken().then(function(access_token) {
|
|
68
|
-
if (credential.user_ref)
|
|
67
|
+
credential.getAccessToken().then(function (access_token) {
|
|
68
|
+
if (credential.user_ref)
|
|
69
|
+
query +=
|
|
70
|
+
'&user_ref=' +
|
|
71
|
+
encodeURIComponent(credential.user_ref) +
|
|
72
|
+
'&account_id=' +
|
|
73
|
+
encodeURIComponent(credential.account_id)
|
|
69
74
|
else if (credential.user_mask) query += '&user-mask=' + encodeURIComponent(credential.user_mask)
|
|
70
75
|
else if (access_token) query += '&access-token=' + access_token
|
|
71
76
|
|
|
72
|
-
callAPI('post', apiUrl + 'subs' + query, JSON.stringify({events: events}), function(body, code) {
|
|
77
|
+
callAPI('post', apiUrl + 'subs' + query, JSON.stringify({events: events}), function (body, code) {
|
|
73
78
|
if (dead) {
|
|
74
79
|
out = repeat('dead', events.length)
|
|
75
80
|
return rs(false) // break loop
|
|
@@ -102,7 +107,7 @@ function Conn(apiUrl, credential, onDead, onEvents, callAPI) {
|
|
|
102
107
|
})
|
|
103
108
|
})
|
|
104
109
|
})
|
|
105
|
-
.then(function() {
|
|
110
|
+
.then(function () {
|
|
106
111
|
return out
|
|
107
112
|
})
|
|
108
113
|
})
|
|
@@ -124,7 +129,7 @@ function Realtime(apiUrls, credential, callAPI) {
|
|
|
124
129
|
if (typeof apiUrls === 'string' || apiUrls instanceof String) apiUrls = [apiUrls]
|
|
125
130
|
credential = credential || {}
|
|
126
131
|
if (!credential.getAccessToken) {
|
|
127
|
-
credential.getAccessToken = function() {
|
|
132
|
+
credential.getAccessToken = function () {
|
|
128
133
|
return Promise.resolve('')
|
|
129
134
|
}
|
|
130
135
|
}
|
|
@@ -141,22 +146,22 @@ function Realtime(apiUrls, credential, callAPI) {
|
|
|
141
146
|
|
|
142
147
|
// stop the connection
|
|
143
148
|
var stop = false
|
|
144
|
-
this.stop = function() {
|
|
149
|
+
this.stop = function () {
|
|
145
150
|
if (stop) return
|
|
146
151
|
stop = true
|
|
147
152
|
conn.kill()
|
|
148
153
|
}
|
|
149
154
|
|
|
150
|
-
this.onEvent = function(cb) {
|
|
155
|
+
this.onEvent = function (cb) {
|
|
151
156
|
return pubsub.on('event', cb)
|
|
152
157
|
}
|
|
153
158
|
|
|
154
|
-
this.onInterrupted = function(cb) {
|
|
159
|
+
this.onInterrupted = function (cb) {
|
|
155
160
|
return pubsub.on('interrupted', cb)
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
var conn
|
|
159
|
-
this.subscribe = function(events) {
|
|
164
|
+
this.subscribe = function (events) {
|
|
160
165
|
if (stop) return Promise.resolve({})
|
|
161
166
|
if (typeof events === 'string') events = [events]
|
|
162
167
|
if (!Array.isArray(events)) return Promise.resolve({error: 'param should be an array or string'})
|
|
@@ -169,7 +174,7 @@ function Realtime(apiUrls, credential, callAPI) {
|
|
|
169
174
|
if (!topics[topic]) all.push(conn.subscribe(topic))
|
|
170
175
|
}
|
|
171
176
|
if (all.length === 0) return Promise.resolve({})
|
|
172
|
-
return Promise.all(all).then(function(errs) {
|
|
177
|
+
return Promise.all(all).then(function (errs) {
|
|
173
178
|
for (var i = 0; i < errs.length; i++) if (errs[i]) return {error: errs[i]}
|
|
174
179
|
for (var j = 0; j < events.length; j++) topics[events[j]] = true
|
|
175
180
|
return {}
|
|
@@ -178,7 +183,7 @@ function Realtime(apiUrls, credential, callAPI) {
|
|
|
178
183
|
|
|
179
184
|
// reconnect make sure there is alway a Conn running in the background
|
|
180
185
|
// if the Conn is dead, it recreate a new one
|
|
181
|
-
var reconnect = function() {
|
|
186
|
+
var reconnect = function () {
|
|
182
187
|
if (stop) return
|
|
183
188
|
// reset subscribed topic
|
|
184
189
|
var allTopics = Object.keys(topics)
|
|
@@ -188,12 +193,12 @@ function Realtime(apiUrls, credential, callAPI) {
|
|
|
188
193
|
conn = new Conn(
|
|
189
194
|
randomUrl,
|
|
190
195
|
credential,
|
|
191
|
-
function(code, body, status) {
|
|
196
|
+
function (code, body, status) {
|
|
192
197
|
if (stop) return
|
|
193
198
|
pubsub.emit('interrupted', code, body, status)
|
|
194
199
|
setTimeout(reconnect, 2000) // reconnect and resubscribe after 2 sec
|
|
195
200
|
},
|
|
196
|
-
function(topics) {
|
|
201
|
+
function (topics) {
|
|
197
202
|
if (stop) return
|
|
198
203
|
for (var i = 0; i < topics.length; i++) pubsub.emit('event', topics[i])
|
|
199
204
|
},
|
|
@@ -207,7 +212,7 @@ function Realtime(apiUrls, credential, callAPI) {
|
|
|
207
212
|
reconnect()
|
|
208
213
|
}
|
|
209
214
|
|
|
210
|
-
var dofetch = function(method, url, body, cb) {
|
|
215
|
+
var dofetch = function (method, url, body, cb) {
|
|
211
216
|
let headers = {}
|
|
212
217
|
if (body) headers['content-type'] = 'text/plain'
|
|
213
218
|
fetch(url, {
|
|
@@ -219,16 +224,16 @@ var dofetch = function(method, url, body, cb) {
|
|
|
219
224
|
.catch((err) => cb && cb(err, -1))
|
|
220
225
|
}
|
|
221
226
|
// xhrsend sends an HTTP request
|
|
222
|
-
var xhrsend = function(method, url, body, cb) {
|
|
227
|
+
var xhrsend = function (method, url, body, cb) {
|
|
223
228
|
if (typeof XMLHttpRequest === 'undefined') return dofetch(method, url, body, cb)
|
|
224
229
|
var request = new XMLHttpRequest()
|
|
225
|
-
request.onreadystatechange = function(e) {
|
|
230
|
+
request.onreadystatechange = function (e) {
|
|
226
231
|
if (request.readyState !== 4) return
|
|
227
232
|
cb && cb(request.responseText, request.status)
|
|
228
233
|
cb = undefined // dont call cb anymore
|
|
229
234
|
}
|
|
230
235
|
|
|
231
|
-
request.onerror = function() {
|
|
236
|
+
request.onerror = function () {
|
|
232
237
|
cb && cb(request.responseText, -1) // network error
|
|
233
238
|
cb = undefined // dont call cb anymore
|
|
234
239
|
}
|
|
@@ -256,7 +261,7 @@ function Pubsub() {
|
|
|
256
261
|
|
|
257
262
|
// emit notifies any callback functions that previously
|
|
258
263
|
// subscribed to the topic (by the on function)
|
|
259
|
-
this.emit = function(topic) {
|
|
264
|
+
this.emit = function (topic) {
|
|
260
265
|
// skip the first item in arguments, which is the topic
|
|
261
266
|
var args = []
|
|
262
267
|
for (var i = 1; i < arguments.length; i++) args.push(arguments[i])
|
|
@@ -269,7 +274,7 @@ function Pubsub() {
|
|
|
269
274
|
// register a callback function for a topic
|
|
270
275
|
// when something is sent to the topic (by the emit function),
|
|
271
276
|
// the callback function will be called
|
|
272
|
-
this.on = function(topic, cb) {
|
|
277
|
+
this.on = function (topic, cb) {
|
|
273
278
|
if (!listeners[topic]) listeners[topic] = []
|
|
274
279
|
listeners[topic].push(cb)
|
|
275
280
|
}
|