1- import type { Input } from "@/lib/types/model/channel" ;
1+ import {
2+ type Input ,
3+ isComponentChannel ,
4+ isContainerChannel ,
5+ isHostChannel ,
6+ } from "@/lib/types/model/channel" ;
27import type { ContributionState } from "@/lib/types/state/contribution" ;
38import {
49 type ComponentState ,
510 isComponentState ,
611 isContainerState ,
712} from "@/lib/types/state/component" ;
8- import { formatObjPath , getValue , normalizeObjPath } from "@/lib/utils/objPath" ;
13+ import { formatObjPath , getValue , type ObjPathLike } from "@/lib/utils/objPath" ;
914import { isObject } from "@/lib/utils/isObject" ;
1015import type { HostStore } from "@/lib/types/state/options" ;
1116
@@ -27,13 +32,17 @@ export function getInputValue(
2732 hostStore ?: HostStore ,
2833) : unknown {
2934 let inputValue : unknown = undefined ;
30- const dataSource = input . link || "component" ;
31- if ( dataSource === "component" && contributionState . component ) {
32- inputValue = getInputValueFromComponent ( input , contributionState . component ) ;
33- } else if ( dataSource === "container" && contributionState . container ) {
34- inputValue = getInputValueFromState ( input , contributionState . container ) ;
35- } else if ( dataSource === "app" && hostStore ) {
36- inputValue = getInputValueFromHostStore ( input , hostStore ) ;
35+ const { id, property } = input ;
36+ if ( isComponentChannel ( input ) && contributionState . component ) {
37+ inputValue = getInputValueFromComponent (
38+ contributionState . component ,
39+ id ,
40+ property ,
41+ ) ;
42+ } else if ( isContainerChannel ( input ) && contributionState . container ) {
43+ inputValue = getInputValueFromState ( contributionState . container , property ) ;
44+ } else if ( isHostChannel ( input ) && hostStore ) {
45+ inputValue = getInputValueFromHostStore ( hostStore , property ) ;
3746 } else {
3847 console . warn ( `input with unknown data source:` , input ) ;
3948 }
@@ -47,16 +56,17 @@ export function getInputValue(
4756
4857// we export for testing only
4958export function getInputValueFromComponent (
50- input : Input ,
5159 componentState : ComponentState ,
60+ id : string ,
61+ property : ObjPathLike ,
5262) : unknown {
53- if ( componentState . id === input . id ) {
54- return getValue ( componentState , input . property ) ;
63+ if ( componentState . id === id ) {
64+ return getValue ( componentState , property ) ;
5565 } else if ( isContainerState ( componentState ) ) {
5666 for ( let i = 0 ; i < componentState . children . length ; i ++ ) {
5767 const item = componentState . children [ i ] ;
5868 if ( isComponentState ( item ) ) {
59- const itemValue = getInputValueFromComponent ( input , item ) ;
69+ const itemValue = getInputValueFromComponent ( item , id , property ) ;
6070 if ( itemValue !== noValue ) {
6171 return itemValue ;
6272 }
@@ -68,24 +78,16 @@ export function getInputValueFromComponent(
6878
6979// we export for testing only
7080export function getInputValueFromState (
71- input : Input ,
7281 state : object | undefined ,
82+ property : ObjPathLike ,
7383) : unknown {
74- let inputValue : unknown = state ;
75- if ( input . id && isObject ( inputValue ) ) {
76- inputValue = inputValue [ input . id ] ;
77- }
78- if ( isObject ( inputValue ) ) {
79- const state = inputValue ;
80- const property = normalizeObjPath ( input . property ) ;
81- inputValue = getValue ( state , property ) ;
82- }
83- return inputValue ;
84+ return isObject ( state ) ? getValue ( state , property ) : undefined ;
8485}
8586
87+ // we export for testing only
8688export function getInputValueFromHostStore (
87- input : Input ,
8889 hostStore : HostStore ,
90+ property : ObjPathLike ,
8991) : unknown {
90- return hostStore . get ( formatObjPath ( input . property ) ) ;
92+ return hostStore . get ( formatObjPath ( property ) ) ;
9193}
0 commit comments