-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathindex.ts
More file actions
166 lines (143 loc) · 5.78 KB
/
index.ts
File metadata and controls
166 lines (143 loc) · 5.78 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {throttle} from '@solid-primitives/scheduled'
import {ObjectType, getSdtId} from '../main/id.ts'
import * as roots from '../main/roots.ts'
import {type Mapped, type NodeID, type Solid, DEFAULT_WALKER_MODE, DevtoolsMainView, NodeType, TreeWalkerMode} from '../main/types.ts'
import {isDisposed, markOwnerType} from '../main/utils.ts'
import * as walker from './walker.ts'
export type StructureUpdates = {
/** Partial means that the updates are based on the previous structure state */
partial: boolean
/** Removed roots */
removed: NodeID[]
/** Record: `rootId` -- Record of updated nodes by `nodeId` */
updated: Partial<Record<NodeID, Partial<Record<NodeID, Mapped.Owner>>>>
}
/**
* Finds the closest owner of a given owner that is included in the tree walker.
*/
// TODO unit test this
function getClosestIncludedOwner(owner: Solid.Owner, mode: TreeWalkerMode): Solid.Owner | null {
// 1. make sure the tree is not disposed
// if it is, find the non-disposed owner and use as the owner
let closest: Solid.Owner | null = null
let current: Solid.Owner | null = owner
do {
if (isDisposed(current)) closest = current.owner
current = current.owner
} while (current)
owner = closest ?? owner
// 2. find the closest owner that is included in the tree walker
if (mode === TreeWalkerMode.Owners) return owner
let root: Solid.Owner | null = null
do {
const type = markOwnerType(owner)
if (type === NodeType.Component || type === NodeType.Context) return owner
if (type === NodeType.Root) root = owner
owner = owner.owner!
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
} while (owner)
return root
}
export function createStructure<TEl extends object>(props: {
onStructureUpdate: (updates: StructureUpdates) => void
onNodeUpdate: (nodeId: NodeID) => void
enabled: () => boolean
component_registry: walker.ComponentRegistry<TEl>,
}) {
let treeWalkerMode: TreeWalkerMode = DEFAULT_WALKER_MODE
const updateQueue = new Set<Solid.Owner>()
/** root ids correcponding to owners in the update queue */
const ownerRoots = new Map<Solid.Owner, NodeID>()
const removedRoots = new Set<NodeID>()
let shouldUpdateAllRoots = true
const onComputationUpdate: walker.ComputationUpdateHandler = (
root_id, owner, changed_structure,
) => {
// separate the callback from the computation
queueMicrotask(() => {
if (!props.enabled()) return
if (changed_structure) {
let owner_to_update = getClosestIncludedOwner(owner, treeWalkerMode) ?? owner
updateOwner(owner_to_update, root_id)
}
let id = getSdtId(owner, ObjectType.Owner)
props.onNodeUpdate(id)
})
}
function forceFlushRootUpdateQueue(): void {
if (props.enabled()) {
let partial = !shouldUpdateAllRoots
shouldUpdateAllRoots = false
let updated: StructureUpdates['updated'] = {}
let owners: Iterable<Solid.Owner>
let getRootId: (owner: Solid.Owner) => NodeID
if (partial) {
owners = updateQueue
getRootId = owner => ownerRoots.get(owner)!
} else {
owners = roots.getCurrentRoots()
getRootId = owner => getSdtId(owner, ObjectType.Owner)
}
for (let owner of owners) {
let root_id = getRootId(owner)
let tree = walker.walkSolidTree(owner, {
rootId: root_id,
mode: treeWalkerMode,
onUpdate: onComputationUpdate,
eli: props.component_registry.eli,
registry: props.component_registry,
})
let map = updated[root_id]
if (map != null) map[tree.id] = tree
else updated[root_id] = {[tree.id]: tree}
}
props.onStructureUpdate({partial, updated, removed: [...removedRoots]})
}
updateQueue.clear()
flushRootUpdateQueue.clear()
removedRoots.clear()
ownerRoots.clear()
}
const flushRootUpdateQueue = throttle(forceFlushRootUpdateQueue, 250)
function updateOwner(node: Solid.Owner, topRootId: NodeID): void {
updateQueue.add(node)
ownerRoots.set(node, topRootId)
flushRootUpdateQueue()
}
roots.setOnOwnerNeedsUpdate((node: Solid.Owner, topRootId: NodeID) => {
const closestIncludedOwner = getClosestIncludedOwner(node, treeWalkerMode)
closestIncludedOwner && updateOwner(closestIncludedOwner, topRootId)
})
roots.setOnRootRemoved((rootId: NodeID) => {
removedRoots.add(rootId)
flushRootUpdateQueue()
})
function updateAllRoots(): void {
shouldUpdateAllRoots = true
flushRootUpdateQueue()
}
function forceUpdateAllRoots(): void {
shouldUpdateAllRoots = true
queueMicrotask(forceFlushRootUpdateQueue)
}
function setTreeWalkerMode(mode: TreeWalkerMode): void {
treeWalkerMode = mode
updateAllRoots()
walker.clearComponentRegistry(props.component_registry)
}
return {
updateAllRoots,
forceUpdateAllRoots,
setTreeWalkerMode,
resetTreeWalkerMode: () => setTreeWalkerMode(DEFAULT_WALKER_MODE),
getClosestIncludedOwner(owner: Solid.Owner) {
return getClosestIncludedOwner(owner, treeWalkerMode)
},
onViewChange(view: DevtoolsMainView) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (view === DevtoolsMainView.Structure) {
updateAllRoots()
}
},
}
}