@knowlearning/agents 0.4.5 → 0.4.7

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.
Files changed (2) hide show
  1. package/agents/generic.js +14 -16
  2. package/package.json +1 -1
package/agents/generic.js CHANGED
@@ -14,10 +14,6 @@ function sanitizeJSONPatchPathSegment(s) {
14
14
  else return s
15
15
  }
16
16
 
17
- function copy(value) {
18
- return JSON.parse(JSON.stringify(value))
19
- }
20
-
21
17
  export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fetch, applyPatch, login, logout, reboot }) {
22
18
  let ws
23
19
  let user
@@ -40,11 +36,10 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
40
36
  let lastSentSI = -1
41
37
  let lastHeartbeat
42
38
 
43
- // TODO: probably an use "mutate" function from below
44
- const subscriptions = new MutableProxy({}, patch => queueMessage({scope: 'subscriptions', patch}))
45
- const uploads = new MutableProxy({}, patch => queueMessage({scope: 'uploads', patch}))
46
- const downloads = new MutableProxy({}, patch => queueMessage({scope: 'downloads', patch}))
47
- const patches = new MutableProxy({}, patch => queueMessage({scope: 'patches', patch}))
39
+ const subscriptions = mutate('subscriptions', false)
40
+ const uploads = mutate('uploads', false)
41
+ const downloads = mutate('downloads', false)
42
+ const patches = mutate('patches', false)
48
43
 
49
44
  log('INITIALIZING AGENT CONNECTION')
50
45
  initWS()
@@ -219,10 +214,14 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
219
214
  keyToSubscriptionId[k] = id
220
215
  watchers[k] = []
221
216
  states[k] = new Promise(async resolve => {
222
- subscriptions[id] = { scope, ii: null, session }
223
217
 
224
- if (d !== domain) subscriptions[id].domain = d
225
- if (u !== user) subscriptions[id].user = u
218
+ subscriptions[id] = {
219
+ session,
220
+ domain: d,
221
+ user: u,
222
+ scope,
223
+ ii: null
224
+ }
226
225
 
227
226
  const { ii, state } = await lastMessageResponse()
228
227
  subscriptions[id].ii = ii
@@ -344,10 +343,9 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
344
343
  return interact('claims', [{ op: 'add', path: [domain], value: null }])
345
344
  }
346
345
 
347
- async function mutate(scope, initialize=true) {
348
- // TODO: probably can remove redundant copy
349
- const initial = initialize ? copy(await state(scope) || {}) : {}
350
- return new MutableProxy(initial, patch => interact(scope, patch))
346
+ function mutate(scope, initialize=true) {
347
+ const mp = s => new MutableProxy(s || {}, patch => interact(scope, patch))
348
+ return initialize ? state.then(mp) : mp({})
351
349
  }
352
350
 
353
351
  function reset(scope) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",