1- // @ts -ignore
2- import { Injectable } from '@angular/core'
31import { Overmind , EventType } from 'overmind'
42import { BehaviorSubject , Observable } from 'rxjs'
53import { map } from 'rxjs/operators'
64
5+ class ServiceBase { }
6+
77// @ts -ignore
88const IS_PRODUCTION = process . env . NODE_ENV === 'production'
99
1010let nextComponentId = 0
1111
12- export const Track = ( Component ) => {
12+ const Track = ( Component ) => {
1313 if ( IS_PRODUCTION ) {
1414 return
1515 }
@@ -18,7 +18,7 @@ export const Track = (Component) => {
1818 let componentInstanceId = 0
1919
2020 function findService ( target ) {
21- return Object . keys ( target ) . find ( ( key ) => target [ key ] instanceof Service )
21+ return Object . keys ( target ) . find ( ( key ) => target [ key ] instanceof ServiceBase )
2222 }
2323
2424 const targetNgAfterViewInit = Component . prototype . ngAfterViewInit
@@ -34,65 +34,83 @@ export const Track = (Component) => {
3434 }
3535}
3636
37- @Injectable ( )
38- export class Service < App extends Overmind < any > > {
39- private tree : any
40- private state$ : Observable < any >
41- private subject : BehaviorSubject < any >
42- private overmind : App
43- private componentDetails : any
44- actions : App [ 'actions' ]
45- effects : App [ 'effects' ]
46- addMutationListener : App [ 'addMutationListener' ]
47- constructor ( overmind : App ) {
48- this . tree = ( overmind as any ) . proxyStateTree . getTrackStateTreeWithProxifier ( )
49- this . subject = new BehaviorSubject ( this . tree . state )
50- this . state$ = this . subject . asObservable ( )
51- this . overmind = overmind
37+ interface IService < App extends Overmind < any > > {
38+ new ( ) : {
39+ actions : App [ 'actions' ]
40+ effects : App [ 'effects' ]
41+ addMutationListener : App [ 'addMutationListener' ]
42+ select < T > ( expr : ( state : App [ 'state' ] ) => T ) : Observable < T >
43+ select ( ) : Observable < App [ 'state' ] >
44+ }
45+ Track
46+ }
5247
53- this . actions = this . overmind . actions
54- this . addMutationListener = this . overmind . addMutationListener
48+ export function createService < App extends Overmind < any > > (
49+ overmind : App
50+ ) : IService < App > {
51+ return class Service extends ServiceBase {
52+ static Track
53+ private tree : any
54+ private state$ : Observable < any >
55+ private subject : BehaviorSubject < any >
56+ private overmind : App
57+ private componentDetails : any
58+ actions : App [ 'actions' ]
59+ effects : App [ 'effects' ]
60+ addMutationListener : App [ 'addMutationListener' ]
61+ constructor ( ) {
62+ super ( )
63+ this . tree = ( overmind as any ) . proxyStateTree . getTrackStateTreeWithProxifier ( )
64+ this . subject = new BehaviorSubject ( this . tree . state )
65+ this . state$ = this . subject . asObservable ( )
66+ this . overmind = overmind
5567
56- this . tree . track ( this . onUpdate )
57- }
58- private addComponent ( componentDetails ) {
59- this . componentDetails = componentDetails
60- this . overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
61- componentId : componentDetails . componentId ,
62- componentInstanceId : componentDetails . componentInstanceId ,
63- name : componentDetails . name ,
64- paths : Array . from ( this . tree . pathDependencies ) as any ,
65- } )
66- }
67- private onUpdate = ( mutations , paths , flushId ) => {
68- this . tree . track ( this . onUpdate )
69- this . subject . next ( this . tree . state )
70- if ( this . componentDetails ) {
71- ; ( window [ '__zone_symbol__setTimeout' ] || setTimeout ) ( ( ) => {
72- this . overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
68+ this . actions = this . overmind . actions
69+ this . addMutationListener = this . overmind . addMutationListener
70+
71+ this . tree . track ( this . onUpdate )
72+ }
73+ private addComponent ( componentDetails ) {
74+ this . componentDetails = componentDetails
75+ this . overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
76+ componentId : componentDetails . componentId ,
77+ componentInstanceId : componentDetails . componentInstanceId ,
78+ name : componentDetails . name ,
79+ paths : Array . from ( this . tree . pathDependencies ) as any ,
80+ } )
81+ }
82+ private onUpdate = ( mutations , paths , flushId ) => {
83+ this . tree . track ( this . onUpdate )
84+ this . subject . next ( this . tree . state )
85+ if ( this . componentDetails ) {
86+ ; ( window [ '__zone_symbol__setTimeout' ] || setTimeout ) ( ( ) => {
87+ this . overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
88+ componentId : this . componentDetails . componentId ,
89+ componentInstanceId : this . componentDetails . componentInstanceId ,
90+ name : this . componentDetails . name ,
91+ paths : Array . from ( this . tree . pathDependencies ) as any ,
92+ flushId,
93+ } )
94+ } )
95+ }
96+ }
97+ private ngOnDestroy ( ) {
98+ ; ( this . overmind as any ) . proxyStateTree . disposeTree ( this . tree )
99+ if ( this . componentDetails ) {
100+ this . overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
73101 componentId : this . componentDetails . componentId ,
74102 componentInstanceId : this . componentDetails . componentInstanceId ,
75103 name : this . componentDetails . name ,
76- paths : Array . from ( this . tree . pathDependencies ) as any ,
77- flushId,
78104 } )
79- } )
105+ }
80106 }
81- }
82- private ngOnDestroy ( ) {
83- ; ( this . overmind as any ) . proxyStateTree . disposeTree ( this . tree )
84- if ( this . componentDetails ) {
85- this . overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
86- componentId : this . componentDetails . componentId ,
87- componentInstanceId : this . componentDetails . componentInstanceId ,
88- name : this . componentDetails . name ,
89- } )
107+ select < T > ( expr : ( state : App [ 'state' ] ) => T ) : Observable < T >
108+ select ( ) : Observable < App [ 'state' ] >
109+ select ( ) {
110+ const args = arguments
111+ return this . state$ . pipe (
112+ map ( ( value ) => ( args [ 0 ] ? args [ 0 ] ( value ) : value ) )
113+ )
90114 }
91115 }
92- select < T > ( expr : ( state : App [ 'state' ] ) => T ) : Observable < T >
93- select ( ) : Observable < App [ 'state' ] >
94- select ( ) {
95- const args = arguments
96- return this . state$ . pipe ( map ( ( value ) => ( args [ 0 ] ? args [ 0 ] ( value ) : value ) ) )
97- }
98116}
0 commit comments