-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsState.ts
More file actions
300 lines (268 loc) · 8.19 KB
/
Copy pathcsState.ts
File metadata and controls
300 lines (268 loc) · 8.19 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
291
292
293
294
295
296
297
298
299
300
import log from "loglevel";
import { MacroMap } from "../types/macros";
import { mergeDType, DType } from "../types/dtypes";
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { SubscriptionType } from "../connection";
export const initialCsState: CsState = {
valueCache: {},
globalMacros: { SUFFIX: "1" },
effectivePvNameMap: {},
subscriptions: {},
deviceCache: {},
pvwsSettings: {}
};
export interface PvState {
value?: DType;
connected: boolean;
readonly: boolean;
}
export type PvDatum = PvState & {
effectivePvName: string;
};
export type PvDataCollection = {
pvData: PvDatum[];
};
export interface FullPvState extends PvState {
initializingPvName: string;
}
export interface ValueCache {
[pvName: string]: FullPvState;
}
export interface Subscriptions {
[pvName: string]: string[];
}
export interface DeviceCache {
[deviceName: string]: DType;
}
export interface PvwsSettings {
pvwsHost?: string;
}
export interface PvArrayResults {
[pvName: string]: [PvState, string];
}
/* The shape of the store for the entire application. */
export interface CsState {
valueCache: ValueCache;
effectivePvNameMap: { [pvName: string]: string };
globalMacros: MacroMap;
subscriptions: Subscriptions;
deviceCache: DeviceCache;
pvwsSettings: PvwsSettings;
}
type ValueChangedActionType = PayloadAction<{ pvName: string; value: DType }>;
/* Given a new object that is a shallow copy of the original
valueCache, update with contents of a ValueChanged action. */
function updateValueCache(
valueCache: ValueCache,
action: ValueChangedActionType
): void {
const { pvName, value } = action.payload;
const mergedValue = mergeDType(valueCache[pvName]?.value, value);
const existing = valueCache[pvName] ?? {
connected: false,
readonly: true,
initializingPvName: pvName
};
valueCache[pvName] = { ...existing, value: mergedValue };
}
const csSlice = createSlice({
name: "cs",
initialState: initialCsState,
reducers: {
setPvwsSettings(state, action: PayloadAction<PvwsSettings>) {
log.debug(action);
state.pvwsSettings.pvwsHost = action.payload.pvwsHost;
},
valueChanged(state, action: ValueChangedActionType) {
log.debug(action);
updateValueCache(state.valueCache, action);
},
valuesChanged(state, action: PayloadAction<Array<ValueChangedActionType>>) {
log.debug(action);
for (const valueAction of action.payload) {
updateValueCache(state.valueCache, valueAction);
}
},
connectionClosed(
state,
action: PayloadAction<{
pvName: string;
}>
) {
log.debug(action);
const { pvName } = action.payload;
if (pvName in state.valueCache) {
delete state.valueCache[pvName];
}
},
connectionChanged(
state,
action: PayloadAction<{
pvName: string;
value: { isConnected: boolean; isReadonly?: boolean };
}>
) {
log.debug(action);
const { pvName, value } = action.payload;
const existing = state.valueCache[pvName] ?? {
connected: false,
readonly: true,
initializingPvName: pvName
};
state.valueCache[pvName] = {
...existing,
connected: value.isConnected,
readonly: value.isReadonly ?? existing.readonly
};
},
subscribe(
state,
action: PayloadAction<{
componentId: string;
pvName: string;
effectivePvName?: string;
type: SubscriptionType;
}>
) {
// the connection middleware intercepts this action and injects the effectivePvName before forwarding it
log.debug(action);
const { componentId, pvName } = action.payload;
const effectivePvName = action.payload?.effectivePvName || pvName;
const list = state.subscriptions[effectivePvName] ?? [];
if (!list.includes(componentId)) {
list.push(componentId);
state.subscriptions[effectivePvName] = list;
}
if (pvName !== effectivePvName) {
state.effectivePvNameMap[pvName] = effectivePvName;
}
},
unsubscribe(
state,
action: PayloadAction<{ componentId: string; pvName: string }>
) {
log.debug(action);
const { componentId, pvName } = action.payload;
const effectivePvName = state.effectivePvNameMap[pvName] ?? pvName;
const current = state.subscriptions[effectivePvName] ?? [];
const next = current.filter(id => id !== componentId);
if (next.length === 0) {
delete state.subscriptions[effectivePvName];
Object.keys(state.effectivePvNameMap).forEach((key: string) => {
if (state.effectivePvNameMap[key] === effectivePvName) {
delete state.effectivePvNameMap[key];
}
});
} else {
state.subscriptions[effectivePvName] = next;
}
},
writePv(_state, _action: PayloadAction<{ pvName: string; value: DType }>) {
// intentionally empty — handled by listener middleware
},
deviceQueried(
state,
action: PayloadAction<{ device: string; value: DType }>
) {
log.debug(action);
const { device, value } = action.payload;
state.deviceCache[device] = value;
},
queryDevice(_state, _action: PayloadAction<{ device: string }>) {
// intentionally empty — handled by listener middleware
}
},
selectors: {
selectDeviceCache: state => state.deviceCache,
selectEffectivePvNameMap: state => state.effectivePvNameMap,
selectValueCache: state => state.valueCache,
selectGlobalMacros: state => state.globalMacros,
selectSubscriptions: state => state.subscriptions
}
});
export const {
setPvwsSettings,
valueChanged,
valuesChanged,
connectionClosed,
connectionChanged,
subscribe,
unsubscribe,
writePv,
deviceQueried,
queryDevice
} = csSlice.actions;
export default csSlice.reducer;
export const {
selectDeviceCache,
selectEffectivePvNameMap,
selectValueCache,
selectGlobalMacros,
selectSubscriptions
} = csSlice.selectors;
export const selectEffectivePvName = createSelector(
[selectEffectivePvNameMap, (_state, pvName: string) => pvName],
(effectivePvNameMap, pvName) => effectivePvNameMap[pvName]
);
export const selectDevice = createSelector(
[selectDeviceCache, (_state, deviceId: string) => deviceId],
(deviceCache, deviceId) => deviceCache[deviceId]
);
export const selectPvStates = createSelector(
[
selectEffectivePvNameMap,
selectValueCache,
(_state, pvNames: string[]) => pvNames
],
(pvNameMap, valueCache, pvNames) => {
const results: {
[pvName: string]: [PvState, string];
} = {};
for (const pvName of pvNames) {
const effectivePvName = pvNameMap[pvName] || pvName;
results[pvName] = [valueCache[effectivePvName], effectivePvName];
}
return results;
}
);
/* Used for preventing re-rendering if the results are equivalent.
Note that if the state for a particular PV hasn't changed, we will
get back the same object as last time so we are safe to compare them.
We need to be careful that we don't have a situation where we get back
the same object with different properties and compare it as equal when
in fact it has changed.
*/
export const pvStateComparator = (
before: PvArrayResults,
after: PvArrayResults
): boolean => {
if (Object.keys(before).length !== Object.keys(after).length) {
return false;
}
for (const [pvName, [beforeVal, beforeEffPvName]] of Object.entries(before)) {
// If the PV name for a widget has changed the previous results may
// not resemble the new results at all.
if (!after.hasOwnProperty(pvName)) {
return false;
}
const [afterVal, afterEffPvName] = after[pvName];
// If the PV state hasn't changed in the store, we will receive the same
// object when selecting that PV state.
if (beforeVal !== afterVal || beforeEffPvName !== afterEffPvName) {
return false;
}
}
return true;
};
export const deviceComparator = (before: DType, after: DType): boolean => {
if (!before || !after) {
return false;
}
if (Object.keys(before).length !== Object.keys(after).length) {
return false;
}
if (before.value.stringValue !== after.value.stringValue) {
return false;
}
return true;
};