-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraversals.js
63 lines (61 loc) · 1.84 KB
/
traversals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import F from 'futil'
import _ from 'lodash/fp.js'
import { Tree, postWalkBranches } from './util/tree.js'
export default (extend) => {
let markForUpdate = (x) => {
if (x.paused) extend(x, { missedUpdate: true })
else if (!x.markedForUpdate) {
let updatingDeferred = F.defer()
extend(x, {
markedForUpdate: true,
updatingPromise: updatingDeferred.promise,
updatingDeferred,
isStale: true,
})
}
return x
}
return {
markForUpdate,
clearUpdate: (node) => extend(node, { updating: false, isStale: false }),
syncMarkedForUpdate(tree) {
// This method is to sync markedForUpdate/isStale
// in theory this could be a getter/setter or writeableComputed
// syncing manually avoids taking on a dependency like mobx in the server
// This walk/push will be replaced in the future by Tree.toArrayBy({ post: /* ... */})
let updatedNodes = []
Tree.walk(
() => {},
(node) => {
if (_.some('markedForUpdate', node.children))
updatedNodes.push(markForUpdate(node))
else if (node.children)
extend(node, {
markedForUpdate: false,
...(node.updating || { isStale: false }),
})
}
)(tree)
return updatedNodes
},
syncComputedGroupFields: (fields, tree) =>
postWalkBranches((node) =>
_.each(
(field) => extend(node, { [field]: _.some(field, node.children) }),
fields
)
)(tree),
markLastUpdate: (time) =>
Tree.walk((node) => {
if (node.markedForUpdate) extend(node, { lastUpdateTime: time })
}),
prepForUpdate: Tree.walk((node) => {
if (node.markedForUpdate) {
extend(node, {
updating: true,
markedForUpdate: false,
})
}
}),
}
}