1+ import memoize from "micro-memoize" ;
2+
13import type {
4+ Callback ,
25 CallbackRef ,
36 CallbackRequest ,
47 ContribRef ,
@@ -11,6 +14,7 @@ import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
1114import type { ContributionState } from "@/types/state/contribution" ;
1215import type { HostStore } from "@/types/state/host" ;
1316import { store } from "@/store" ;
17+ import type { ContribPoint } from "@/types/model/extension" ;
1418
1519/**
1620 * A reference to a property of an input of a callback of a contribution.
@@ -33,7 +37,7 @@ export function handleHostStoreChange() {
3337 // Exit if there are no extensions (yet)
3438 return ;
3539 }
36- const propertyRefs = getHostStorePropertyRefs ( ) ;
40+ const propertyRefs = getHostStorePropertyRefs ( contributionsRecord ) ;
3741 if ( ! propertyRefs || propertyRefs . length === 0 ) {
3842 // Exit if there are is nothing to be changed
3943 return ;
@@ -66,37 +70,42 @@ function getCallbackRequests(
6670 } ) ;
6771}
6872
69- // TODO: use a memoized selector to get hostStorePropertyRefs
70- // Note that this will only be effective and once we split the
71- // static contribution infos and dynamic contribution states.
72- // The hostStorePropertyRefs only depend on the static
73- // contribution infos.
74-
7573/**
7674 * Get the static list of host state property references for all contributions.
7775 */
78- function getHostStorePropertyRefs ( ) : PropertyRef [ ] {
79- const { contributionsRecord } = store . getState ( ) ;
76+ const getHostStorePropertyRefs = memoize ( _getHostStorePropertyRefs ) ;
77+
78+ function getCallbackfn (
79+ contribPoint : string ,
80+ contribution : ContributionState ,
81+ contribIndex : number ,
82+ ) {
83+ const propertyRefs : PropertyRef [ ] = [ ] ;
84+ const callbacks : Callback [ ] = contribution . callbacks || [ ] ;
85+ callbacks . forEach ( ( callback , callbackIndex ) => {
86+ const inputs = callback . inputs || [ ] ;
87+ inputs . forEach ( ( input , inputIndex ) => {
88+ if ( ! input . noTrigger && input . id === "@app" && input . property ) {
89+ propertyRefs . push ( {
90+ contribPoint,
91+ contribIndex,
92+ callbackIndex,
93+ inputIndex,
94+ property : formatObjPath ( input . property ) ,
95+ } ) ;
96+ }
97+ } ) ;
98+ } ) ;
99+ return propertyRefs ;
100+ }
101+
102+ function _getHostStorePropertyRefs (
103+ contributionsRecord : Record < ContribPoint , ContributionState [ ] > ,
104+ ) : PropertyRef [ ] {
80105 const propertyRefs : PropertyRef [ ] = [ ] ;
81106 Object . getOwnPropertyNames ( contributionsRecord ) . forEach ( ( contribPoint ) => {
82107 const contributions = contributionsRecord [ contribPoint ] ;
83- contributions . forEach ( ( contribution , contribIndex ) => {
84- ( contribution . callbacks || [ ] ) . forEach (
85- ( callback , callbackIndex ) =>
86- ( callback . inputs || [ ] ) . forEach ( ( input , inputIndex ) => {
87- if ( ! input . noTrigger && input . id === "@app" && input . property ) {
88- propertyRefs . push ( {
89- contribPoint,
90- contribIndex,
91- callbackIndex,
92- inputIndex,
93- property : formatObjPath ( input . property ) ,
94- } ) ;
95- }
96- } ) ,
97- [ ] as Input [ ] ,
98- ) ;
99- } ) ;
108+ contributions . forEach ( getCallbackfn ( propertyRefs , contribPoint ) ) ;
100109 } ) ;
101110 return propertyRefs ;
102111}
0 commit comments