-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.ts
More file actions
290 lines (236 loc) · 6.97 KB
/
Copy pathstate.ts
File metadata and controls
290 lines (236 loc) · 6.97 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { convertSimUnit, type SimUnit } from './units'
export type SimStateValue = number | string | boolean
export type SimStateValueType = 'number' | 'string' | 'boolean'
export type SimStateSource = 'default' | 'subsystem' | 'loaded' | 'runtime'
export const SIM_STATE_SOURCE_PRIORITY: Record<SimStateSource, number> = {
default: 0,
subsystem: 10,
loaded: 20,
runtime: 30,
}
export interface SimStateDefinition {
readonly key: string
readonly unit?: SimUnit
readonly valueType?: SimStateValueType
readonly defaultValue?: SimStateValue
readonly description?: string
}
export interface SimStateWriteOptions {
readonly source: SimStateSource
readonly unit?: SimUnit
readonly metadata?: Readonly<Record<string, unknown>>
}
export interface SimStateEntry {
readonly key: string
readonly value: SimStateValue
readonly unit?: SimUnit
readonly source: SimStateSource
readonly priority: number
readonly revision: number
readonly metadata?: Readonly<Record<string, unknown>>
}
export interface SimStateChangeEvent {
readonly key: string
readonly previous: SimStateEntry | undefined
readonly current: SimStateEntry | undefined
}
export type SimStateListener = (event: SimStateChangeEvent) => void
export class SimStateStore {
private readonly definitions = new Map<string, SimStateDefinition>()
private readonly valuesBySource = new Map<
string,
Map<SimStateSource, SimStateEntry>
>()
private readonly listeners = new Set<SimStateListener>()
private revision = 0
define(definition: SimStateDefinition): void {
const existing = this.definitions.get(definition.key)
this.definitions.set(definition.key, {
...existing,
...definition,
})
if ('defaultValue' in definition) {
this.set(definition.key, definition.defaultValue as SimStateValue, {
source: 'default',
unit: definition.unit,
})
}
}
defineMany(definitions: readonly SimStateDefinition[]): void {
for (const definition of definitions) {
this.define(definition)
}
}
set(key: string, value: SimStateValue, options: SimStateWriteOptions): void {
const previous = this.getEntry(key)
const definition = this.definitions.get(key)
const storedUnit = definition?.unit ?? options.unit
const storedValue = coerceStateValue(value, definition, options.unit)
const sourceValues = getOrCreateSourceValues(this.valuesBySource, key)
this.revision += 1
sourceValues.set(options.source, {
key,
value: storedValue,
unit: storedUnit,
source: options.source,
priority: SIM_STATE_SOURCE_PRIORITY[options.source],
revision: this.revision,
metadata: options.metadata,
})
this.emitIfChanged(key, previous, this.getEntry(key))
}
clearSource(key: string, source: SimStateSource): void {
const previous = this.getEntry(key)
const sourceValues = this.valuesBySource.get(key)
if (sourceValues == null || !sourceValues.delete(source)) {
return
}
if (sourceValues.size === 0) {
this.valuesBySource.delete(key)
}
this.emitIfChanged(key, previous, this.getEntry(key))
}
clearSourceValues(source: SimStateSource): void {
const keys = [...this.valuesBySource.keys()]
for (const key of keys) {
this.clearSource(key, source)
}
}
read<T extends SimStateValue = SimStateValue>(key: string): T | undefined {
return this.getEntry(key)?.value as T | undefined
}
readNumber(
key: string,
options: { readonly unit?: SimUnit; readonly fallback?: number } = {}
): number | undefined {
const entry = this.getEntry(key)
if (entry == null) {
return options.fallback
}
if (typeof entry.value === 'boolean') {
return entry.value ? 1 : 0
}
if (typeof entry.value !== 'number') {
return options.fallback
}
if (options.unit != null && entry.unit != null) {
return convertSimUnit(entry.value, entry.unit, options.unit)
}
return entry.value
}
readBoolean(
key: string,
options: { readonly fallback?: boolean } = {}
): boolean | undefined {
const value = this.read(key)
if (typeof value === 'boolean') {
return value
}
if (typeof value === 'number') {
return value !== 0
}
return options.fallback
}
getEntry(key: string): SimStateEntry | undefined {
const sourceValues = this.valuesBySource.get(key)
if (sourceValues == null) {
return undefined
}
let selected: SimStateEntry | undefined
for (const entry of sourceValues.values()) {
if (
selected == null ||
entry.priority > selected.priority ||
(entry.priority === selected.priority && entry.revision > selected.revision)
) {
selected = entry
}
}
return selected
}
getDefinition(key: string): SimStateDefinition | undefined {
return this.definitions.get(key)
}
listDefinitions(): readonly SimStateDefinition[] {
return [...this.definitions.values()]
}
subscribe(listener: SimStateListener): () => void {
this.listeners.add(listener)
return () => {
this.listeners.delete(listener)
}
}
private emitIfChanged(
key: string,
previous: SimStateEntry | undefined,
current: SimStateEntry | undefined
): void {
if (stateEntriesAreEquivalent(previous, current)) {
return
}
for (const listener of this.listeners) {
listener({ key, previous, current })
}
}
}
function getOrCreateSourceValues(
valuesBySource: Map<string, Map<SimStateSource, SimStateEntry>>,
key: string
): Map<SimStateSource, SimStateEntry> {
const existing = valuesBySource.get(key)
if (existing != null) {
return existing
}
const created = new Map<SimStateSource, SimStateEntry>()
valuesBySource.set(key, created)
return created
}
function coerceStateValue(
value: SimStateValue,
definition: SimStateDefinition | undefined,
incomingUnit: SimUnit | undefined
): SimStateValue {
const expectedType = definition?.valueType ?? inferValueType(definition, value)
if (typeof value !== expectedType) {
throw new TypeError(
`State value for ${definition?.key ?? 'unregistered key'} must be ${expectedType}`
)
}
if (
typeof value === 'number' &&
definition?.unit != null &&
incomingUnit != null
) {
return convertSimUnit(value, incomingUnit, definition.unit)
}
return value
}
function inferValueType(
definition: SimStateDefinition | undefined,
value: SimStateValue
): SimStateValueType {
if (definition?.unit === 'boolean') {
return 'boolean'
}
if (definition?.unit != null) {
return 'number'
}
if (typeof value === 'number') {
return 'number'
}
if (typeof value === 'boolean') {
return 'boolean'
}
return 'string'
}
function stateEntriesAreEquivalent(
left: SimStateEntry | undefined,
right: SimStateEntry | undefined
): boolean {
return (
left?.key === right?.key &&
left?.value === right?.value &&
left?.unit === right?.unit &&
left?.source === right?.source
)
}