1
- import { PureComponent , createElement } from 'react' ;
1
+ import { PureComponent , createElement , createRef } from 'react' ;
2
2
import { Scope } from './scope' ;
3
3
4
4
type Props = {
@@ -12,20 +12,58 @@ type State = {
12
12
flip : boolean ;
13
13
} ;
14
14
15
+ let moduleEntries : any = [ ]
16
+
17
+ let onMounts : any [ ] = [ ]
18
+ let onUpdates : any [ ] = [ ]
19
+ let onUnmounts : any [ ] = [ ]
20
+
21
+ export function setModules ( mods : any ) {
22
+ if ( mods === null || typeof mods !== 'object' ) return ;
23
+ moduleEntries = Object . entries ( mods )
24
+ onMounts = moduleEntries . map ( mod => [ mod [ 0 ] , mod [ 1 ] . componentDidMount ] ) . filter ( mod => mod [ 1 ] )
25
+ onUpdates = moduleEntries . map ( mod => [ mod [ 0 ] , mod [ 1 ] . componentDidUpdate ] ) . filter ( mod => mod [ 1 ] )
26
+ onUnmounts = moduleEntries . map ( mod => [ mod [ 0 ] , mod [ 1 ] . componentWillUnmount ] ) . filter ( mod => mod [ 1 ] )
27
+ }
28
+
29
+ export function hasModuleProps ( props ) {
30
+ return props
31
+ ? moduleEntries . some ( ( [ mkey ] ) => props . hasOwnProperty ( mkey ) )
32
+ : false
33
+ }
34
+
35
+ function moduleProcessor ( base , ref , props ) {
36
+ if ( ref && ref . current && base . length ) {
37
+ base . forEach ( ( [ key , f ] ) => {
38
+ const prop = props [ key ]
39
+ if ( prop ) f ( ref . current , prop )
40
+ } ) ;
41
+ }
42
+ }
43
+
15
44
export default class Incorporator extends PureComponent < Props , State > {
45
+ private ref : any ;
46
+ private selector : string | symbol ;
47
+ private unsubscribe : any ;
48
+
16
49
constructor ( props : Props ) {
17
50
super ( props ) ;
51
+
18
52
this . state = { flip : false } ;
19
53
this . selector = props . targetProps . sel ;
54
+ this . ref = props . targetRef || ( hasModuleProps ( props . targetProps ) ? createRef ( ) : null ) ;
20
55
}
21
56
22
- private selector : string | symbol ;
23
- private unsubscribe : any ;
24
-
25
57
public componentDidMount ( ) {
26
58
this . unsubscribe = this . props . scope . subscribe ( this . selector , ( ) => {
27
59
this . setState ( ( prev : any ) => ( { flip : ! prev . flip } ) ) ;
28
60
} ) ;
61
+
62
+ moduleProcessor ( onMounts , this . ref , this . props . targetProps )
63
+ }
64
+
65
+ public componentDidUpdate ( ) {
66
+ moduleProcessor ( onUpdates , this . ref , this . props . targetProps )
29
67
}
30
68
31
69
private incorporateHandlers < P > ( props : P , scope : Scope ) : P {
@@ -38,27 +76,31 @@ export default class Incorporator extends PureComponent<Props, State> {
38
76
}
39
77
40
78
private materializeTargetProps ( ) {
41
- const { targetProps, targetRef , scope} = this . props ;
79
+ const { targetProps, scope} = this . props ;
42
80
let output = { ...targetProps } ;
43
81
output = this . incorporateHandlers ( output , scope ) ;
44
- if ( targetRef ) {
45
- output . ref = targetRef ;
82
+ if ( this . ref ) {
83
+ output . ref = this . ref ;
46
84
}
47
85
delete output . sel ;
86
+ moduleEntries . forEach ( pair => delete output [ pair [ 0 ] ] )
48
87
return output ;
49
88
}
50
89
51
90
public render ( ) {
52
91
const { target} = this . props ;
53
92
const targetProps = this . materializeTargetProps ( ) ;
93
+
54
94
if ( targetProps . children ) {
55
95
return createElement ( target , targetProps , targetProps . children ) ;
56
96
} else {
57
97
return createElement ( target , targetProps ) ;
58
98
}
59
99
}
60
100
61
- public componentWillUnmount ( ) {
101
+ public componentWillUnmount ( ) {
102
+ moduleProcessor ( onUnmounts , this . ref , this . props . targetProps )
103
+
62
104
this . unsubscribe ( ) ;
63
105
}
64
106
}
0 commit comments