1- import { PureComponent , createElement } from 'react' ;
1+ import { PureComponent , createElement , createRef } from 'react' ;
22import { Scope } from './scope' ;
33
44type Props = {
@@ -12,20 +12,75 @@ type State = {
1212 flip : boolean ;
1313} ;
1414
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+ ? ( props . targetRef || moduleEntries . some ( ( [ mkey ] ) => props . hasOwnProperty ( mkey ) ) )
32+ : false
33+ }
34+
35+ function moduleProcessor ( base , current , props ) {
36+ if ( current && base . length ) {
37+ base . forEach ( ( [ key , f ] ) => {
38+ const prop = props [ key ]
39+ if ( prop ) f ( current , prop )
40+ } ) ;
41+ }
42+ }
43+
1544export default class Incorporator extends PureComponent < Props , State > {
45+ private ref : any ;
46+ private selector : string | symbol ;
47+ private unsubscribe : any ;
48+ private element : any ;
49+ private setRef : any ;
50+
1651 constructor ( props : Props ) {
1752 super ( props ) ;
53+ this . element = null
54+
1855 this . state = { flip : false } ;
1956 this . selector = props . targetProps . sel ;
20- }
2157
22- private selector : string | symbol ;
23- private unsubscribe : any ;
58+ if ( props . targetRef ) {
59+ if ( typeof props . targetRef === 'function' ) {
60+ this . setRef = element => {
61+ this . element = element ;
62+ props . targetRef ( element ) ;
63+ } ;
64+
65+ this . ref = this . setRef ;
66+ } else {
67+ this . ref = props . targetRef ;
68+ }
69+ } else {
70+ this . ref = hasModuleProps ( props . targetProps ) ? createRef ( ) : null ;
71+ }
72+ }
2473
2574 public componentDidMount ( ) {
2675 this . unsubscribe = this . props . scope . subscribe ( this . selector , ( ) => {
2776 this . setState ( ( prev : any ) => ( { flip : ! prev . flip } ) ) ;
2877 } ) ;
78+
79+ moduleProcessor ( onMounts , this . element || ( this . ref && this . ref . current ) , this . props . targetProps )
80+ }
81+
82+ public componentDidUpdate ( ) {
83+ moduleProcessor ( onUpdates , this . element || ( this . ref && this . ref . current ) , this . props . targetProps )
2984 }
3085
3186 private incorporateHandlers < P > ( props : P , scope : Scope ) : P {
@@ -38,27 +93,31 @@ export default class Incorporator extends PureComponent<Props, State> {
3893 }
3994
4095 private materializeTargetProps ( ) {
41- const { targetProps, targetRef , scope} = this . props ;
96+ const { targetProps, scope} = this . props ;
4297 let output = { ...targetProps } ;
4398 output = this . incorporateHandlers ( output , scope ) ;
44- if ( targetRef ) {
45- output . ref = targetRef ;
99+ if ( this . ref ) {
100+ output . ref = this . ref ;
46101 }
47102 delete output . sel ;
103+ moduleEntries . forEach ( pair => delete output [ pair [ 0 ] ] )
48104 return output ;
49105 }
50106
51107 public render ( ) {
52108 const { target} = this . props ;
53109 const targetProps = this . materializeTargetProps ( ) ;
110+
54111 if ( targetProps . children ) {
55112 return createElement ( target , targetProps , targetProps . children ) ;
56113 } else {
57114 return createElement ( target , targetProps ) ;
58115 }
59116 }
60117
61- public componentWillUnmount ( ) {
118+ public componentWillUnmount ( ) {
119+ moduleProcessor ( onUnmounts , this . element || ( this . ref && this . ref . current ) , this . props . targetProps )
120+
62121 this . unsubscribe ( ) ;
63122 }
64123}
0 commit comments