@knowlearning/agents 0.9.90 → 0.9.91
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
CHANGED
|
@@ -4,18 +4,23 @@ import { getToken, login, logout } from './auth.js'
|
|
|
4
4
|
import GenericAgent from '../generic/index.js'
|
|
5
5
|
|
|
6
6
|
const DEVELOPMENT_HOST = 'localhost:32001'
|
|
7
|
-
const REMOTE_HOST = 'api.knowlearning.systems'
|
|
7
|
+
const REMOTE_HOST = localStorage.getItem('mode') === 'staging' ? 'api.staging.knowlearning.systems' : 'api.knowlearning.systems'
|
|
8
8
|
|
|
9
9
|
function isLocal() { return localStorage.getItem('api') === 'local' }
|
|
10
10
|
|
|
11
|
+
const API_HOST = isLocal() ? DEVELOPMENT_HOST : REMOTE_HOST
|
|
12
|
+
|
|
11
13
|
export default options => {
|
|
12
14
|
const { host, protocol } = window.location
|
|
13
15
|
|
|
14
16
|
const Connection = function () {
|
|
15
|
-
const ws = new WebSocket(`${protocol === 'https:' ? 'wss' : 'ws'}://${
|
|
17
|
+
const ws = new WebSocket(`${protocol === 'https:' ? 'wss' : 'ws'}://${API_HOST}`)
|
|
16
18
|
|
|
17
19
|
this.send = message => ws.send(JSON.stringify(message))
|
|
18
|
-
this.close =
|
|
20
|
+
this.close = info => {
|
|
21
|
+
this.send({ type: 'close', info })
|
|
22
|
+
ws.close()
|
|
23
|
+
}
|
|
19
24
|
|
|
20
25
|
ws.onopen = () => this.onopen()
|
|
21
26
|
ws.onmessage = ({ data }) => this.onmessage(data.length === 0 ? null : JSON.parse(data))
|
|
@@ -37,15 +42,12 @@ export default options => {
|
|
|
37
42
|
})
|
|
38
43
|
|
|
39
44
|
agent.local = () => {
|
|
40
|
-
if (isLocal()) return
|
|
41
|
-
|
|
42
45
|
localStorage.setItem('api', 'local')
|
|
43
46
|
location.reload()
|
|
44
47
|
}
|
|
45
|
-
agent.remote = () => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
localStorage.removeItem('api')
|
|
48
|
+
agent.remote = (mode='production') => {
|
|
49
|
+
localStorage.setItem('api', 'remote')
|
|
50
|
+
localStorage.setItem('mode', mode)
|
|
49
51
|
location.reload()
|
|
50
52
|
}
|
|
51
53
|
agent.close = () => {
|
|
@@ -236,7 +236,7 @@ export default function messageQueue({ token, Connection, watchers, states, appl
|
|
|
236
236
|
function disconnect() {
|
|
237
237
|
log('DISCONNECTED AGENT!!!!!!!!!!!!!!!')
|
|
238
238
|
disconnected = true
|
|
239
|
-
connection.close()
|
|
239
|
+
connection.close({ keepalive: true })
|
|
240
240
|
}
|
|
241
241
|
|
|
242
242
|
function reconnect() {
|