@knowlearning/agents 0.5.6 → 0.5.8

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/generic.js CHANGED
@@ -296,6 +296,33 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
296
296
  return promise
297
297
  }
298
298
 
299
+ function watch(id, fn) {
300
+ let initialSent = false
301
+ const queue = []
302
+ function cb(update) {
303
+ if (initialSent) fn(update)
304
+ else queue.push(update)
305
+ }
306
+
307
+ const statePromise = state(id)
308
+ if (!watchers[id]) watchers[id] = []
309
+ watchers[id].push(cb)
310
+
311
+ metadata(id)
312
+ .then(async ({ ii }) => {
313
+ fn({
314
+ scope: id,
315
+ state: await statePromise,
316
+ patch: null,
317
+ ii
318
+ })
319
+ initialSent = true
320
+ queue.forEach(fn)
321
+ })
322
+
323
+ return () => removeWatcher(id, cb)
324
+ }
325
+
299
326
  // TODO: if no data, set up streaming upload
300
327
  async function upload(name, type, data, id=uuid()) {
301
328
  // TODO: include data size info...
@@ -424,10 +451,12 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
424
451
  }
425
452
 
426
453
  return {
454
+ uuid,
427
455
  environment,
428
456
  login,
429
457
  logout,
430
458
  state,
459
+ watch,
431
460
  upload,
432
461
  download,
433
462
  interact,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knowlearning/agents",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "description": "API for embedding applications in KnowLearning systems.",
5
5
  "main": "node.js",
6
6
  "browser": "browser.js",
@@ -4,6 +4,11 @@ import MutableProxy from './json.js'
4
4
 
5
5
  const copy = x => JSON.parse(JSON.stringify(x))
6
6
 
7
+ async function scopeIsUninitialized(scope) {
8
+ const { ii } = await Agent.metadata(scope)
9
+ return ii === 0
10
+ }
11
+
7
12
  export default async function (storeDefinition) {
8
13
  // Set up persistance
9
14
  const { scope } = await Agent.environment()
@@ -15,7 +20,7 @@ export default async function (storeDefinition) {
15
20
  const originalState = s instanceof Function ? s() : s
16
21
  const handlePatch = patch => Agent.interact(scope, patch)
17
22
 
18
- if (Object.entries(state).length === 0) {
23
+ if (await scopeIsUninitialized(scope)) {
19
24
  state = copy(originalState)
20
25
  handlePatch([{ op: 'add', path: [], value: state }])
21
26
  }
@@ -58,14 +63,20 @@ async function attachModuleState(state, module, scopedPaths, path='') {
58
63
  // if our path is in scoped paths, return new Mutable proxy attached to scope
59
64
  const scope = scopedPaths[path]
60
65
  if (scope) {
61
- const handlePatch = patch => Agent.interact(scope, patch)
66
+ const handlePatch = patch => {
67
+ patch.forEach(({ path }) => path.unshift('active'))
68
+ return Agent.interact(scope, patch)
69
+ }
62
70
  const initState = await Agent.state(scope)
63
71
  const ephemeralPaths = descendantPaths(path, scopedPaths)
64
72
  state = MutableProxy(copy(initState), handlePatch, ephemeralPaths)
65
- if (Object.keys(initState).length === 0) Object.assign(state, module.state())
73
+ if (await scopeIsUninitialized(scope)) {
74
+ Object.assign(state, module.state())
75
+ }
76
+ return { ...module, state: () => state }
66
77
  }
78
+ else return module
67
79
 
68
- return state ? { ...module, state: () => state } : module
69
80
  }
70
81
 
71
82
  function getScopedPaths(module, path="", paths={}) {