-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
330 lines (282 loc) · 9.3 KB
/
index.js
File metadata and controls
330 lines (282 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
var { cst, getState, setState, appendState, ccContext, configure } = require('concent');
var pluginName = 'loading';
var module2trueLoadingCount = {};
var moduleAndFnName2isAsyncFn = {};
var fnLoading = true;// if false, plugin will not record loading status for fn, only record loading status for module
var onlyForAsync = false;// if true, will only change loading status while call async&generator function
var enqueue = true;// if false, every fn call will set loading status immediately, not batch then and set then until ** ms later
var excludeModules = [];
var excludeFns = [];
var includeFns = [];
/**
* see: https://github.com/concentjs/concent/issues/59
*/
var onlySourceCallTriggerLoading = true;
var invokeCallNoLoading = true;
var toExport = module.exports = {};
function isGenerator(obj) {
return 'function' == typeof obj.next && 'function' == typeof obj.throw;
}
function isGeneratorFunction(obj) {
var constructor = obj.constructor;
if (!constructor) return false;
if ('GeneratorFunction' === constructor.name || 'GeneratorFunction' === constructor.displayName) return true;
return isGenerator(constructor.prototype);
}
function isAsyncFunction(module, fn) {
var key = module + '/' + fn.name;
var isAsync = moduleAndFnName2isAsyncFn[key];
if (isAsync !== undefined) {//返回缓存的结果
return isAsync;
}
isAsync = Object.prototype.toString.call(fn) === '[object AsyncFunction]';
if (isAsync === true) {
moduleAndFnName2isAsyncFn[key] = true;
return true;
}
//有可能成降级编译成 __awaiter格式的了
var fnStr = fn.toString();
if (fnStr.indexOf('__awaiter') > 0) {
moduleAndFnName2isAsyncFn[key] = true;
return true;
}
if (isGeneratorFunction(fn)) {
moduleAndFnName2isAsyncFn[key] = true;
return true;
}
moduleAndFnName2isAsyncFn[key] = false;
return false;
}
function _makeFnLoadingState(reducerMod) {
var state = {};
var _module2fnNames = ccContext.reducer._module2fnNames;
if (reducerMod) {
if (excludeModules.includes(reducerMod)) {
return null;
}
var fullFnNames = _module2fnNames[reducerMod];
if (fullFnNames) {
fullFnNames.forEach(function (name) {
state[reducerMod + '/' + name] = false;
});
}
state[reducerMod] = false;
module2trueLoadingCount[reducerMod] = 0;
return state;
}
Object.keys(_module2fnNames).forEach(function (reducerMod) {
if (excludeModules.includes(reducerMod)) {
return;
}
const fnNames = _module2fnNames[reducerMod];
fnNames.forEach(function (name) {
state[reducerMod + '/' + name] = false;
});
state[reducerMod] = false;
module2trueLoadingCount[reducerMod] = 0;
});
return state;
}
var latestLoading = true;
var enqueuedState = {};
var timer = 0;
function _commitEnqueuedLoadingStatus() {
var moduledFnKeys = Object.keys(enqueuedState);
var len = moduledFnKeys.length;
if (len > 0) {
var fullStateToSet = {};
for (var i = 0; i < len; i++) {
var moduledFnKey = moduledFnKeys[i];
var loading = enqueuedState[moduledFnKey];
var ret = moduledFnKey.split('/');
var module = ret[0];
var fnName = ret[1];
_makeFnLoadingStateToSet(module, fnName, loading, fullStateToSet);
}
setState(pluginName, fullStateToSet);
enqueuedState = {};
}
}
function _enqueueLoadingStatus(fnKey, loading) {
if (loading !== latestLoading) {
_commitEnqueuedLoadingStatus();
}
enqueuedState[fnKey] = loading;
latestLoading = loading;
clearTimeout(timer);
timer = setTimeout(function () {
_commitEnqueuedLoadingStatus();
}, 190);
}
function _makeFnLoadingStateToSet(module, fnName, loading, toSetObj) {
var key = module + '/' + fnName;
var moduleKey = module;
var toSet = toSetObj || {};
toSet[key] = loading;
var newCount;
var count = module2trueLoadingCount[module];
if (loading === true) {
newCount = count + 1;
module2trueLoadingCount[module] = newCount;
} else {
newCount = count - 1;
if (newCount >= 0) {
module2trueLoadingCount[module] = newCount;
}
}
var pluginState = getState(pluginName);
if (newCount > 0 && pluginState[moduleKey] !== true) {
toSet[moduleKey] = true;
}
if (newCount === 0 && pluginState[moduleKey] === true) {
toSet[moduleKey] = false;
}
return toSet;
}
function setFnLoadingStatus(module, fnName, loading) {
var key = module + '/' + fnName;
if (enqueue !== true) {
var toSet = _makeFnLoadingStateToSet(module, fnName, loading);
setState(pluginName, toSet);
return;
}
var pluginState = getState(pluginName);
var prevLoadingStatus = pluginState[key];
if (loading === true) {
if (prevLoadingStatus !== true) {
_enqueueLoadingStatus(key, true);
};
} else {
//不检查prevLoadingStatus(来自于store的值很可能有还未变化,
//因为如果时间够短,可能enqueuedState里有一批为true的loading还未提交),
//直接触发_enqueueLoadingStatus
//此时_enqueueLoadingStatus会触发这批true被提交
_enqueueLoadingStatus(key, false);
}
}
function setLoadingTrue(module, fnName) {
if (fnLoading === true) {
setFnLoadingStatus(module, fnName, true);
return;
}
var count = module2trueLoadingCount[module];
module2trueLoadingCount[module] = count + 1;
var pluginState = getState(pluginName);
//不为true时,才通知concent变成true
if (pluginState[module] !== true) {
var toSet = {};
toSet[module] = true;
setState(pluginName, toSet);
}
}
function setLoadingFalse(module, fnName) {
if (fnLoading === true) {
setFnLoadingStatus(module, fnName, false);
return;
}
var count = module2trueLoadingCount[module];
var newCount = count - 1;
if (newCount < 0) newCount = 0;
module2trueLoadingCount[module] = newCount;
if (newCount === 0) {
var pluginState = getState(pluginName);
if (pluginState[module] !== false) {
var toSet = {};
toSet[module] = false;
setState(pluginName, toSet);
}
}
}
/** concent启动时会调用一次插件的install接口 */
toExport.install = function (on) {
var state = {};
if (fnLoading) {
state = _makeFnLoadingState();
} else {
var moduleName2stateKeys = ccContext.moduleName2stateKeys;
Object.keys(moduleName2stateKeys).forEach(function (mod) {
state[mod] = false;
module2trueLoadingCount[mod] = 0;
});
}
// 将loading配置成concent的模块
configure(pluginName, { state });
on([cst.SIG_FN_START, cst.SIG_FN_END, cst.SIG_FN_ERR, cst.SIG_FN_QUIT], function (data) {
var payload = data.payload;
var sig = data.sig;
var fn = payload.fn;
if (!fn) return; // 有可能非reducer调用
if (payload.calledBy == 'invoke') return; // invoke调用,无loading特效
if (!payload.isSourceCall) return; // 非源头触发的调用,无loading特效
var module = payload.module;
if (excludeModules.includes(module)) {
return;
}
var fnName = fn.__fnName || fn.name;
var fnKey = module + '/' + fnName;
// 配置了可以触发loading函数则优先判断 fnKey 是否在这些函数范围内,此时 excludeFns 无效
if (includeFns.length > 0) {
if (!includeFns.includes(fnKey)) {
return;
}
} else if (excludeFns.includes(fnKey)) {
return;
}
if (cst.SIG_FN_START === sig) {
if (onlyForAsync === true) {//处于只有async函数才需要有loading的工作模式
if (isAsyncFunction(module, fn)) {
setLoadingTrue(module, fnName);
}
return;
}
setLoadingTrue(module, fnName);
return;
}
if ([cst.SIG_FN_END, cst.SIG_FN_ERR, cst.SIG_FN_QUIT].indexOf(sig) !== -1) {
if (onlyForAsync === true) {
if (isAsyncFunction(module, fn)) {
setLoadingFalse(module, fnName);
}
return;
}
setLoadingFalse(module, fnName);
}
});
on(cst.SIG_MODULE_CONFIGURED, function (data) {
var newModule = data.payload;
var toSet = {};
if (fnLoading) {
toSet = _makeFnLoadingState(newModule);
if (toSet) appendState(pluginName, toSet);
return;
}
if (excludeModules.includes(newModule)) {
return;
}
toSet[newModule] = false;
appendState(pluginName, toSet);
module2trueLoadingCount[newModule] = 0;
})
return { name: pluginName }
}
toExport.setConf = function (/** @type {import('./types').IConfig} */conf) {
if (conf) {
var _fnLoading = conf.fnLoading;
var _onlyForAsync = conf.onlyForAsync;
var _enqueue = conf.enqueue;
var _excludeModules = conf.excludeModules;
var _excludeFns = conf.excludeFns;
var _includeFns = conf.includeFns;
var _onlySourceCallTriggerLoading = conf.onlySourceCallTriggerLoading;
var _invokeCallNoLoading = conf.invokeCallNoLoading;
if (_fnLoading !== undefined) fnLoading = _fnLoading;
if (_onlyForAsync !== undefined) onlyForAsync = _onlyForAsync;
if (_enqueue !== undefined) enqueue = _enqueue;
if (Array.isArray(_excludeModules)) excludeModules = _excludeModules;
if (Array.isArray(_excludeFns)) excludeFns = _excludeFns;
if (Array.isArray(_includeFns)) includeFns = _includeFns;
if (_onlySourceCallTriggerLoading !== undefined) onlySourceCallTriggerLoading = _onlySourceCallTriggerLoading;
if (_invokeCallNoLoading !== undefined) invokeCallNoLoading = _invokeCallNoLoading;
}
}
toExport.default = toExport;