@knowlearning/agents 0.5.6 → 0.5.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.
- package/package.json +1 -1
- package/persistence/vuex.js +15 -4
package/package.json
CHANGED
package/persistence/vuex.js
CHANGED
|
@@ -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 (
|
|
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 =>
|
|
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 (
|
|
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={}) {
|