11import { EventType } from 'overmind'
22
3+ const OVERMIND = Symbol ( 'OVERMIND' )
4+ const IS_PRODUCTION = process . env . NODE_ENV === 'production'
5+
36let nextComponentId = 0
47
58function createMixin ( overmind , propsCallback ) {
@@ -8,59 +11,69 @@ function createMixin(overmind, propsCallback) {
811
912 return {
1013 beforeMount ( this : any ) {
11- this . __tree = ( overmind as any ) . proxyStateTree . getTrackStateTree ( )
12- this . __componentInstanceId = componentInstanceId ++
13- this . __onUpdate = ( mutations , paths , flushId ) => {
14- this . __currentFlushId = flushId
15- this . $forceUpdate ( )
14+ this [ OVERMIND ] = {
15+ tree : ( overmind as any ) . proxyStateTree . getTrackStateTree ( ) ,
16+ componentInstanceId : componentInstanceId ++ ,
17+ onUpdate : ( mutations , paths , flushId ) => {
18+ this [ OVERMIND ] . currentFlushId = flushId
19+ this . $forceUpdate ( )
20+ } ,
1621 }
1722 this . overmind = {
18- state : this . __tree . state ,
23+ state : this [ OVERMIND ] . tree . state ,
1924 actions : overmind . actions ,
2025 effects : overmind . effects ,
2126 addMutationListener : overmind . addMutationListener ,
2227 }
28+
2329 if ( propsCallback ) {
2430 Object . assign (
2531 this ,
2632 propsCallback ( {
27- state : this . __tree . state ,
33+ state : this [ OVERMIND ] . tree . state ,
2834 actions : overmind . actions ,
2935 effects : overmind . effects ,
3036 } )
3137 )
3238 }
33- this . __tree . track ( this . __onUpdate )
39+ this [ OVERMIND ] . tree . track ( this [ OVERMIND ] . onUpdate )
3440 } ,
3541 beforeUpdate ( this : any ) {
36- this . __tree . track ( this . __onUpdate )
37- } ,
38- mounted ( this : any ) {
39- overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
40- componentId,
41- componentInstanceId : this . __componentInstanceId ,
42- name : this . $options . name || '' ,
43- paths : Array . from ( this . __tree . pathDependencies ) as any ,
44- } )
45- } ,
46- updated ( this : any ) {
47- overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
48- componentId,
49- componentInstanceId : this . __componentInstanceId ,
50- name : this . $options . name || '' ,
51- flushId : this . __currentFlushId ,
52- paths : Array . from ( this . __tree . pathDependencies ) as any ,
53- } )
42+ this [ OVERMIND ] . tree . track ( this [ OVERMIND ] . onUpdate )
5443 } ,
44+ ...( IS_PRODUCTION
45+ ? null
46+ : {
47+ mounted ( this : any ) {
48+ overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
49+ componentId,
50+ componentInstanceId : this [ OVERMIND ] . componentInstanceId ,
51+ name : this . $options . name || '' ,
52+ paths : Array . from ( this [ OVERMIND ] . tree . pathDependencies ) as any ,
53+ } )
54+ } ,
55+ updated ( this : any ) {
56+ overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
57+ componentId,
58+ componentInstanceId : this [ OVERMIND ] . componentInstanceId ,
59+ name : this . $options . name || '' ,
60+ flushId : this [ OVERMIND ] . currentFlushId ,
61+ paths : Array . from ( this [ OVERMIND ] . tree . pathDependencies ) as any ,
62+ } )
63+ } ,
64+ } ) ,
5565 beforeDestroy ( this : any ) {
66+ // @ts -ignore
67+ overmind . proxyStateTree . disposeTree ( this [ OVERMIND ] . tree )
68+ if ( IS_PRODUCTION ) {
69+ return
70+ }
71+
5672 overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
5773 componentId,
58- componentInstanceId : this . __componentInstanceId ,
74+ componentInstanceId : this [ OVERMIND ] . componentInstanceId ,
5975 name : this . $options . name || '' ,
6076 } )
61-
62- // @ts -ignore
63- overmind . proxyStateTree . disposeTree ( this . __tree )
6477 } ,
6578 }
6679}
0 commit comments