1- import { PureComponent , createElement } from 'react' ;
1+ import { PureComponent , createElement , createRef } from 'react' ;
22import { Scope } from './scope' ;
33
44type Props = {
@@ -12,11 +12,44 @@ 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+ ? 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+ f ( ref . current , props [ key ] )
39+ } ) ;
40+ }
41+
42+ }
43+
1544export default class Incorporator extends PureComponent < Props , State > {
45+ private ref : any ;
46+ private moduleProps : string ;
47+
1648 constructor ( props : Props ) {
1749 super ( props ) ;
1850 this . state = { flip : false } ;
1951 this . selector = props . targetProps . sel ;
52+ this . ref = props . targetRef || ( moduleEntries . some ( e => Object . keys ( props . targetProps ) . some ( key => key === e [ 0 ] ) ) ? createRef ( ) : null ) ;
2053 }
2154
2255 private selector : string | symbol ;
@@ -26,6 +59,12 @@ export default class Incorporator extends PureComponent<Props, State> {
2659 this . unsubscribe = this . props . scope . subscribe ( this . selector , ( ) => {
2760 this . setState ( ( prev : any ) => ( { flip : ! prev . flip } ) ) ;
2861 } ) ;
62+
63+ moduleProcessor ( onMounts , this . ref , this . props . targetProps )
64+ }
65+
66+ public componentDidUpdate ( ) {
67+ moduleProcessor ( onUpdates , this . ref , this . props . targetProps )
2968 }
3069
3170 private incorporateHandlers < P > ( props : P , scope : Scope ) : P {
@@ -38,19 +77,21 @@ export default class Incorporator extends PureComponent<Props, State> {
3877 }
3978
4079 private materializeTargetProps ( ) {
41- const { targetProps, targetRef , scope} = this . props ;
80+ const { targetProps, scope} = this . props ;
4281 let output = { ...targetProps } ;
4382 output = this . incorporateHandlers ( output , scope ) ;
44- if ( targetRef ) {
45- output . ref = targetRef ;
83+ if ( this . ref ) {
84+ output . ref = this . ref ;
4685 }
4786 delete output . sel ;
87+ moduleEntries . forEach ( pair => delete output [ pair [ 0 ] ] )
4888 return output ;
4989 }
5090
5191 public render ( ) {
5292 const { target} = this . props ;
5393 const targetProps = this . materializeTargetProps ( ) ;
94+
5495 if ( targetProps . children ) {
5596 return createElement ( target , targetProps , targetProps . children ) ;
5697 } else {
@@ -59,6 +100,8 @@ export default class Incorporator extends PureComponent<Props, State> {
59100 }
60101
61102 public componentWillUnmount ( ) {
103+ moduleProcessor ( onUnmounts , this . ref , this . props . targetProps )
104+
62105 this . unsubscribe ( ) ;
63106 }
64107}
0 commit comments