@@ -7,73 +7,65 @@ import type {
77} from "@/lib/types/model/callback" ;
88import type { Input } from "@/lib/types/model/channel" ;
99import { getInputValues } from "@/lib/actions/helpers/getInputValues" ;
10- import {
11- getValue ,
12- type ObjPath ,
13- normalizeObjPath ,
14- formatObjPath ,
15- } from "@/lib/utils/objPath" ;
10+ import { formatObjPath } from "@/lib/utils/objPath" ;
1611import { invokeCallbacks } from "@/lib/actions/helpers/invokeCallbacks" ;
1712import type { ContributionState } from "@/lib/types/state/contribution" ;
18- import type { GetDerivedState } from "@/lib/types/state/store " ;
13+ import type { HostStore } from "@/lib/types/state/options " ;
1914
2015/**
2116 * A reference to a property of an input of a callback of a contribution.
2217 */
2318export interface PropertyRef extends ContribRef , CallbackRef , InputRef {
24- /** The property name as path . */
25- propertyPath : ObjPath ;
19+ /** The property. */
20+ property : string ;
2621}
2722
28- export function handleHostStoreChange < S extends object = object > (
29- currState : S ,
30- prevState : S ,
31- ) {
32- if ( store . getState ( ) . extensions . length === 0 ) {
33- // Exit immediately if there are no extensions (yet)
23+ export function handleHostStoreChange ( ) {
24+ const { extensions, configuration, contributionsRecord } = store . getState ( ) ;
25+ const { hostStore } = configuration ;
26+ if ( ! hostStore || extensions . length === 0 ) {
27+ // Exit if no host store configured or
28+ // there are no extensions (yet)
29+ return ;
30+ }
31+ const propertyRefs = getHostStorePropertyRefs ( ) ;
32+ if ( ! propertyRefs || propertyRefs . length === 0 ) {
33+ // Exit if there are is nothing to be changed
3434 return ;
3535 }
36- const { configuration, contributionsRecord } = store . getState ( ) ;
37- const { getDerivedHostState } = configuration ;
3836 const callbackRequests = getCallbackRequests (
37+ propertyRefs ,
3938 contributionsRecord ,
40- currState ,
41- prevState ,
42- getDerivedHostState ,
39+ hostStore ,
4340 ) ;
44- invokeCallbacks ( callbackRequests ) ;
41+ if ( callbackRequests && callbackRequests . length > 0 ) {
42+ invokeCallbacks ( callbackRequests ) ;
43+ }
4544}
4645
47- function getCallbackRequests < S extends object = object > (
46+ function getCallbackRequests (
47+ propertyRefs : PropertyRef [ ] ,
4848 contributionsRecord : Record < string , ContributionState [ ] > ,
49- hostState : S ,
50- prevHostState : S ,
51- getDerivedHostState : GetDerivedState < S > | undefined ,
49+ hostStore : HostStore ,
5250) : CallbackRequest [ ] {
53- return getHostStorePropertyRefs ( )
54- . filter ( ( propertyRef ) =>
55- hasPropertyChanged (
56- propertyRef . propertyPath ,
57- hostState ,
58- prevHostState ,
59- getDerivedHostState ,
60- ) ,
61- )
62- . map ( ( propertyRef ) => {
63- const contributions = contributionsRecord [ propertyRef . contribPoint ] ;
64- const contribution = contributions [ propertyRef . contribIndex ] ;
65- const callback = contribution . callbacks ! [ propertyRef . callbackIndex ] ;
66- const inputValues = getInputValues (
67- callback . inputs ! ,
68- contribution ,
69- hostState ,
70- getDerivedHostState as GetDerivedState ,
71- ) ;
72- return { ...propertyRef , inputValues } ;
73- } ) ;
51+ return propertyRefs . map ( ( propertyRef ) => {
52+ const contributions = contributionsRecord [ propertyRef . contribPoint ] ;
53+ const contribution = contributions [ propertyRef . contribIndex ] ;
54+ const callback = contribution . callbacks ! [ propertyRef . callbackIndex ] ;
55+ const inputValues = getInputValues (
56+ callback . inputs ! ,
57+ contribution ,
58+ hostStore ,
59+ ) ;
60+ return { ...propertyRef , inputValues } ;
61+ } ) ;
7462}
7563
76- // const getHostStorePropertyRefs = memoizeOne(_getHostStorePropertyRefs);
64+ // TODO: use a memoized selector to get hostStorePropertyRefs
65+ // Note that this will only be effective and once we split the
66+ // static contribution infos and dynamic contribution states.
67+ // The hostStorePropertyRefs only depend on the static
68+ // contribution infos.
7769
7870/**
7971 * Get the static list of host state property references for all contributions.
@@ -87,13 +79,13 @@ function getHostStorePropertyRefs(): PropertyRef[] {
8779 ( contribution . callbacks || [ ] ) . forEach (
8880 ( callback , callbackIndex ) =>
8981 ( callback . inputs || [ ] ) . forEach ( ( input , inputIndex ) => {
90- if ( ! input . noTrigger && input . link === "app" ) {
82+ if ( ! input . noTrigger && input . link === "app" && input . property ) {
9183 propertyRefs . push ( {
9284 contribPoint,
9385 contribIndex,
9486 callbackIndex,
9587 inputIndex,
96- propertyPath : normalizeObjPath ( input . property ! ) ,
88+ property : formatObjPath ( input . property ) ,
9789 } ) ;
9890 }
9991 } ) ,
@@ -103,25 +95,3 @@ function getHostStorePropertyRefs(): PropertyRef[] {
10395 } ) ;
10496 return propertyRefs ;
10597}
106-
107- function hasPropertyChanged < S extends object = object > (
108- propertyPath : ObjPath ,
109- currState : S ,
110- prevState : S ,
111- getDerivedHostState : GetDerivedState < S > | undefined ,
112- ) : boolean {
113- const currValue = getValue ( currState , propertyPath ) ;
114- const prevValue = getValue ( prevState , propertyPath ) ;
115- if (
116- currValue === undefined &&
117- prevValue === undefined &&
118- getDerivedHostState !== undefined
119- ) {
120- const propertyName = formatObjPath ( propertyPath ) ;
121- return ! Object . is (
122- getDerivedHostState ( currState , propertyName ) ,
123- getDerivedHostState ( prevState , propertyName ) ,
124- ) ;
125- }
126- return ! Object . is ( currValue , prevValue ) ;
127- }
0 commit comments