@@ -26,12 +26,13 @@ export function reflectFactory(context: Context) {
2626 return function reflect < Props , Bind extends BindProps < Props > = BindProps < Props > > (
2727 config : ReflectConfig < Props , Bind > ,
2828 ) : React . ExoticComponent < { } > {
29- const { stores, events, data, functions } = sortProps ( config ) ;
29+ const { stores, events, data, functions } = sortProps ( config . bind ) ;
30+ const hooks = sortProps ( config . hooks || { } ) ;
3031
3132 return React . forwardRef ( ( props , ref ) => {
3233 const storeProps = context . useUnit ( stores , config . useUnitConfig ) ;
3334 const eventsProps = context . useUnit ( events as any , config . useUnitConfig ) ;
34- const functionProps = useBindedFunctions ( functions ) ;
35+ const functionProps = useBoundFunctions ( functions ) ;
3536
3637 const elementProps : Props = Object . assign (
3738 { ref } ,
@@ -42,42 +43,38 @@ export function reflectFactory(context: Context) {
4243 props ,
4344 ) ;
4445
45- const mounted = wrapToHook (
46- config . hooks ?. mounted ,
47- context ,
48- config . useUnitConfig ,
49- ) ;
50- const unmounted = wrapToHook (
51- config . hooks ?. unmounted ,
52- context ,
53- config . useUnitConfig ,
54- ) ;
46+ const eventsHooks = context . useUnit ( hooks . events as any , config . useUnitConfig ) ;
47+ const functionsHooks = useBoundFunctions ( hooks . functions ) ;
5548
5649 React . useEffect ( ( ) => {
57- if ( mounted ) mounted ( ) ;
50+ const hooks : Hooks = Object . assign ( { } , functionsHooks , eventsHooks ) ;
51+
52+ if ( hooks . mounted ) {
53+ hooks . mounted ( ) ;
54+ }
5855
5956 return ( ) => {
60- if ( unmounted ) unmounted ( ) ;
57+ if ( hooks . unmounted ) {
58+ hooks . unmounted ( ) ;
59+ }
6160 } ;
62- } , [ ] ) ;
61+ } , [ eventsHooks , functionsHooks ] ) ;
6362
6463 return React . createElement ( config . view as any , elementProps as any ) ;
6564 } ) ;
6665 } ;
6766}
6867
69- function sortProps < Props , Bind extends BindProps < Props > = BindProps < Props > > (
70- config : ReflectConfig < Props , Bind > ,
71- ) {
68+ function sortProps < T extends object > ( props : T ) {
7269 type GenericEvent = Event < unknown > | Effect < unknown , unknown , unknown > ;
7370
7471 const events : Record < string , GenericEvent > = { } ;
7572 const stores : Record < string , Store < unknown > > = { } ;
7673 const data : Record < string , unknown > = { } ;
7774 const functions : Record < string , Function > = { } ;
7875
79- for ( const key in config . bind ) {
80- const value = config . bind [ key ] ;
76+ for ( const key in props ) {
77+ const value = props [ key ] ;
8178
8279 if ( is . event ( value ) || is . effect ( value ) ) {
8380 events [ key ] = value ;
@@ -93,30 +90,18 @@ function sortProps<Props, Bind extends BindProps<Props> = BindProps<Props>>(
9390 return { events, stores, data, functions } ;
9491}
9592
96- function useBindedFunctions ( functions : Record < string , Function > ) {
93+ function useBoundFunctions ( functions : Record < string , Function > ) {
9794 const scope = useProvidedScope ( ) ;
9895
9996 return React . useMemo ( ( ) => {
100- const bindedFunctions : Record < string , Function > = { } ;
97+ const boundFunctions : Record < string , Function > = { } ;
10198
10299 for ( const key in functions ) {
103100 const fn = functions [ key ] ;
104101
105- bindedFunctions [ key ] = scopeBind ( fn , { scope : scope || undefined , safe : true } ) ;
102+ boundFunctions [ key ] = scopeBind ( fn , { scope : scope || undefined , safe : true } ) ;
106103 }
107104
108- return bindedFunctions ;
105+ return boundFunctions ;
109106 } , [ scope , functions ] ) ;
110107}
111-
112- function wrapToHook ( hook : Hook | void , context : Context , config ?: UseUnitConifg ) {
113- if ( hookDefined ( hook ) ) {
114- return context . useUnit ( hook as EventCallable < void > , config ) ;
115- }
116-
117- return hook ;
118- }
119-
120- function hookDefined ( hook : Hook | void ) : hook is Hook {
121- return Boolean ( hook && ( is . event ( hook ) || is . effect ( hook ) ) ) ;
122- }
0 commit comments