@flowlist/js-core 1.7.6 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -1
- package/dist/js-core.es.js +592 -1
- package/dist/js-core.umd.js +1 -1
- package/package.json +17 -16
- package/src/actions.ts +31 -51
- package/src/setters.ts +24 -38
- package/src/types.ts +2 -22
- package/src/utils.ts +10 -5
- package/dist/src/actions.d.ts +0 -5
- package/dist/src/enum.d.ts +0 -32
- package/dist/src/index.d.ts +0 -4
- package/dist/src/setters.d.ts +0 -3
- package/dist/src/types.d.ts +0 -117
- package/dist/src/utils.d.ts +0 -58
- package/dist/tests/unit/api.d.ts +0 -68
- package/dist/tests/unit/env.d.ts +0 -20
- package/dist/tests/unit/item-factory.d.ts +0 -23
- package/src/index.ts +0 -4
package/dist/index.d.ts
CHANGED
package/dist/js-core.es.js
CHANGED
|
@@ -1 +1,592 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
const FETCH_TYPE_ARRAY = ["jump", "sinceId", "page", "seenIds", "auto"];
|
|
21
|
+
var ENUM = {
|
|
22
|
+
SETTER_TYPE: {
|
|
23
|
+
RESET: 0,
|
|
24
|
+
MERGE: 1
|
|
25
|
+
},
|
|
26
|
+
FETCH_TYPE_ARRAY,
|
|
27
|
+
FETCH_TYPE: {
|
|
28
|
+
PAGINATION: FETCH_TYPE_ARRAY[0],
|
|
29
|
+
SINCE_FIRST_OR_END_ID: FETCH_TYPE_ARRAY[1],
|
|
30
|
+
SCROLL_LOAD_MORE: FETCH_TYPE_ARRAY[2],
|
|
31
|
+
HAS_LOADED_IDS: FETCH_TYPE_ARRAY[3],
|
|
32
|
+
AUTO: FETCH_TYPE_ARRAY[4]
|
|
33
|
+
},
|
|
34
|
+
CHANGE_TYPE: {
|
|
35
|
+
SEARCH_FIELD: "search",
|
|
36
|
+
RESET_FIELD: "reset",
|
|
37
|
+
RESULT_UPDATE_KV: "update",
|
|
38
|
+
RESULT_ADD_AFTER: "push",
|
|
39
|
+
RESULT_ADD_BEFORE: "unshift",
|
|
40
|
+
RESULT_REMOVE_BY_ID: "delete",
|
|
41
|
+
RESULT_INSERT_TO_BEFORE: "insert-before",
|
|
42
|
+
RESULT_INSERT_TO_AFTER: "insert-after",
|
|
43
|
+
RESULT_LIST_MERGE: "patch",
|
|
44
|
+
RESULT_ITEM_MERGE: "merge"
|
|
45
|
+
},
|
|
46
|
+
FIELD_DATA: {
|
|
47
|
+
RESULT_KEY: "result",
|
|
48
|
+
EXTRA_KEY: "extra"
|
|
49
|
+
},
|
|
50
|
+
DEFAULT_UNIQUE_KEY_NAME: "id"
|
|
51
|
+
};
|
|
52
|
+
const isObjectResult = (data) => {
|
|
53
|
+
return data.result === void 0;
|
|
54
|
+
};
|
|
55
|
+
const generateDefaultField = (opts = {}) => __spreadValues(__spreadValues({}, {
|
|
56
|
+
result: [],
|
|
57
|
+
noMore: false,
|
|
58
|
+
nothing: false,
|
|
59
|
+
loading: true,
|
|
60
|
+
error: null,
|
|
61
|
+
extra: null,
|
|
62
|
+
fetched: false,
|
|
63
|
+
page: 0,
|
|
64
|
+
total: 0
|
|
65
|
+
}), opts);
|
|
66
|
+
const generateFieldName = ({
|
|
67
|
+
func,
|
|
68
|
+
type,
|
|
69
|
+
query = {}
|
|
70
|
+
}) => {
|
|
71
|
+
func = typeof func === "string" ? func : `api-${Math.random().toString(36).substring(2)}`;
|
|
72
|
+
type = type || "auto";
|
|
73
|
+
let result = `${func}-${type}`;
|
|
74
|
+
Object.keys(query).filter((_) => !~["undefined", "object", "function"].indexOf(typeof query[_]) && !~[
|
|
75
|
+
"page",
|
|
76
|
+
"is_up",
|
|
77
|
+
"since_id",
|
|
78
|
+
"seen_ids",
|
|
79
|
+
"__refresh__",
|
|
80
|
+
"__reload__"
|
|
81
|
+
].indexOf(_)).sort().forEach((key) => {
|
|
82
|
+
result += `-${key}-${query[key]}`;
|
|
83
|
+
});
|
|
84
|
+
return result;
|
|
85
|
+
};
|
|
86
|
+
const getObjectDeepValue = (field, keys) => {
|
|
87
|
+
if (!keys) {
|
|
88
|
+
return field || "";
|
|
89
|
+
}
|
|
90
|
+
let result = field || "";
|
|
91
|
+
const keysArr = isArray(keys) ? keys : keys.split(".");
|
|
92
|
+
keysArr.forEach((key) => {
|
|
93
|
+
result = result[key];
|
|
94
|
+
});
|
|
95
|
+
return result || "";
|
|
96
|
+
};
|
|
97
|
+
const updateObjectDeepValue = (field, changeKey, value) => {
|
|
98
|
+
if (/\./.test(changeKey)) {
|
|
99
|
+
const keys = changeKey.split(".");
|
|
100
|
+
const prefix = keys.pop();
|
|
101
|
+
let result = field;
|
|
102
|
+
keys.forEach((key) => {
|
|
103
|
+
result = result[key];
|
|
104
|
+
});
|
|
105
|
+
result[prefix] = value;
|
|
106
|
+
} else {
|
|
107
|
+
field[changeKey] = value;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const searchValueByKey = (result, id, key) => {
|
|
111
|
+
if (isArray(result)) {
|
|
112
|
+
const index = computeMatchedItemIndex(id, result, key);
|
|
113
|
+
if (index < 0) {
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
return result[index];
|
|
117
|
+
}
|
|
118
|
+
return result[id];
|
|
119
|
+
};
|
|
120
|
+
const computeMatchedItemIndex = (itemId, fieldArr, changingKey) => {
|
|
121
|
+
let s = -1;
|
|
122
|
+
for (let i = 0; i < fieldArr.length; i++) {
|
|
123
|
+
if (getObjectDeepValue(fieldArr[i], changingKey.toString()).toString() === (itemId || "").toString()) {
|
|
124
|
+
s = i;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return s;
|
|
129
|
+
};
|
|
130
|
+
const combineArrayData = (fieldArray, value, changingKey) => {
|
|
131
|
+
if (isArray(value)) {
|
|
132
|
+
value.forEach((col) => {
|
|
133
|
+
const stringifyId = getObjectDeepValue(col, changingKey).toString();
|
|
134
|
+
fieldArray.forEach((item, index) => {
|
|
135
|
+
if (getObjectDeepValue(item, changingKey).toString() === stringifyId) {
|
|
136
|
+
fieldArray[index] = __spreadValues(__spreadValues({}, item), col);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
} else {
|
|
141
|
+
Object.keys(value).forEach((uniqueId) => {
|
|
142
|
+
const stringifyId = (uniqueId || "").toString();
|
|
143
|
+
fieldArray.forEach((item, index) => {
|
|
144
|
+
if (getObjectDeepValue(item, changingKey).toString() === stringifyId) {
|
|
145
|
+
fieldArray[index] = __spreadValues(__spreadValues({}, item), value[uniqueId]);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const isArray = (data) => Object.prototype.toString.call(data) === "[object Array]";
|
|
152
|
+
const setReactivityField = (field, key, value, type, insertBefore) => {
|
|
153
|
+
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
154
|
+
field[key] = value;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (isArray(value)) {
|
|
158
|
+
field[key] = insertBefore ? value.concat(field[key] || []) : (field[key] || []).concat(value);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (key !== ENUM.FIELD_DATA.RESULT_KEY) {
|
|
162
|
+
field[key] = value;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (isArray(field[key])) {
|
|
166
|
+
field[key] = {};
|
|
167
|
+
}
|
|
168
|
+
Object.keys(value).forEach((subKey) => {
|
|
169
|
+
field[key][subKey] = field[key][subKey] ? insertBefore ? value[subKey].concat(field[key][subKey]) : field[key][subKey].concat(value[subKey]) : value[subKey];
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
const computeResultLength = (data) => {
|
|
173
|
+
let result = 0;
|
|
174
|
+
if (isArray(data)) {
|
|
175
|
+
result = data.length;
|
|
176
|
+
} else {
|
|
177
|
+
Object.keys(data).forEach((key) => {
|
|
178
|
+
result += data[key].length;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
};
|
|
183
|
+
const generateRequestParams = ({
|
|
184
|
+
field,
|
|
185
|
+
uniqueKey,
|
|
186
|
+
query = {},
|
|
187
|
+
type
|
|
188
|
+
}) => {
|
|
189
|
+
const result = {};
|
|
190
|
+
if (field.fetched) {
|
|
191
|
+
const changing = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME;
|
|
192
|
+
if (type === ENUM.FETCH_TYPE.AUTO) {
|
|
193
|
+
result.seen_ids = field.result.map((_) => getObjectDeepValue(_, changing)).join(",");
|
|
194
|
+
result.since_id = getObjectDeepValue(field.result[query.is_up ? 0 : field.result.length - 1], changing);
|
|
195
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
196
|
+
result.page = query.page || field.page + 1;
|
|
197
|
+
} else if (type === ENUM.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
198
|
+
result.seen_ids = field.result.map((_) => getObjectDeepValue(_, changing)).join(",");
|
|
199
|
+
} else if (type === ENUM.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
200
|
+
result.since_id = getObjectDeepValue(field.result[query.is_up ? 0 : field.result.length - 1], changing);
|
|
201
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
202
|
+
} else if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
203
|
+
result.page = query.page;
|
|
204
|
+
} else if (type === ENUM.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
205
|
+
result.page = field.page + 1;
|
|
206
|
+
}
|
|
207
|
+
} else {
|
|
208
|
+
if (type === ENUM.FETCH_TYPE.AUTO) {
|
|
209
|
+
result.seen_ids = "";
|
|
210
|
+
result.since_id = query.sinceId || (query.is_up ? 999999999 : 0);
|
|
211
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
212
|
+
result.page = query.page || field.page || 1;
|
|
213
|
+
} else if (type === ENUM.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
214
|
+
result.seen_ids = "";
|
|
215
|
+
} else if (type === ENUM.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
216
|
+
result.since_id = query.sinceId || (query.is_up ? 999999999 : 0);
|
|
217
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
218
|
+
} else if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
219
|
+
result.page = query.page || field.page;
|
|
220
|
+
} else if (type === ENUM.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
221
|
+
result.page = 1;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return __spreadValues(__spreadValues({}, query), result);
|
|
225
|
+
};
|
|
226
|
+
var utils = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
227
|
+
__proto__: null,
|
|
228
|
+
isObjectResult,
|
|
229
|
+
generateDefaultField,
|
|
230
|
+
generateFieldName,
|
|
231
|
+
getObjectDeepValue,
|
|
232
|
+
updateObjectDeepValue,
|
|
233
|
+
searchValueByKey,
|
|
234
|
+
computeMatchedItemIndex,
|
|
235
|
+
combineArrayData,
|
|
236
|
+
isArray,
|
|
237
|
+
setReactivityField,
|
|
238
|
+
computeResultLength,
|
|
239
|
+
generateRequestParams
|
|
240
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
241
|
+
const SET_DATA = ({
|
|
242
|
+
getter,
|
|
243
|
+
setter,
|
|
244
|
+
data,
|
|
245
|
+
fieldName,
|
|
246
|
+
type,
|
|
247
|
+
page,
|
|
248
|
+
insertBefore
|
|
249
|
+
}) => {
|
|
250
|
+
return new Promise((resolve, reject) => {
|
|
251
|
+
const fieldData = getter(fieldName);
|
|
252
|
+
if (!fieldData) {
|
|
253
|
+
reject();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
let result;
|
|
257
|
+
let extra;
|
|
258
|
+
if (isObjectResult(data)) {
|
|
259
|
+
result = data;
|
|
260
|
+
fieldData.nothing = false;
|
|
261
|
+
fieldData.fetched = true;
|
|
262
|
+
fieldData.noMore = true;
|
|
263
|
+
fieldData.page = -1;
|
|
264
|
+
} else {
|
|
265
|
+
result = data.result;
|
|
266
|
+
extra = data.extra;
|
|
267
|
+
const isEmpty = computeResultLength(result) === 0;
|
|
268
|
+
fieldData.nothing = fieldData.fetched ? false : isEmpty;
|
|
269
|
+
fieldData.fetched = true;
|
|
270
|
+
fieldData.total = data.total || 0;
|
|
271
|
+
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
272
|
+
fieldData.noMore = false;
|
|
273
|
+
fieldData.page = +page;
|
|
274
|
+
} else {
|
|
275
|
+
fieldData.noMore = data.no_more || isEmpty;
|
|
276
|
+
fieldData.page = fieldData.page + 1;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
fieldData.loading = false;
|
|
280
|
+
setReactivityField(fieldData, ENUM.FIELD_DATA.RESULT_KEY, result, type, insertBefore);
|
|
281
|
+
extra && setReactivityField(fieldData, ENUM.FIELD_DATA.EXTRA_KEY, extra, type, insertBefore);
|
|
282
|
+
setter({
|
|
283
|
+
key: fieldName,
|
|
284
|
+
type: ENUM.SETTER_TYPE.RESET,
|
|
285
|
+
value: fieldData,
|
|
286
|
+
callback: () => {
|
|
287
|
+
resolve(null);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
const SET_ERROR = ({ setter, fieldName, error }) => {
|
|
293
|
+
setter({
|
|
294
|
+
key: fieldName,
|
|
295
|
+
type: ENUM.SETTER_TYPE.MERGE,
|
|
296
|
+
value: {
|
|
297
|
+
error,
|
|
298
|
+
loading: false
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
const initState = ({
|
|
303
|
+
getter,
|
|
304
|
+
setter,
|
|
305
|
+
func,
|
|
306
|
+
type,
|
|
307
|
+
query,
|
|
308
|
+
opts
|
|
309
|
+
}) => {
|
|
310
|
+
return new Promise((resolve) => {
|
|
311
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
312
|
+
const fieldData = getter(fieldName);
|
|
313
|
+
if (fieldData) {
|
|
314
|
+
resolve(null);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
setter({
|
|
318
|
+
key: fieldName,
|
|
319
|
+
type: ENUM.SETTER_TYPE.RESET,
|
|
320
|
+
value: generateDefaultField(opts),
|
|
321
|
+
callback: () => {
|
|
322
|
+
resolve(null);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
};
|
|
327
|
+
const initData = ({
|
|
328
|
+
getter,
|
|
329
|
+
setter,
|
|
330
|
+
func,
|
|
331
|
+
type,
|
|
332
|
+
query,
|
|
333
|
+
api,
|
|
334
|
+
uniqueKey,
|
|
335
|
+
callback
|
|
336
|
+
}) => new Promise((resolve, reject) => {
|
|
337
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
338
|
+
const fieldData = getter(fieldName);
|
|
339
|
+
const doRefresh = !!(query == null ? void 0 : query.__refresh__);
|
|
340
|
+
const needReset = !!(query == null ? void 0 : query.__reload__);
|
|
341
|
+
if (fieldData && fieldData.error && !doRefresh) {
|
|
342
|
+
return resolve(null);
|
|
343
|
+
}
|
|
344
|
+
if (fieldData && fieldData.loading && fieldData.fetched && !doRefresh && !needReset) {
|
|
345
|
+
return resolve(null);
|
|
346
|
+
}
|
|
347
|
+
const dontFetch = fieldData && fieldData.fetched && !doRefresh;
|
|
348
|
+
if (dontFetch) {
|
|
349
|
+
return resolve(null);
|
|
350
|
+
}
|
|
351
|
+
const params = generateRequestParams({
|
|
352
|
+
field: __spreadProps(__spreadValues({}, fieldData), {
|
|
353
|
+
fetched: false
|
|
354
|
+
}),
|
|
355
|
+
uniqueKey,
|
|
356
|
+
query,
|
|
357
|
+
type
|
|
358
|
+
});
|
|
359
|
+
const getData = () => {
|
|
360
|
+
const loadData = () => new Promise((res) => {
|
|
361
|
+
const getDataFromAPI = () => {
|
|
362
|
+
const funcCaller = typeof func === "string" ? api[func] : func;
|
|
363
|
+
if (!funcCaller) {
|
|
364
|
+
const error = new Error("func is not a function");
|
|
365
|
+
SET_ERROR({ setter, fieldName, error });
|
|
366
|
+
reject(error);
|
|
367
|
+
}
|
|
368
|
+
funcCaller(params).then(res).catch((error) => {
|
|
369
|
+
SET_ERROR({ setter, fieldName, error });
|
|
370
|
+
reject(error);
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
getDataFromAPI();
|
|
374
|
+
});
|
|
375
|
+
loadData().then((data) => {
|
|
376
|
+
const setData = () => {
|
|
377
|
+
SET_DATA({
|
|
378
|
+
getter,
|
|
379
|
+
setter,
|
|
380
|
+
data,
|
|
381
|
+
fieldName,
|
|
382
|
+
type,
|
|
383
|
+
page: params.page || 0,
|
|
384
|
+
insertBefore: false
|
|
385
|
+
}).then(() => {
|
|
386
|
+
if (callback) {
|
|
387
|
+
callback({
|
|
388
|
+
params,
|
|
389
|
+
data,
|
|
390
|
+
refresh: doRefresh
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
resolve(null);
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
if (needReset) {
|
|
397
|
+
setter({
|
|
398
|
+
key: fieldName,
|
|
399
|
+
type: ENUM.SETTER_TYPE.RESET,
|
|
400
|
+
value: generateDefaultField(),
|
|
401
|
+
callback: setData
|
|
402
|
+
});
|
|
403
|
+
} else {
|
|
404
|
+
setData();
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
if (!dontFetch && !needReset) {
|
|
409
|
+
setter({
|
|
410
|
+
key: fieldName,
|
|
411
|
+
type: ENUM.SETTER_TYPE.RESET,
|
|
412
|
+
value: __spreadProps(__spreadValues({}, generateDefaultField()), {
|
|
413
|
+
loading: true,
|
|
414
|
+
error: null
|
|
415
|
+
}),
|
|
416
|
+
callback: getData
|
|
417
|
+
});
|
|
418
|
+
} else {
|
|
419
|
+
getData();
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
const loadMore = ({
|
|
423
|
+
getter,
|
|
424
|
+
setter,
|
|
425
|
+
query,
|
|
426
|
+
type,
|
|
427
|
+
func,
|
|
428
|
+
api,
|
|
429
|
+
uniqueKey,
|
|
430
|
+
errorRetry,
|
|
431
|
+
callback
|
|
432
|
+
}) => new Promise((resolve, reject) => {
|
|
433
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
434
|
+
const fieldData = getter(fieldName);
|
|
435
|
+
if (!fieldData) {
|
|
436
|
+
return resolve(null);
|
|
437
|
+
}
|
|
438
|
+
if (fieldData.loading) {
|
|
439
|
+
return resolve(null);
|
|
440
|
+
}
|
|
441
|
+
if (fieldData.nothing) {
|
|
442
|
+
return resolve(null);
|
|
443
|
+
}
|
|
444
|
+
if (fieldData.noMore && !errorRetry) {
|
|
445
|
+
return resolve(null);
|
|
446
|
+
}
|
|
447
|
+
if (type === ENUM.FETCH_TYPE.PAGINATION && query && +query.page === fieldData.page) {
|
|
448
|
+
return resolve(null);
|
|
449
|
+
}
|
|
450
|
+
let loadingState;
|
|
451
|
+
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
452
|
+
loadingState = {
|
|
453
|
+
loading: true,
|
|
454
|
+
error: null,
|
|
455
|
+
[ENUM.FIELD_DATA.RESULT_KEY]: [],
|
|
456
|
+
[ENUM.FIELD_DATA.EXTRA_KEY]: null
|
|
457
|
+
};
|
|
458
|
+
} else {
|
|
459
|
+
loadingState = {
|
|
460
|
+
loading: true,
|
|
461
|
+
error: null
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
const params = generateRequestParams({
|
|
465
|
+
field: fieldData,
|
|
466
|
+
uniqueKey,
|
|
467
|
+
query,
|
|
468
|
+
type
|
|
469
|
+
});
|
|
470
|
+
params[ENUM.FIELD_DATA.EXTRA_KEY] = fieldData[ENUM.FIELD_DATA.EXTRA_KEY];
|
|
471
|
+
const getData = () => {
|
|
472
|
+
const funcCaller = typeof func === "string" ? api[func] : func;
|
|
473
|
+
if (!funcCaller) {
|
|
474
|
+
const error = new Error("func is not a function");
|
|
475
|
+
SET_ERROR({ setter, fieldName, error });
|
|
476
|
+
reject(error);
|
|
477
|
+
}
|
|
478
|
+
funcCaller(params).then((data) => {
|
|
479
|
+
SET_DATA({
|
|
480
|
+
getter,
|
|
481
|
+
setter,
|
|
482
|
+
data,
|
|
483
|
+
fieldName,
|
|
484
|
+
type,
|
|
485
|
+
page: params.page || 0,
|
|
486
|
+
insertBefore: !!(query == null ? void 0 : query.is_up)
|
|
487
|
+
}).then(() => {
|
|
488
|
+
if (callback) {
|
|
489
|
+
callback({
|
|
490
|
+
params,
|
|
491
|
+
data,
|
|
492
|
+
refresh: false
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
resolve(null);
|
|
496
|
+
});
|
|
497
|
+
}).catch((error) => {
|
|
498
|
+
SET_ERROR({ setter, fieldName, error });
|
|
499
|
+
reject(error);
|
|
500
|
+
});
|
|
501
|
+
};
|
|
502
|
+
setter({
|
|
503
|
+
key: fieldName,
|
|
504
|
+
type: ENUM.SETTER_TYPE.MERGE,
|
|
505
|
+
value: loadingState,
|
|
506
|
+
callback: getData
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
const updateState = ({
|
|
510
|
+
getter,
|
|
511
|
+
setter,
|
|
512
|
+
type,
|
|
513
|
+
func,
|
|
514
|
+
query,
|
|
515
|
+
method,
|
|
516
|
+
value,
|
|
517
|
+
id,
|
|
518
|
+
uniqueKey,
|
|
519
|
+
changeKey
|
|
520
|
+
}) => {
|
|
521
|
+
return new Promise((resolve, reject) => {
|
|
522
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
523
|
+
const fieldData = getter(fieldName);
|
|
524
|
+
if (!fieldData) {
|
|
525
|
+
reject();
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (fieldData.page === -1) {
|
|
529
|
+
resolve(null);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
const _id = id || "";
|
|
533
|
+
const _uniqueKey = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME;
|
|
534
|
+
const _changeKey = changeKey || ENUM.FIELD_DATA.RESULT_KEY;
|
|
535
|
+
const beforeLength = computeResultLength(fieldData[ENUM.FIELD_DATA.RESULT_KEY]);
|
|
536
|
+
if (method === ENUM.CHANGE_TYPE.SEARCH_FIELD) {
|
|
537
|
+
resolve(searchValueByKey(fieldData[ENUM.FIELD_DATA.RESULT_KEY], _id, _uniqueKey));
|
|
538
|
+
} else if (method === ENUM.CHANGE_TYPE.RESULT_UPDATE_KV) {
|
|
539
|
+
const matchedIndex = computeMatchedItemIndex(_id, fieldData[ENUM.FIELD_DATA.RESULT_KEY], _uniqueKey);
|
|
540
|
+
updateObjectDeepValue(fieldData[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex], _changeKey, value);
|
|
541
|
+
} else if (method === ENUM.CHANGE_TYPE.RESULT_ITEM_MERGE) {
|
|
542
|
+
const matchedIndex = computeMatchedItemIndex(_id, fieldData[ENUM.FIELD_DATA.RESULT_KEY], _uniqueKey);
|
|
543
|
+
fieldData[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex] = __spreadValues(__spreadValues({}, fieldData[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex]), value);
|
|
544
|
+
} else if (method === ENUM.CHANGE_TYPE.RESET_FIELD) {
|
|
545
|
+
updateObjectDeepValue(fieldData, _changeKey, value);
|
|
546
|
+
} else {
|
|
547
|
+
let modifyValue = getObjectDeepValue(fieldData, _changeKey);
|
|
548
|
+
const matchedIndex = computeMatchedItemIndex(_id, modifyValue, _uniqueKey);
|
|
549
|
+
switch (method) {
|
|
550
|
+
case ENUM.CHANGE_TYPE.RESULT_ADD_AFTER:
|
|
551
|
+
isArray(value) ? modifyValue = modifyValue.concat(value) : modifyValue.push(value);
|
|
552
|
+
break;
|
|
553
|
+
case ENUM.CHANGE_TYPE.RESULT_ADD_BEFORE:
|
|
554
|
+
isArray(value) ? modifyValue = value.concat(modifyValue) : modifyValue.unshift(value);
|
|
555
|
+
break;
|
|
556
|
+
case ENUM.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
|
|
557
|
+
if (matchedIndex >= 0) {
|
|
558
|
+
modifyValue.splice(matchedIndex, 1);
|
|
559
|
+
} else if (isArray(_id)) {
|
|
560
|
+
modifyValue = modifyValue.filter((_) => _id.indexOf(_[_uniqueKey]) === -1);
|
|
561
|
+
}
|
|
562
|
+
break;
|
|
563
|
+
case ENUM.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
|
|
564
|
+
if (matchedIndex >= 0) {
|
|
565
|
+
modifyValue.splice(matchedIndex, 0, value);
|
|
566
|
+
}
|
|
567
|
+
break;
|
|
568
|
+
case ENUM.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
|
|
569
|
+
if (matchedIndex >= 0) {
|
|
570
|
+
modifyValue.splice(matchedIndex + 1, 0, value);
|
|
571
|
+
}
|
|
572
|
+
break;
|
|
573
|
+
case ENUM.CHANGE_TYPE.RESULT_LIST_MERGE:
|
|
574
|
+
combineArrayData(modifyValue, value, _uniqueKey);
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
fieldData[_changeKey] = modifyValue;
|
|
578
|
+
}
|
|
579
|
+
const afterLength = computeResultLength(fieldData[ENUM.FIELD_DATA.RESULT_KEY]);
|
|
580
|
+
fieldData.total = fieldData.total + afterLength - beforeLength;
|
|
581
|
+
fieldData.nothing = afterLength === 0;
|
|
582
|
+
setter({
|
|
583
|
+
key: fieldName,
|
|
584
|
+
type: ENUM.SETTER_TYPE.MERGE,
|
|
585
|
+
value: fieldData,
|
|
586
|
+
callback: () => {
|
|
587
|
+
resolve(null);
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
};
|
|
592
|
+
export { ENUM, initData, initState, loadMore, updateState, utils };
|