@@ -26,12 +26,13 @@ export function reflectFactory(context: Context) {
26
26
return function reflect < Props , Bind extends BindProps < Props > = BindProps < Props > > (
27
27
config : ReflectConfig < Props , Bind > ,
28
28
) : 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 || { } ) ;
30
31
31
32
return React . forwardRef ( ( props , ref ) => {
32
33
const storeProps = context . useUnit ( stores , config . useUnitConfig ) ;
33
34
const eventsProps = context . useUnit ( events as any , config . useUnitConfig ) ;
34
- const functionProps = useBindedFunctions ( functions ) ;
35
+ const functionProps = useBoundFunctions ( functions ) ;
35
36
36
37
const elementProps : Props = Object . assign (
37
38
{ ref } ,
@@ -42,42 +43,38 @@ export function reflectFactory(context: Context) {
42
43
props ,
43
44
) ;
44
45
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 ) ;
55
48
56
49
React . useEffect ( ( ) => {
57
- if ( mounted ) mounted ( ) ;
50
+ const hooks : Hooks = Object . assign ( { } , functionsHooks , eventsHooks ) ;
51
+
52
+ if ( hooks . mounted ) {
53
+ hooks . mounted ( ) ;
54
+ }
58
55
59
56
return ( ) => {
60
- if ( unmounted ) unmounted ( ) ;
57
+ if ( hooks . unmounted ) {
58
+ hooks . unmounted ( ) ;
59
+ }
61
60
} ;
62
- } , [ ] ) ;
61
+ } , [ eventsHooks , functionsHooks ] ) ;
63
62
64
63
return React . createElement ( config . view as any , elementProps as any ) ;
65
64
} ) ;
66
65
} ;
67
66
}
68
67
69
- function sortProps < Props , Bind extends BindProps < Props > = BindProps < Props > > (
70
- config : ReflectConfig < Props , Bind > ,
71
- ) {
68
+ function sortProps < T extends object > ( props : T ) {
72
69
type GenericEvent = Event < unknown > | Effect < unknown , unknown , unknown > ;
73
70
74
71
const events : Record < string , GenericEvent > = { } ;
75
72
const stores : Record < string , Store < unknown > > = { } ;
76
73
const data : Record < string , unknown > = { } ;
77
74
const functions : Record < string , Function > = { } ;
78
75
79
- for ( const key in config . bind ) {
80
- const value = config . bind [ key ] ;
76
+ for ( const key in props ) {
77
+ const value = props [ key ] ;
81
78
82
79
if ( is . event ( value ) || is . effect ( value ) ) {
83
80
events [ key ] = value ;
@@ -93,30 +90,18 @@ function sortProps<Props, Bind extends BindProps<Props> = BindProps<Props>>(
93
90
return { events, stores, data, functions } ;
94
91
}
95
92
96
- function useBindedFunctions ( functions : Record < string , Function > ) {
93
+ function useBoundFunctions ( functions : Record < string , Function > ) {
97
94
const scope = useProvidedScope ( ) ;
98
95
99
96
return React . useMemo ( ( ) => {
100
- const bindedFunctions : Record < string , Function > = { } ;
97
+ const boundFunctions : Record < string , Function > = { } ;
101
98
102
99
for ( const key in functions ) {
103
100
const fn = functions [ key ] ;
104
101
105
- bindedFunctions [ key ] = scopeBind ( fn , { scope : scope || undefined , safe : true } ) ;
102
+ boundFunctions [ key ] = scopeBind ( fn , { scope : scope || undefined , safe : true } ) ;
106
103
}
107
104
108
- return bindedFunctions ;
105
+ return boundFunctions ;
109
106
} , [ scope , functions ] ) ;
110
107
}
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