-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleComponentChange.ts
More file actions
90 lines (88 loc) · 2.91 KB
/
handleComponentChange.ts
File metadata and controls
90 lines (88 loc) · 2.91 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
import { store } from "@/store";
import { type ContribPoint } from "@/types/model/extension";
import { type CallbackRequest } from "@/types/model/callback";
import { type ComponentChangeEvent } from "@/types/state/event";
import { getInputValues } from "@/actions/helpers/getInputValues";
import { applyStateChangeRequests } from "@/actions/helpers/applyStateChangeRequests";
import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
import { equalObjPaths } from "@/utils/objPath";
export function handleComponentChange(
contribPoint: ContribPoint,
contribIndex: number,
changeEvent: ComponentChangeEvent,
) {
if (store.getState().extensions.length === 0) {
// Exit immediately if there are no extensions (yet)
return;
}
// Apply actual component state change immediately
applyStateChangeRequests([
{
contribPoint,
contribIndex,
stateChanges: [
{
id: changeEvent.id,
property: changeEvent.property,
value: changeEvent.value,
},
],
},
]);
const callbackRequests = getCallbackRequests(
contribPoint,
contribIndex,
changeEvent,
);
if (callbackRequests && callbackRequests.length > 0) {
invokeCallbacks(callbackRequests);
}
}
/**
* Collect callback requests for the callbacks of
* the contribution that are triggered by the change event.
*
* @param contribPoint Name of the contribution point.
* @param contribIndex Index of the contribution.
* @param changeEvent The change event.
*/
function getCallbackRequests(
contribPoint: ContribPoint,
contribIndex: number,
changeEvent: ComponentChangeEvent,
): CallbackRequest[] {
const { configuration, contributionsRecord } = store.getState();
const { hostStore } = configuration;
const contributions = contributionsRecord[contribPoint];
const contribution = contributions[contribIndex];
const callbackRequests: CallbackRequest[] = [];
(contribution.callbacks || []).forEach((callback, callbackIndex) => {
if (callback.inputs && callback.inputs.length) {
const inputs = callback.inputs;
const inputIndex = inputs.findIndex(
(input) =>
!input.noTrigger &&
input.id &&
!input.id.startsWith("@") &&
input.id === changeEvent.id &&
equalObjPaths(input.property, changeEvent.property),
);
if (inputIndex >= 0) {
// Collect output IDs for updating their respective loading states
const outputs = contribution.callbacks?.[callbackIndex]["outputs"];
const outputIds: string[] =
outputs?.map((output) => output.id as string) ?? [];
// Collect triggered callback
callbackRequests.push({
contribPoint,
contribIndex,
callbackIndex,
inputIndex,
inputValues: getInputValues(inputs, contribution, hostStore),
outputIds: outputIds,
});
}
}
});
return callbackRequests;
}