11import { Provider } from "@angular/core" ;
22import { DefaultActions } from "../shared/store.enums" ;
33import { provideStoreStates } from "../shared/store.providers" ;
4+ import { Service , ServiceClass } from "../shared/store.types" ;
45import { MergeReducer } from "../variations/merge.reducer" ;
56import { StorageReducer } from "../variations/storage.reducer" ;
67import { StoreAction } from "./store.action" ;
@@ -9,27 +10,47 @@ import { StoreReducer } from "./store.reducer";
910
1011const DefaultReducers = [ StoreReducer , MergeReducer , StorageReducer ] ;
1112
12- export class StoreState < T extends any = any , A extends any [ ] = any [ ] , K extends string = string , F extends string = string > {
13+ export class StoreState < M extends any = any , N extends string = string , S extends Service = Service , A extends any [ ] = any [ ] , F extends string = string > {
1314 app ?: string ;
14- name : K ;
15- initial : T ;
15+ name : N ;
16+ initial : M ;
17+ service : ServiceClass < S > ;
1618 actions : A ;
1719 fallback : F [ ] = [ ] ;
1820 options : StoreOptions ;
1921 reducers : StoreReducer [ ] = [ ] ;
2022
21- constructor ( state ?: Partial < StoreState < T , A , K , F > > , autoExtend = true ) {
23+ constructor ( state ?: Partial < StoreState < M , N , S , A , F > > , autoExtend = true ) {
2224 this . app = state ?. app ;
2325 this . name = ! state || typeof ( state ) === 'string' ? state as any : state . name ;
24- this . initial = state ?. initial || { } as T ;
26+ this . initial = state ?. initial || { } as M ;
27+ this . service = state ?. service as ServiceClass < S > ;
2528 this . fallback = state ?. fallback || [ ] as F [ ] ;
2629 this . options = state ?. options || { } ;
2730 this . reducers = state ?. reducers || [ ] ;
2831 this . actions = state ?. actions as A || [ ] ;
2932
33+ if ( this . service ) {
34+ Object . getOwnPropertyNames ( this . service . prototype )
35+ . forEach ( key => {
36+ const isFunction = key !== 'constructor' && typeof this . service . prototype [ key ] === 'function' ;
37+ const name = key . split ( / (? = [ A - Z ] ) / ) . join ( '_' ) . toUpperCase ( ) ;
38+ const isNew = ! this . actions . find ( x => x . name == name ) ;
39+ if ( isFunction && isNew ) {
40+ const action = new StoreAction ( {
41+ name,
42+ service : this . service ,
43+ method : key as any
44+ } , this . name ) ;
45+ this . actions . push ( action ) ;
46+ }
47+ } ) ;
48+ }
49+
50+
3051 Object . keys ( DefaultActions ) . forEach ( action => {
3152 if ( ! this . actions . find ( x => x . name == action ) ) {
32- this . actions . push ( new StoreAction ( action as any , this . name ) ) ;
53+ this . actions . push ( new StoreAction ( action as any , this . name ) as any ) ;
3354 }
3455 } ) ;
3556
@@ -41,30 +62,20 @@ export class StoreState<T extends any = any, A extends any[] = any[], K extends
4162
4263 }
4364
44- update ( state : any , action : StoreAction ) : T {
45- this . reducers ?. forEach ( reducer => {
46- state = reducer . prePopulate ( this as any , state , action as StoreAction ) ;
47- } ) ;
48- this . reducers ?. forEach ( reducer => {
49- state = reducer . onPopulate ( this as any , state , action as StoreAction ) ;
50- } ) ;
51- this . reducers ?. forEach ( reducer => {
52- state = reducer . postPopulate ( this as any , state , action as StoreAction ) ;
53- } ) ;
54- return state ;
65+ update ( state : any , action : StoreAction ) : M {
66+ this . reducers ?. forEach ( reducer => {
67+ state = reducer . prePopulate ( this as any , state , action as StoreAction ) ;
68+ } ) ;
69+ this . reducers ?. forEach ( reducer => {
70+ state = reducer . onPopulate ( this as any , state , action as StoreAction ) ;
71+ } ) ;
72+ this . reducers ?. forEach ( reducer => {
73+ state = reducer . postPopulate ( this as any , state , action as StoreAction ) ;
74+ } ) ;
75+ return state ;
5576 }
5677
5778 provideState ( ) : Provider [ ] {
5879 return provideStoreStates ( [ this ] , { app : this . app } )
5980 }
60-
61- private addAction ( ) {
62-
63- }
64- private addReducer ( reducer : any ) {
65- if ( ! this . reducers . find ( x => x instanceof reducer ) ) {
66- this . reducers . push ( new reducer ( ) ) ;
67- }
68-
69- }
7081}
0 commit comments