55 */
66
77import { store } from "@/store" ;
8- import type {
9- CallbackRequest ,
10- StateChangeRequest ,
11- } from "@/types/model/callback" ;
12- import type { Output } from "@/types/model/channel" ;
13- import type { ComponentState } from "@/types/state/component" ;
8+ import type { CallbackRequest } from "@/types/model/callback" ;
149import { fetchCallback } from "@/api/fetchCallback" ;
1510import { applyStateChangeRequests } from "@/actions/helpers/applyStateChangeRequests" ;
16- import { formatObjPath } from "@/utils/objPath" ;
17-
18- interface PendingProgressTarget {
19- contribPoint : string ;
20- contribIndex : number ;
21- id : string ;
22- output : Output ;
23- }
24-
25- const progressComponentTypes = new Set ( [
26- "CircularProgress" ,
27- "CircularProgressWithLabel" ,
28- "LinearProgress" ,
29- "LinearProgressWithLabel" ,
30- ] ) ;
31-
32- const pendingProgressCounts : Record < string , number > = { } ;
11+ import {
12+ getPendingProgressTargets ,
13+ releasePendingProgressTargets ,
14+ showPendingProgressTargets ,
15+ } from "@/actions/helpers/pendingProgress" ;
3316
3417export function invokeCallbacks ( callbackRequests : CallbackRequest [ ] ) {
3518 const { configuration } = store . getState ( ) ;
@@ -67,118 +50,8 @@ export function invokeCallbacks(callbackRequests: CallbackRequest[]) {
6750 ) ;
6851}
6952
70- function getPendingProgressTargets (
71- callbackRequests : CallbackRequest [ ] ,
72- ) : PendingProgressTarget [ ] {
73- const { contributionsRecord } = store . getState ( ) ;
74- const targets : PendingProgressTarget [ ] = [ ] ;
75- const targetKeys = new Set < string > ( ) ;
76-
77- callbackRequests . forEach ( ( { contribPoint, contribIndex, callbackIndex } ) => {
78- const contribution = contributionsRecord [ contribPoint ] ?. [ contribIndex ] ;
79- const callback = contribution ?. callbacks ?. [ callbackIndex ] ;
80- callback ?. outputs ?. forEach ( ( output ) => {
81- if (
82- formatObjPath ( output . property ) === "visible" &&
83- isProgressComponent ( contribution . component , output . id )
84- ) {
85- const target = { contribPoint, contribIndex, id : output . id , output } ;
86- const key = getPendingProgressTargetKey ( target ) ;
87- if ( ! targetKeys . has ( key ) ) {
88- targetKeys . add ( key ) ;
89- targets . push ( target ) ;
90- }
91- }
92- } ) ;
93- } ) ;
94-
95- return targets ;
96- }
97-
98- function showPendingProgressTargets ( targets : PendingProgressTarget [ ] ) {
99- incrementPendingProgressCounts ( targets ) ;
100- applyPendingProgressTargets ( targets , true ) ;
101- }
102-
103- function releasePendingProgressTargets (
104- targets : PendingProgressTarget [ ] ,
105- callbackSucceeded : boolean ,
106- ) {
107- decrementPendingProgressCounts ( targets ) ;
108- const stillPendingTargets = targets . filter (
109- ( target ) => pendingProgressCounts [ getPendingProgressTargetKey ( target ) ] > 0 ,
110- ) ;
111- applyPendingProgressTargets ( stillPendingTargets , true ) ;
112-
113- if ( ! callbackSucceeded ) {
114- const completedTargets = targets . filter (
115- ( target ) => ! pendingProgressCounts [ getPendingProgressTargetKey ( target ) ] ,
116- ) ;
117- applyPendingProgressTargets ( completedTargets , false ) ;
118- }
119- }
120-
121- function incrementPendingProgressCounts ( targets : PendingProgressTarget [ ] ) {
122- targets . forEach ( ( target ) => {
123- const key = getPendingProgressTargetKey ( target ) ;
124- pendingProgressCounts [ key ] = ( pendingProgressCounts [ key ] || 0 ) + 1 ;
125- } ) ;
126- }
127-
128- function decrementPendingProgressCounts ( targets : PendingProgressTarget [ ] ) {
129- targets . forEach ( ( target ) => {
130- const key = getPendingProgressTargetKey ( target ) ;
131- const count = ( pendingProgressCounts [ key ] || 0 ) - 1 ;
132- if ( count > 0 ) {
133- pendingProgressCounts [ key ] = count ;
134- } else {
135- delete pendingProgressCounts [ key ] ;
136- }
137- } ) ;
138- }
139-
140- function applyPendingProgressTargets (
141- targets : PendingProgressTarget [ ] ,
142- visible : boolean ,
143- ) {
144- if ( targets . length === 0 ) {
145- return ;
146- }
147- applyStateChangeRequests (
148- targets . map < StateChangeRequest > ( ( target ) => ( {
149- contribPoint : target . contribPoint ,
150- contribIndex : target . contribIndex ,
151- stateChanges : [ { ...target . output , value : visible } ] ,
152- } ) ) ,
153- ) ;
154- }
155-
156- function getPendingProgressTargetKey ( target : PendingProgressTarget ) {
157- return `${ target . contribPoint } -${ target . contribIndex } -${ target . id } ` ;
158- }
159-
160- function isProgressComponent (
161- component : ComponentState | undefined ,
162- id : string ,
163- ) : boolean {
164- if ( ! component ) {
165- return false ;
166- }
167- if ( component . id === id ) {
168- return progressComponentTypes . has ( component . type ) ;
169- }
170- return Boolean (
171- component . children ?. some (
172- ( child ) =>
173- typeof child === "object" &&
174- child !== null &&
175- isProgressComponent ( child , id ) ,
176- ) ,
177- ) ;
178- }
179-
18053let invocationCounter = 0 ;
18154
18255function getInvocationId ( ) {
18356 return invocationCounter ++ ;
184- }
57+ }
0 commit comments