@@ -20,14 +20,27 @@ const Model = <M extends Models>(models: M, initialState?: Global['State']) => {
2020 Global . AsyncState [ name ] = model . asyncState
2121 } )
2222
23+ const actions = Object . keys ( models ) . reduce (
24+ ( o , modelName ) => ( { ...o , name : getActions ( modelName ) } ) ,
25+ { }
26+ )
27+
2328 Global . withDevTools =
2429 typeof window !== 'undefined' &&
2530 ( window as any ) . __REDUX_DEVTOOLS_EXTENSION__
2631 if ( Global . withDevTools ) {
2732 Global . devTools = ( window as any ) . __REDUX_DEVTOOLS_EXTENSION__
2833 Global . devTools . connect ( )
2934 }
30- return { useStore, getState, getInitialState, getActions, subscribe } as {
35+ return {
36+ actions,
37+ useStore,
38+ getState,
39+ getInitialState,
40+ getActions,
41+ subscribe,
42+ unsubscribe
43+ } as {
3144 useStore : < K extends keyof M > (
3245 name : K ,
3346 depActions ?: ( keyof Get < M [ K ] , 'actions' > ) [ ]
@@ -39,25 +52,59 @@ const Model = <M extends Models>(models: M, initialState?: Global['State']) => {
3952 getInitialState : typeof getInitialState
4053 subscribe : < K extends keyof M > (
4154 modelName : K ,
42- actionName : keyof Get < M [ K ] , 'actions' > | keyof Get < M [ K ] , 'actions' > ,
55+ actionName : keyof Get < M [ K ] , 'actions' > | ( keyof Get < M [ K ] , 'actions' > ) [ ] ,
4356 callback : Function
4457 ) => void
58+ unsubscribe : < K extends keyof M > (
59+ modelName : K ,
60+ actionName : keyof Get < M [ K ] , 'actions' > | ( keyof Get < M [ K ] , 'actions' > ) [ ]
61+ ) => void
62+ actions : {
63+ [ K in keyof M ] : Readonly < getConsumerActionsType < Get < M [ K ] , 'actions' > > >
64+ }
4565 }
4666}
4767
68+ const unsubscribe = ( modelName : string , actions : string | string [ ] ) => {
69+ subscribe ( modelName , actions , undefined )
70+ }
71+
4872const subscribe = (
4973 modelName : string ,
50- actionName : string ,
51- callback : Function
74+ actions : string | string [ ] ,
75+ callback ? : Function
5276) => {
53- Global . subscriptions [ `${ modelName } _${ actionName } ` ] = callback
77+ if ( Array . isArray ( actions ) ) {
78+ actions . forEach ( actionName => {
79+ if ( ! Global . subscriptions [ `${ modelName } _${ actionName } ` ] ) {
80+ Global . subscriptions [ `${ modelName } _${ actionName } ` ] = [ ]
81+ }
82+ if ( callback ) {
83+ Global . subscriptions [ `${ modelName } _${ actionName } ` ] . push ( callback )
84+ } else {
85+ Global . subscriptions [ `${ modelName } _${ actionName } ` ] = [ ]
86+ }
87+ } )
88+ } else {
89+ if ( ! Global . subscriptions [ `${ modelName } _${ actions } ` ] ) {
90+ Global . subscriptions [ `${ modelName } _${ actions } ` ] = [ ]
91+ }
92+ if ( callback ) {
93+ Global . subscriptions [ `${ modelName } _${ actions } ` ] . push ( callback )
94+ } else {
95+ Global . subscriptions [ `${ modelName } _${ actions } ` ] = [ ]
96+ }
97+ }
5498}
5599
56100const getState = ( modelName : keyof typeof Global . State ) => {
57101 return Global . State [ modelName ]
58102}
59103
60- const getActions = ( modelName : string , baseContext : Partial < Context > ) => {
104+ const getActions = (
105+ modelName : string ,
106+ baseContext : Partial < Context > = { type : 'outer' }
107+ ) => {
61108 const updaters : any = { }
62109 Object . entries ( Global . Actions [ modelName ] ) . forEach (
63110 ( [ key , action ] ) =>
0 commit comments