@@ -43,15 +43,16 @@ export function createConnectedStoreAs<
4343 let Context = React . createContext ( { __MISSING_PROVIDER__ : true } as any )
4444
4545 type ContainerState = {
46- storeDefinitions : { [ K in keyof States ] : StoreDefinition < States [ K ] > | null }
4746 storeSnapshots : { [ K in keyof States ] : StoreSnapshot < States [ K ] > | null }
48- subscriptions : { [ K in keyof States ] : Subscription }
4947 }
5048
5149 class Container extends React . Component <
5250 ContainerPropsAs < States > ,
5351 ContainerState
5452 > {
53+ storeDefinitions : { [ K in keyof States ] : StoreDefinition < States [ K ] > }
54+ subscriptions : { [ K in keyof States ] : Subscription } | null
55+
5556 constructor ( props : ContainerPropsAs < States > ) {
5657 super ( props )
5758
@@ -65,37 +66,36 @@ export function createConnectedStoreAs<
6566 fx ( stores as any ) // TODO
6667 }
6768
69+ this . storeDefinitions = stores as any
70+ this . subscriptions = this . _createSubscriptions ( )
6871 this . state = {
69- storeDefinitions : stores as any , // TODO
7072 // TODO
71- storeSnapshots : mapValues ( stores , _ => _ . getCurrentSnapshot ( ) ) as any ,
72- subscriptions : mapValues ( stores , ( _ , k ) =>
73- _ . onAll ( ) . subscribe ( ( ) =>
74- this . setState ( state => ( {
75- storeSnapshots : Object . assign ( { } , state . storeSnapshots , {
76- [ k ] : _ . getCurrentSnapshot ( )
77- } )
78- } ) )
79- )
73+ storeSnapshots : mapValues ( stores , _ => _ . getCurrentSnapshot ( ) ) as any
74+ }
75+ }
76+
77+ _createSubscriptions = ( ) =>
78+ mapValues ( this . storeDefinitions , ( _ , k ) =>
79+ _ . onAll ( ) . subscribe ( ( ) =>
80+ this . setState ( state => ( {
81+ storeSnapshots : Object . assign ( { } , state . storeSnapshots , {
82+ [ k ] : _ . getCurrentSnapshot ( )
83+ } )
84+ } ) )
8085 )
86+ ) as any
87+
88+ componentDidMount ( ) : void {
89+ if ( this . subscriptions == null ) {
90+ this . subscriptions = this . _createSubscriptions ( )
8191 }
8292 }
8393
8494 componentWillUnmount ( ) {
85- mapValues ( this . state . subscriptions , _ => _ . unsubscribe ( ) )
86- // Let the state get GC'd.
87- // TODO: Find a more elegant way to do this.
88- if ( this . state . storeSnapshots ) {
95+ if ( this . subscriptions != null ) {
96+ mapValues ( this . subscriptions , _ => _ . unsubscribe ( ) )
97+ this . subscriptions = null
8998 }
90- mapValues ( this . state . storeSnapshots , _ => ( ( _ as any ) . state = null ) )
91- mapValues (
92- this . state . storeSnapshots ,
93- _ => ( ( _ as any ) . storeDefinition = null )
94- )
95- mapValues (
96- this . state . storeDefinitions ,
97- _ => ( ( _ as any ) . storeSnapshot = null )
98- )
9999 }
100100
101101 render ( ) {
0 commit comments