@knowlearning/agents 0.4.0 → 0.4.2
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/agents/browser/root.js +3 -1
- package/agents/generic.js +7 -6
- package/package.json +1 -1
package/agents/browser/root.js
CHANGED
|
@@ -8,13 +8,15 @@ const REMOTE_HOST = 'api.knowlearning.systems'
|
|
|
8
8
|
|
|
9
9
|
function isLocal() { return localStorage.getItem('api') === 'local' }
|
|
10
10
|
|
|
11
|
+
const getIdTokenPromise = new Promise(r => onAuth(({ getIdToken }) => r(getIdToken)))
|
|
12
|
+
|
|
11
13
|
export default () => {
|
|
12
14
|
const { host, protocol } = window.location
|
|
13
15
|
|
|
14
16
|
const agent = GenericAgent({
|
|
15
17
|
host: isLocal() ? DEVELOPMENT_HOST : REMOTE_HOST,
|
|
16
18
|
protocol: protocol === 'https:' ? 'wss' : 'ws',
|
|
17
|
-
token: () =>
|
|
19
|
+
token: () => getIdTokenPromise.then(f => f(true)),
|
|
18
20
|
WebSocket,
|
|
19
21
|
uuid,
|
|
20
22
|
fetch,
|
package/agents/generic.js
CHANGED
|
@@ -88,23 +88,25 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
88
88
|
)
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
let restarting = false
|
|
91
92
|
async function restartConnection() {
|
|
92
|
-
if (
|
|
93
|
+
if (restarting) return
|
|
94
|
+
|
|
95
|
+
restarting = true
|
|
93
96
|
authed = false
|
|
94
97
|
ws.onmessage = () => {} // needs to be a no-op since a closing ws can still get messages
|
|
95
98
|
if (!disconnected) {
|
|
96
99
|
await new Promise(r => setTimeout(r, Math.min(1000, failedConnections * 100)))
|
|
97
100
|
failedConnections += 1
|
|
98
101
|
initWS() // TODO: don't do this if we are purposefully unloading...
|
|
102
|
+
restarting = false
|
|
99
103
|
}
|
|
100
104
|
}
|
|
101
105
|
|
|
102
106
|
function initWS() {
|
|
103
107
|
ws = new WebSocket(`${protocol}://${host}`)
|
|
104
108
|
|
|
105
|
-
let opened = false
|
|
106
109
|
ws.onopen = async () => {
|
|
107
|
-
opened = true
|
|
108
110
|
log('AUTHORIZING NEWLY OPENED WS FOR SESSION:', session)
|
|
109
111
|
failedConnections = 0
|
|
110
112
|
ws.send(JSON.stringify({ token: await token(), session }))
|
|
@@ -177,12 +179,11 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
177
179
|
}
|
|
178
180
|
|
|
179
181
|
ws.onerror = async error => {
|
|
180
|
-
log('WS CONNECTION ERROR', error.message
|
|
181
|
-
if (!opened) restartConnection() // onclose won't trigger if never opened
|
|
182
|
+
log('WS CONNECTION ERROR', error.message)
|
|
182
183
|
}
|
|
183
184
|
|
|
184
185
|
ws.onclose = async error => {
|
|
185
|
-
log('WS CLOSURE', error.message
|
|
186
|
+
log('WS CLOSURE', error.message)
|
|
186
187
|
restartConnection()
|
|
187
188
|
}
|
|
188
189
|
|