1- import { PureComponent , createElement } from 'react' ;
1+ import { PureComponent , createElement , createRef } from 'react' ;
22import { Scope } from './scope' ;
33
44type Props = {
@@ -12,20 +12,58 @@ 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+ const prop = props [ key ]
39+ if ( prop ) f ( ref . 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+
1649 constructor ( props : Props ) {
1750 super ( props ) ;
51+
1852 this . state = { flip : false } ;
1953 this . selector = props . targetProps . sel ;
54+ this . ref = props . targetRef || ( hasModuleProps ( props . targetProps ) ? createRef ( ) : null ) ;
2055 }
2156
22- private selector : string | symbol ;
23- private unsubscribe : any ;
24-
2557 public componentDidMount ( ) {
2658 this . unsubscribe = this . props . scope . subscribe ( this . selector , ( ) => {
2759 this . setState ( ( prev : any ) => ( { flip : ! prev . flip } ) ) ;
2860 } ) ;
61+
62+ moduleProcessor ( onMounts , this . ref , this . props . targetProps )
63+ }
64+
65+ public componentDidUpdate ( ) {
66+ moduleProcessor ( onUpdates , this . ref , this . props . targetProps )
2967 }
3068
3169 private incorporateHandlers < P > ( props : P , scope : Scope ) : P {
@@ -38,27 +76,31 @@ export default class Incorporator extends PureComponent<Props, State> {
3876 }
3977
4078 private materializeTargetProps ( ) {
41- const { targetProps, targetRef , scope} = this . props ;
79+ const { targetProps, scope} = this . props ;
4280 let output = { ...targetProps } ;
4381 output = this . incorporateHandlers ( output , scope ) ;
44- if ( targetRef ) {
45- output . ref = targetRef ;
82+ if ( this . ref ) {
83+ output . ref = this . ref ;
4684 }
4785 delete output . sel ;
86+ moduleEntries . forEach ( pair => delete output [ pair [ 0 ] ] )
4887 return output ;
4988 }
5089
5190 public render ( ) {
5291 const { target} = this . props ;
5392 const targetProps = this . materializeTargetProps ( ) ;
93+
5494 if ( targetProps . children ) {
5595 return createElement ( target , targetProps , targetProps . children ) ;
5696 } else {
5797 return createElement ( target , targetProps ) ;
5898 }
5999 }
60100
61- public componentWillUnmount ( ) {
101+ public componentWillUnmount ( ) {
102+ moduleProcessor ( onUnmounts , this . ref , this . props . targetProps )
103+
62104 this . unsubscribe ( ) ;
63105 }
64106}
0 commit comments