1+ import produce from "immer" ;
2+
13export default function loading (
24 config = {
35 name : 'loading' ,
46 }
57) {
68 return function init ( {
7- onModel $,
9+ onModelBeforeCreate $,
810 onEpicStart$,
911 onEpicEnd$,
1012 onEpicCancel$,
@@ -14,40 +16,71 @@ export default function loading(
1416 const _model = {
1517 name : config . name ,
1618 state : {
17- global : 0 ,
1819 epics : { } ,
1920 } ,
2021 reducers : {
2122 init ( state , action ) {
22- state . epics = action . epics ;
23-
24- return state ;
23+ return produce ( state , draft => {
24+ draft . epics = action . epics ;
25+ } ) ;
2526 } ,
2627 epicStart ( state , action ) {
27- const epicCounterKey = `${ action . epic } Counter` ;
28- let epicCounter = state . epics [ action . model ] [ epicCounterKey ] + action . loading ;
29-
30- state . epics [ action . model ] [ epicCounterKey ] = epicCounter ;
31- state . epics [ action . model ] [ action . epic ] = epicCounter > 0 ;
28+ return produce ( state , draft => {
29+ const epicCounterKey = `${ action . epic } Counter` ;
30+ let epicCounter = draft . epics [ action . model ] [ epicCounterKey ] + action . loading ;
3231
33- return state ;
32+ draft . epics [ action . model ] [ epicCounterKey ] = epicCounter ;
33+ draft . epics [ action . model ] [ action . epic ] = epicCounter > 0 ;
34+ } ) ;
3435 } ,
3536 epicStop ( state , action ) {
36- const epicCounterKey = `${ action . epic } Counter` ;
37-
38- state . epics [ action . model ] [ epicCounterKey ] = 0 ;
39- state . epics [ action . model ] [ action . epic ] = false ;
40-
41- return state ;
37+ return produce ( state , draft => {
38+ const epicCounterKey = `${ action . epic } Counter` ;
39+ draft . epics [ action . model ] [ epicCounterKey ] = 0 ;
40+ draft . epics [ action . model ] [ action . epic ] = false ;
41+ } ) ;
4242 } ,
4343 } ,
4444 } ;
4545 this . model ( _model ) ;
4646 this . stream ( 'loading' ) . subscribe ( ) ;
47+
48+ onModelBeforeCreate$ . subscribe ( ( { model } ) => {
49+ if (
50+ typeof model . state !== 'object' ||
51+ ! model . epics ||
52+ model . state . loading !== void 0
53+ ) return ;
54+
55+ const loading = { } ;
56+ Object . keys ( model . epics ) . forEach ( epic => {
57+ loading [ `${ epic } Counter` ] = 0 ;
58+ loading [ epic ] = false ;
59+ } ) ;
60+
61+ model . state . loading = loading ;
62+ model . reducers . loadingStart = loadingStart ;
63+ model . reducers . loadingEnd = loadingEnd ;
64+
65+ function loadingStart ( state , { payload : { epic } } ) {
66+ return produce ( state , draft => {
67+ const epicCounterKey = `${ epic } Counter` ;
68+ const epicCounter = draft . loading [ epicCounterKey ] + 1
69+ draft . loading [ epicCounterKey ] = epicCounter ;
70+ draft . loading [ epic ] = epicCounter > 0 ;
71+ } ) ;
72+ }
73+
74+ function loadingEnd ( state , { payload : { epic } } ) {
75+ return produce ( state , draft => {
76+ draft . loading [ `${ epic } Counter` ] = 0 ;
77+ draft . loading [ epic ] = false ;
78+ } ) ;
79+ }
80+ } ) ;
4781
4882 // hooks
49- onStart$
50- . subscribe ( ( ) => {
83+ onStart$ . subscribe ( ( ) => {
5184 const epics = { } ;
5285 Object . keys ( this . _stream ) . forEach ( ( model ) => {
5386 if ( model === 'loading' ) return ;
@@ -63,47 +96,59 @@ export default function loading(
6396 } ) ;
6497 } ) ;
6598
66- onEpicStart$
67- . subscribe ( data => {
99+ onEpicStart$ . subscribe ( ( { model, epic } ) => {
68100 this . dispatch ( {
69- epic : data . epic ,
101+ model,
102+ epic,
70103 type : 'loading/epicStart' ,
71- model : data . model ,
72104 loading : 1 ,
73105 } ) ;
106+ this . dispatch ( {
107+ type : `${ model } /loadingStart` ,
108+ payload : { epic } ,
109+ } ) ;
74110 } ) ;
75111
76- onEpicEnd$
77- . subscribe ( data => {
112+ onEpicEnd$ . subscribe ( ( { model, epic } ) => {
78113 this . dispatch ( {
79- epic : data . epic ,
114+ model,
115+ epic,
80116 type : 'loading/epicStop' ,
81- model : data . model ,
82117 loading : 0 ,
83118 isEnd : true ,
84119 } ) ;
120+ this . dispatch ( {
121+ type : `${ model } /loadingEnd` ,
122+ payload : { epic } ,
123+ } ) ;
85124 } ) ;
86125
87- onEpicError$
88- . subscribe ( data => {
126+ onEpicError$ . subscribe ( ( { model, epic } ) => {
89127 this . dispatch ( {
90- epic : data . epic ,
128+ model,
129+ epic,
91130 type : 'loading/epicStop' ,
92- model : data . model ,
93131 loading : 0 ,
94132 isError : true ,
95133 } ) ;
134+ this . dispatch ( {
135+ type : `${ model } /loadingEnd` ,
136+ payload : { epic } ,
137+ } ) ;
96138 } ) ;
97139
98- onEpicCancel$
99- . subscribe ( data => {
140+ onEpicCancel$ . subscribe ( ( { model, epic } ) => {
100141 this . dispatch ( {
101- epic : data . epic ,
142+ model,
143+ epic,
102144 type : 'loading/epicStop' ,
103- model : data . model ,
104145 loading : 0 ,
105146 isCancel : true ,
106147 } ) ;
148+ this . dispatch ( {
149+ type : `${ model } /loadingEnd` ,
150+ payload : { epic } ,
151+ } ) ;
107152 } ) ;
108153 } ;
109154} ;
0 commit comments