1- import appStore , { ContribPoint , ContributionState } from "../store/appStore" ;
1+ import appStore from "../store/appStore" ;
22import {
3- ComponentModel ,
4- ContainerModel ,
5- isContainerModel ,
6- PropertyChangeEvent ,
7- } from "../model/component" ;
3+ ComponentState ,
4+ ContainerState ,
5+ isContainerState ,
6+ } from "../state/component" ;
87import { CallbackCallRequest , ChangeRequest , Change } from "../model/callback" ;
98import fetchApiResult from "../utils/fetchApiResult" ;
109import { fetchChangeRequests } from "../api" ;
1110import { updateArray } from "../utils/updateArray" ;
11+ import { ContributionState } from "../state/contribution" ;
12+ import { PropertyChangeEvent } from "../model/event" ;
1213
13- export default function handleComponentPropertyChange (
14+ import { ContribPoint } from "../model/extension" ;
15+
16+ export default function applyPropertyChange (
1417 contribPoint : ContribPoint ,
1518 contribIndex : number ,
1619 contribEvent : PropertyChangeEvent ,
1720) {
18- const appState = appStore . getState ( ) ;
19- const contributionModels =
20- appState . contributionsRecordResult . data ! [ contribPoint ] ;
21+ const { contributionModelsRecord } = appStore . getState ( ) ;
22+ const contributionModels = contributionModelsRecord [ contribPoint ] ;
2123 const componentId = contribEvent . componentId ;
2224 const componentPropertyName = contribEvent . propertyName ;
2325 const componentPropertyValue = contribEvent . propertyValue ;
@@ -105,15 +107,14 @@ function applyChangeRequests(changeRequests: ChangeRequest[]) {
105107 contribIndex ,
106108 changes ,
107109 ) ;
108- const contributionStatesRecord =
109- appStore . getState ( ) . contributionStatesRecord ;
110+ const { contributionStatesRecord } = appStore . getState ( ) ;
110111 const contributionStates = contributionStatesRecord [ contribPoint ] ;
111112 const contributionState = contributionStates [ contribIndex ] ;
112- const componentModelOld = contributionState . componentState ;
113- let componentModel = componentModelOld ;
113+ const componentStateOld = contributionState . componentState ;
114+ let componentState = componentStateOld ;
114115 changes . forEach ( ( change ) => {
115- if ( componentModel && ( ! change . kind || change . kind === "Component" ) ) {
116- componentModel = updateComponentState ( componentModel , change ) ;
116+ if ( componentState && ( ! change . kind || change . kind === "Component" ) ) {
117+ componentState = applyComponentStateChange ( componentState , change ) ;
117118 } else {
118119 // TODO: process other output kinds which may not require componentModel.
119120 console . warn (
@@ -122,47 +123,44 @@ function applyChangeRequests(changeRequests: ChangeRequest[]) {
122123 ) ;
123124 }
124125 } ) ;
125- if ( componentModel && componentModel !== componentModelOld ) {
126+ if ( componentState && componentState !== componentStateOld ) {
126127 appStore . setState ( {
127128 contributionStatesRecord : {
128129 ...contributionStatesRecord ,
129130 [ contribPoint ] : updateArray < ContributionState > (
130131 contributionStates ,
131132 contribIndex ,
132- {
133- ...contributionState ,
134- componentState : componentModel ,
135- } ,
133+ { ...contributionState , componentState } ,
136134 ) ,
137135 } ,
138136 } ) ;
139137 }
140138 } ) ;
141139}
142140
143- function updateComponentState (
144- componentModel : ComponentModel ,
141+ function applyComponentStateChange (
142+ componentState : ComponentState ,
145143 change : Change ,
146- ) : ComponentModel {
147- if ( componentModel . id === change . id ) {
148- return { ...componentModel , [ change . property ] : change . value } ;
149- } else if ( isContainerModel ( componentModel ) ) {
150- const containerModelOld : ContainerModel = componentModel ;
151- let containerModelNew : ContainerModel = containerModelOld ;
152- for ( let i = 0 ; i < containerModelOld . components . length ; i ++ ) {
153- const itemOld = containerModelOld . components [ i ] ;
154- const itemNew = updateComponentState ( itemOld , change ) ;
144+ ) : ComponentState {
145+ if ( componentState . id === change . id ) {
146+ return { ...componentState , [ change . property ] : change . value } ;
147+ } else if ( isContainerState ( componentState ) ) {
148+ const containerStateOld : ContainerState = componentState ;
149+ let containerStateNew : ContainerState = containerStateOld ;
150+ for ( let i = 0 ; i < containerStateOld . components . length ; i ++ ) {
151+ const itemOld = containerStateOld . components [ i ] ;
152+ const itemNew = applyComponentStateChange ( itemOld , change ) ;
155153 if ( itemNew !== itemOld ) {
156- if ( containerModelNew === containerModelOld ) {
157- containerModelNew = {
158- ...containerModelOld ,
159- components : [ ...containerModelOld . components ] ,
154+ if ( containerStateNew === containerStateOld ) {
155+ containerStateNew = {
156+ ...containerStateOld ,
157+ components : [ ...containerStateOld . components ] ,
160158 } ;
161159 }
162- containerModelNew . components [ i ] = itemNew ;
160+ containerStateNew . components [ i ] = itemNew ;
163161 }
164162 }
165- return containerModelNew ;
163+ return containerStateNew ;
166164 }
167- return componentModel ;
165+ return componentState ;
168166}
0 commit comments