@flowlist/js-core 2.3.1 → 3.0.1
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/README.md +74 -102
- package/dist/index.d.mts +263 -0
- package/dist/index.d.ts +263 -4
- package/dist/index.global.js +775 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +770 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +763 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +33 -30
- package/dist/js-core.es.js +0 -582
- package/dist/js-core.umd.js +0 -1
- package/src/actions.ts +0 -388
- package/src/enum.ts +0 -33
- package/src/setters.ts +0 -91
- package/src/types.ts +0 -122
- package/src/utils.ts +0 -325
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
var FlowList = (function (exports) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/utils.ts
|
|
11
|
+
var utils_exports = {};
|
|
12
|
+
__export(utils_exports, {
|
|
13
|
+
combineArrayData: () => combineArrayData,
|
|
14
|
+
computeMatchedItemIndex: () => computeMatchedItemIndex,
|
|
15
|
+
computeResultLength: () => computeResultLength,
|
|
16
|
+
generateDefaultField: () => generateDefaultField,
|
|
17
|
+
generateFieldName: () => generateFieldName,
|
|
18
|
+
generateRequestParams: () => generateRequestParams,
|
|
19
|
+
getObjectDeepValue: () => getObjectDeepValue,
|
|
20
|
+
isArray: () => isArray,
|
|
21
|
+
isObjectResult: () => isObjectResult,
|
|
22
|
+
searchValueByKey: () => searchValueByKey,
|
|
23
|
+
setReactivityField: () => setReactivityField,
|
|
24
|
+
updateObjectDeepValue: () => updateObjectDeepValue
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// src/enum.ts
|
|
28
|
+
var FETCH_TYPE_ARRAY = ["jump", "sinceId", "page", "seenIds", "auto"];
|
|
29
|
+
var enum_default = {
|
|
30
|
+
SETTER_TYPE: {
|
|
31
|
+
RESET: 0,
|
|
32
|
+
MERGE: 1
|
|
33
|
+
},
|
|
34
|
+
FETCH_TYPE_ARRAY,
|
|
35
|
+
FETCH_TYPE: {
|
|
36
|
+
PAGINATION: FETCH_TYPE_ARRAY[0],
|
|
37
|
+
SINCE_FIRST_OR_END_ID: FETCH_TYPE_ARRAY[1],
|
|
38
|
+
SCROLL_LOAD_MORE: FETCH_TYPE_ARRAY[2],
|
|
39
|
+
HAS_LOADED_IDS: FETCH_TYPE_ARRAY[3],
|
|
40
|
+
AUTO: FETCH_TYPE_ARRAY[4]
|
|
41
|
+
},
|
|
42
|
+
CHANGE_TYPE: {
|
|
43
|
+
SEARCH_FIELD: "search",
|
|
44
|
+
RESET_FIELD: "reset",
|
|
45
|
+
RESULT_UPDATE_KV: "update",
|
|
46
|
+
RESULT_ADD_AFTER: "push",
|
|
47
|
+
RESULT_ADD_BEFORE: "unshift",
|
|
48
|
+
RESULT_REMOVE_BY_ID: "delete",
|
|
49
|
+
RESULT_INSERT_TO_BEFORE: "insert-before",
|
|
50
|
+
RESULT_INSERT_TO_AFTER: "insert-after",
|
|
51
|
+
RESULT_LIST_MERGE: "patch",
|
|
52
|
+
RESULT_ITEM_MERGE: "merge"
|
|
53
|
+
},
|
|
54
|
+
FIELD_DATA: {
|
|
55
|
+
RESULT_KEY: "result",
|
|
56
|
+
EXTRA_KEY: "extra"
|
|
57
|
+
},
|
|
58
|
+
DEFAULT_UNIQUE_KEY_NAME: "id"
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/utils.ts
|
|
62
|
+
var isObjectResult = (data) => {
|
|
63
|
+
if (typeof data !== "object" || data === null) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return !Object.prototype.hasOwnProperty.call(data, "result");
|
|
67
|
+
};
|
|
68
|
+
var generateDefaultField = (opts = {}) => ({
|
|
69
|
+
...{
|
|
70
|
+
result: [],
|
|
71
|
+
// 用户需要确保 T 是兼容的类型,例如 T extends unknown[]
|
|
72
|
+
noMore: false,
|
|
73
|
+
nothing: false,
|
|
74
|
+
loading: false,
|
|
75
|
+
error: null,
|
|
76
|
+
extra: null,
|
|
77
|
+
fetched: false,
|
|
78
|
+
page: 0,
|
|
79
|
+
total: 0
|
|
80
|
+
},
|
|
81
|
+
...opts
|
|
82
|
+
});
|
|
83
|
+
var generateFieldName = ({
|
|
84
|
+
func,
|
|
85
|
+
type,
|
|
86
|
+
query = {}
|
|
87
|
+
}) => {
|
|
88
|
+
const funcName = typeof func === "string" ? func : `api-${Math.random().toString(36).substring(2)}`;
|
|
89
|
+
const fetchType = type || "auto";
|
|
90
|
+
let result = `${funcName}-${fetchType}`;
|
|
91
|
+
const filteredKeys = Object.keys(query).filter(
|
|
92
|
+
(key) => !["undefined", "object", "function"].includes(typeof query[key]) && ![
|
|
93
|
+
"page",
|
|
94
|
+
"is_up",
|
|
95
|
+
"since_id",
|
|
96
|
+
"seen_ids",
|
|
97
|
+
"__refresh__",
|
|
98
|
+
"__reload__"
|
|
99
|
+
].includes(key)
|
|
100
|
+
);
|
|
101
|
+
filteredKeys.sort().forEach((key) => {
|
|
102
|
+
result += `-${key}-${query[key]}`;
|
|
103
|
+
});
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
var getObjectDeepValue = (field, keys = "") => {
|
|
107
|
+
if (!keys || Array.isArray(keys) && keys.length === 0) {
|
|
108
|
+
return field;
|
|
109
|
+
}
|
|
110
|
+
const keysArr = Array.isArray(keys) ? keys : keys.split(".");
|
|
111
|
+
let result = field;
|
|
112
|
+
for (const key of keysArr) {
|
|
113
|
+
if (result == null || typeof result !== "object") {
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
result = result[key];
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
};
|
|
120
|
+
var updateObjectDeepValue = (field, changeKey, value) => {
|
|
121
|
+
if (!changeKey) return;
|
|
122
|
+
const keys = changeKey.split(".");
|
|
123
|
+
const lastKey = keys.pop();
|
|
124
|
+
let current = field;
|
|
125
|
+
for (const key of keys) {
|
|
126
|
+
if (current == null || typeof current !== "object") {
|
|
127
|
+
current[key] = {};
|
|
128
|
+
}
|
|
129
|
+
current = current[key];
|
|
130
|
+
}
|
|
131
|
+
if (current != null && typeof current === "object") {
|
|
132
|
+
current[lastKey] = value;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var searchValueByKey = (result, id, key) => {
|
|
136
|
+
if (isArray(result)) {
|
|
137
|
+
const index = computeMatchedItemIndex(id, result, key);
|
|
138
|
+
return index >= 0 ? result[index] : void 0;
|
|
139
|
+
} else {
|
|
140
|
+
return result[id];
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
var computeMatchedItemIndex = (itemId, fieldArr, changingKey) => {
|
|
144
|
+
const stringifiedItemId = String(itemId);
|
|
145
|
+
const len = fieldArr?.length;
|
|
146
|
+
if (typeof len !== "number" || len <= 0) return -1;
|
|
147
|
+
for (let i = 0; i < len; i++) {
|
|
148
|
+
const item = fieldArr[i];
|
|
149
|
+
if (typeof item !== "object" || item === null) continue;
|
|
150
|
+
const itemValue = getObjectDeepValue(item, changingKey);
|
|
151
|
+
if (String(itemValue) === stringifiedItemId) {
|
|
152
|
+
return i;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return -1;
|
|
156
|
+
};
|
|
157
|
+
var combineArrayData = (fieldArray, value, changingKey) => {
|
|
158
|
+
if (isArray(value)) {
|
|
159
|
+
for (const col of value) {
|
|
160
|
+
if (typeof col !== "object" || col === null) continue;
|
|
161
|
+
const stringifyId = String(getObjectDeepValue(col, changingKey));
|
|
162
|
+
const index = fieldArray.findIndex(
|
|
163
|
+
(item) => typeof item === "object" && item !== null && String(getObjectDeepValue(item, changingKey)) === stringifyId
|
|
164
|
+
);
|
|
165
|
+
if (index !== -1) {
|
|
166
|
+
fieldArray[index] = { ...fieldArray[index], ...col };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
for (const [uniqueId, col] of Object.entries(value)) {
|
|
171
|
+
if (typeof col !== "object" || col === null) continue;
|
|
172
|
+
const stringifyId = String(uniqueId);
|
|
173
|
+
const index = fieldArray.findIndex(
|
|
174
|
+
(item) => typeof item === "object" && item !== null && String(getObjectDeepValue(item, changingKey)) === stringifyId
|
|
175
|
+
);
|
|
176
|
+
if (index !== -1) {
|
|
177
|
+
fieldArray[index] = { ...fieldArray[index], ...col };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
var isArray = (data) => {
|
|
183
|
+
return Array.isArray(data);
|
|
184
|
+
};
|
|
185
|
+
var setReactivityField = (field, key, value, type, insertBefore) => {
|
|
186
|
+
if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
187
|
+
field[key] = value;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (isArray(value)) {
|
|
191
|
+
const current = field[key];
|
|
192
|
+
const currentArr = isArray(current) ? current : [];
|
|
193
|
+
const newValue = insertBefore ? value.concat(currentArr) : currentArr.concat(value);
|
|
194
|
+
field[key] = newValue;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (key !== enum_default.FIELD_DATA.RESULT_KEY) {
|
|
198
|
+
field[key] = value;
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const resultField = field.result;
|
|
202
|
+
if (isArray(resultField)) {
|
|
203
|
+
field.result = {};
|
|
204
|
+
}
|
|
205
|
+
const valueObj = value;
|
|
206
|
+
const target = field.result;
|
|
207
|
+
Object.keys(valueObj).forEach((subKey) => {
|
|
208
|
+
const existing = target[subKey];
|
|
209
|
+
const incoming = valueObj[subKey];
|
|
210
|
+
if (existing !== void 0) {
|
|
211
|
+
if (insertBefore) {
|
|
212
|
+
target[subKey] = isArray(incoming) && isArray(existing) ? incoming.concat(existing) : incoming;
|
|
213
|
+
} else {
|
|
214
|
+
target[subKey] = isArray(existing) && isArray(incoming) ? existing.concat(incoming) : incoming;
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
target[subKey] = incoming;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
var computeResultLength = (data) => {
|
|
222
|
+
if (isArray(data)) {
|
|
223
|
+
return data.length;
|
|
224
|
+
}
|
|
225
|
+
if (data && typeof data === "object") {
|
|
226
|
+
return Object.values(data).reduce((acc, val) => {
|
|
227
|
+
if (isArray(val)) {
|
|
228
|
+
return acc + val.length;
|
|
229
|
+
}
|
|
230
|
+
return acc;
|
|
231
|
+
}, 0);
|
|
232
|
+
}
|
|
233
|
+
return 0;
|
|
234
|
+
};
|
|
235
|
+
var generateRequestParams = ({
|
|
236
|
+
field,
|
|
237
|
+
uniqueKey,
|
|
238
|
+
query = {},
|
|
239
|
+
type
|
|
240
|
+
}) => {
|
|
241
|
+
const result = {};
|
|
242
|
+
const changing = uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
|
|
243
|
+
const isFetched = field.fetched;
|
|
244
|
+
const getSafeObjectKey = (item) => {
|
|
245
|
+
if (typeof item !== "object" || item === null) return void 0;
|
|
246
|
+
const val = getObjectDeepValue(item, changing);
|
|
247
|
+
if (typeof val === "string" || typeof val === "number") {
|
|
248
|
+
return val;
|
|
249
|
+
}
|
|
250
|
+
return void 0;
|
|
251
|
+
};
|
|
252
|
+
if (isFetched) {
|
|
253
|
+
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
254
|
+
if (isArray(field.result)) {
|
|
255
|
+
result.seen_ids = field.result.map((item) => getSafeObjectKey(item)).filter((id) => id !== void 0).join(",");
|
|
256
|
+
const targetIndex = query.is_up ? 0 : field.result.length - 1;
|
|
257
|
+
const targetItem = field.result[targetIndex];
|
|
258
|
+
result.since_id = getSafeObjectKey(targetItem);
|
|
259
|
+
}
|
|
260
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
261
|
+
result.page = typeof query.page === "number" ? query.page : field.page + 1;
|
|
262
|
+
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
263
|
+
if (isArray(field.result)) {
|
|
264
|
+
result.seen_ids = field.result.map((item) => getSafeObjectKey(item)).filter((id) => id !== void 0).join(",");
|
|
265
|
+
}
|
|
266
|
+
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
267
|
+
if (isArray(field.result)) {
|
|
268
|
+
const targetIndex = query.is_up ? 0 : field.result.length - 1;
|
|
269
|
+
const targetItem = field.result[targetIndex];
|
|
270
|
+
result.since_id = getSafeObjectKey(targetItem);
|
|
271
|
+
}
|
|
272
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
273
|
+
} else if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
274
|
+
result.page = typeof query.page === "number" ? query.page : void 0;
|
|
275
|
+
} else if (type === enum_default.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
276
|
+
result.page = field.page + 1;
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
280
|
+
result.seen_ids = "";
|
|
281
|
+
result.since_id = typeof query.sinceId === "string" || typeof query.sinceId === "number" ? query.sinceId : query.is_up ? 999999999 : 0;
|
|
282
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
283
|
+
result.page = typeof query.page === "number" ? query.page : field.page || 1;
|
|
284
|
+
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
285
|
+
result.seen_ids = "";
|
|
286
|
+
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
287
|
+
result.since_id = typeof query.sinceId === "string" || typeof query.sinceId === "number" ? query.sinceId : query.is_up ? 999999999 : 0;
|
|
288
|
+
result.is_up = query.is_up ? 1 : 0;
|
|
289
|
+
} else if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
290
|
+
result.page = typeof query.page === "number" ? query.page : field.page;
|
|
291
|
+
} else if (type === enum_default.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
292
|
+
result.page = 1;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
...query,
|
|
297
|
+
...result
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// src/setters.ts
|
|
302
|
+
var SET_DATA = ({
|
|
303
|
+
getter,
|
|
304
|
+
setter,
|
|
305
|
+
data,
|
|
306
|
+
fieldName,
|
|
307
|
+
type,
|
|
308
|
+
page,
|
|
309
|
+
insertBefore
|
|
310
|
+
}) => {
|
|
311
|
+
return new Promise((resolve, reject) => {
|
|
312
|
+
const fieldData = getter(fieldName);
|
|
313
|
+
if (!fieldData) {
|
|
314
|
+
reject(new Error(`Field ${fieldName} not found.`));
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const field = fieldData;
|
|
318
|
+
let result;
|
|
319
|
+
let extra;
|
|
320
|
+
if (isObjectResult(data)) {
|
|
321
|
+
result = data;
|
|
322
|
+
field.nothing = false;
|
|
323
|
+
field.fetched = true;
|
|
324
|
+
field.noMore = true;
|
|
325
|
+
field.page = -1;
|
|
326
|
+
} else {
|
|
327
|
+
const apiResponse = data;
|
|
328
|
+
result = apiResponse.result;
|
|
329
|
+
extra = apiResponse.extra;
|
|
330
|
+
const isEmpty = computeResultLength(result) === 0;
|
|
331
|
+
field.nothing = field.fetched ? false : isEmpty;
|
|
332
|
+
field.fetched = true;
|
|
333
|
+
field.total = apiResponse.total || 0;
|
|
334
|
+
if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
335
|
+
field.noMore = false;
|
|
336
|
+
field.page = +page;
|
|
337
|
+
} else {
|
|
338
|
+
field.noMore = typeof apiResponse.no_more === "undefined" ? isEmpty : apiResponse.no_more || isEmpty;
|
|
339
|
+
field.page = field.page + 1;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
field.loading = false;
|
|
343
|
+
setReactivityField(
|
|
344
|
+
field,
|
|
345
|
+
enum_default.FIELD_DATA.RESULT_KEY,
|
|
346
|
+
result,
|
|
347
|
+
type,
|
|
348
|
+
insertBefore
|
|
349
|
+
);
|
|
350
|
+
if (extra !== void 0 && extra !== null) {
|
|
351
|
+
setReactivityField(
|
|
352
|
+
field,
|
|
353
|
+
enum_default.FIELD_DATA.EXTRA_KEY,
|
|
354
|
+
extra,
|
|
355
|
+
type,
|
|
356
|
+
insertBefore
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
setter({
|
|
360
|
+
key: fieldName,
|
|
361
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
362
|
+
// or MUTATE if you have such type
|
|
363
|
+
value: field,
|
|
364
|
+
// same reference
|
|
365
|
+
callback: () => {
|
|
366
|
+
resolve();
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
var SET_ERROR = ({ setter, fieldName, error }) => {
|
|
372
|
+
setter({
|
|
373
|
+
key: fieldName,
|
|
374
|
+
type: enum_default.SETTER_TYPE.MERGE,
|
|
375
|
+
value: {
|
|
376
|
+
error,
|
|
377
|
+
loading: false
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// src/actions.ts
|
|
383
|
+
var initState = ({
|
|
384
|
+
getter,
|
|
385
|
+
setter,
|
|
386
|
+
func,
|
|
387
|
+
// string only
|
|
388
|
+
type,
|
|
389
|
+
query,
|
|
390
|
+
opts
|
|
391
|
+
}) => {
|
|
392
|
+
return new Promise((resolve) => {
|
|
393
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
394
|
+
const fieldData = getter(fieldName);
|
|
395
|
+
if (fieldData) {
|
|
396
|
+
resolve();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
setter({
|
|
400
|
+
key: fieldName,
|
|
401
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
402
|
+
value: generateDefaultField(opts),
|
|
403
|
+
callback: () => {
|
|
404
|
+
resolve();
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
};
|
|
409
|
+
var initData = ({
|
|
410
|
+
getter,
|
|
411
|
+
setter,
|
|
412
|
+
func,
|
|
413
|
+
// string | function
|
|
414
|
+
type,
|
|
415
|
+
query,
|
|
416
|
+
api,
|
|
417
|
+
uniqueKey,
|
|
418
|
+
callback
|
|
419
|
+
}) => new Promise((resolve, reject) => {
|
|
420
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
421
|
+
const fieldData = getter(fieldName);
|
|
422
|
+
const doRefresh = !!query?.__refresh__;
|
|
423
|
+
const needReset = !!query?.__reload__;
|
|
424
|
+
if (fieldData && fieldData.error && !doRefresh) {
|
|
425
|
+
resolve();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (fieldData && fieldData.loading) {
|
|
429
|
+
resolve();
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const dontFetch = fieldData && fieldData.fetched && !doRefresh;
|
|
433
|
+
if (dontFetch) {
|
|
434
|
+
resolve();
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const params = generateRequestParams({
|
|
438
|
+
field: {
|
|
439
|
+
...fieldData || generateDefaultField(),
|
|
440
|
+
fetched: false
|
|
441
|
+
},
|
|
442
|
+
uniqueKey,
|
|
443
|
+
query,
|
|
444
|
+
type
|
|
445
|
+
});
|
|
446
|
+
const getData = () => {
|
|
447
|
+
const loadData = () => new Promise((res, rej) => {
|
|
448
|
+
const getDataFromAPI = () => {
|
|
449
|
+
const funcCaller = typeof func === "string" && api ? api[func] : func;
|
|
450
|
+
if (typeof funcCaller === "function") {
|
|
451
|
+
funcCaller(params).then(res).catch((error) => {
|
|
452
|
+
SET_ERROR({ setter, fieldName, error });
|
|
453
|
+
rej(error);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
getDataFromAPI();
|
|
458
|
+
});
|
|
459
|
+
loadData().then((data) => {
|
|
460
|
+
const setData = () => {
|
|
461
|
+
SET_DATA({
|
|
462
|
+
getter,
|
|
463
|
+
setter,
|
|
464
|
+
data,
|
|
465
|
+
fieldName,
|
|
466
|
+
type,
|
|
467
|
+
page: params.page || 0,
|
|
468
|
+
insertBefore: false
|
|
469
|
+
}).then(() => {
|
|
470
|
+
if (callback) {
|
|
471
|
+
callback({
|
|
472
|
+
params,
|
|
473
|
+
data,
|
|
474
|
+
// <-- 'data' is now properly in scope
|
|
475
|
+
refresh: doRefresh
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
resolve();
|
|
479
|
+
});
|
|
480
|
+
};
|
|
481
|
+
if (needReset) {
|
|
482
|
+
setter({
|
|
483
|
+
key: fieldName,
|
|
484
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
485
|
+
value: generateDefaultField(),
|
|
486
|
+
callback: setData
|
|
487
|
+
});
|
|
488
|
+
} else {
|
|
489
|
+
setData();
|
|
490
|
+
}
|
|
491
|
+
}).catch(reject);
|
|
492
|
+
};
|
|
493
|
+
if (!dontFetch && !needReset) {
|
|
494
|
+
setter({
|
|
495
|
+
key: fieldName,
|
|
496
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
497
|
+
value: {
|
|
498
|
+
...generateDefaultField(),
|
|
499
|
+
loading: true,
|
|
500
|
+
error: null
|
|
501
|
+
},
|
|
502
|
+
callback: getData
|
|
503
|
+
});
|
|
504
|
+
} else {
|
|
505
|
+
getData();
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
var loadMore = ({
|
|
509
|
+
getter,
|
|
510
|
+
setter,
|
|
511
|
+
query,
|
|
512
|
+
type,
|
|
513
|
+
func,
|
|
514
|
+
// string | function
|
|
515
|
+
api,
|
|
516
|
+
uniqueKey,
|
|
517
|
+
errorRetry,
|
|
518
|
+
callback
|
|
519
|
+
}) => new Promise((resolve, reject) => {
|
|
520
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
521
|
+
const fieldData = getter(fieldName);
|
|
522
|
+
if (!fieldData) {
|
|
523
|
+
resolve();
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (fieldData.loading) {
|
|
527
|
+
resolve();
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
if (fieldData.nothing) {
|
|
531
|
+
resolve();
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
if (fieldData.noMore && !errorRetry) {
|
|
535
|
+
resolve();
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (type === enum_default.FETCH_TYPE.PAGINATION && query && query.page != null && +query.page === fieldData.page) {
|
|
539
|
+
resolve();
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
let loadingState;
|
|
543
|
+
if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
544
|
+
loadingState = {
|
|
545
|
+
loading: true,
|
|
546
|
+
error: null,
|
|
547
|
+
[enum_default.FIELD_DATA.RESULT_KEY]: [],
|
|
548
|
+
[enum_default.FIELD_DATA.EXTRA_KEY]: null
|
|
549
|
+
};
|
|
550
|
+
} else {
|
|
551
|
+
loadingState = {
|
|
552
|
+
loading: true,
|
|
553
|
+
error: null
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
const params = generateRequestParams({
|
|
557
|
+
field: fieldData,
|
|
558
|
+
uniqueKey,
|
|
559
|
+
query,
|
|
560
|
+
type
|
|
561
|
+
});
|
|
562
|
+
if ("extra" in fieldData) {
|
|
563
|
+
params[enum_default.FIELD_DATA.EXTRA_KEY] = fieldData.extra;
|
|
564
|
+
}
|
|
565
|
+
const getData = () => {
|
|
566
|
+
const funcCaller = typeof func === "string" && api ? api[func] : func;
|
|
567
|
+
if (typeof funcCaller === "function") {
|
|
568
|
+
funcCaller(params).then((data) => {
|
|
569
|
+
SET_DATA({
|
|
570
|
+
getter,
|
|
571
|
+
setter,
|
|
572
|
+
data,
|
|
573
|
+
fieldName,
|
|
574
|
+
type,
|
|
575
|
+
page: params.page || 0,
|
|
576
|
+
insertBefore: !!query?.is_up
|
|
577
|
+
}).then(() => {
|
|
578
|
+
if (callback) {
|
|
579
|
+
callback({
|
|
580
|
+
params,
|
|
581
|
+
data,
|
|
582
|
+
refresh: false
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
resolve();
|
|
586
|
+
});
|
|
587
|
+
}).catch((error) => {
|
|
588
|
+
SET_ERROR({ setter, fieldName, error });
|
|
589
|
+
reject(error);
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
setter({
|
|
594
|
+
key: fieldName,
|
|
595
|
+
type: enum_default.SETTER_TYPE.MERGE,
|
|
596
|
+
value: loadingState,
|
|
597
|
+
callback: getData
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
var updateState = ({
|
|
601
|
+
getter,
|
|
602
|
+
setter,
|
|
603
|
+
func,
|
|
604
|
+
// string only
|
|
605
|
+
type,
|
|
606
|
+
query,
|
|
607
|
+
method,
|
|
608
|
+
value,
|
|
609
|
+
id,
|
|
610
|
+
uniqueKey,
|
|
611
|
+
changeKey
|
|
612
|
+
}) => {
|
|
613
|
+
return new Promise((resolve, reject) => {
|
|
614
|
+
const fieldName = generateFieldName({ func, type, query });
|
|
615
|
+
const fieldData = getter(fieldName);
|
|
616
|
+
if (!fieldData) {
|
|
617
|
+
reject(new Error(`Field ${fieldName} not found.`));
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
if (fieldData.page === -1) {
|
|
621
|
+
resolve(null);
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
const _id = id;
|
|
625
|
+
const _uniqueKey = uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
|
|
626
|
+
const _changeKey = changeKey || enum_default.FIELD_DATA.RESULT_KEY;
|
|
627
|
+
const beforeLength = computeResultLength(
|
|
628
|
+
fieldData[enum_default.FIELD_DATA.RESULT_KEY]
|
|
629
|
+
);
|
|
630
|
+
const newFieldData = { ...fieldData };
|
|
631
|
+
if (method === enum_default.CHANGE_TYPE.SEARCH_FIELD) {
|
|
632
|
+
if (_id == null) {
|
|
633
|
+
reject(new Error("ID is required for SEARCH_FIELD."));
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
const result = searchValueByKey(
|
|
637
|
+
newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
|
|
638
|
+
_id,
|
|
639
|
+
_uniqueKey
|
|
640
|
+
);
|
|
641
|
+
resolve(result);
|
|
642
|
+
} else if (method === enum_default.CHANGE_TYPE.RESULT_UPDATE_KV) {
|
|
643
|
+
if (_id == null) {
|
|
644
|
+
reject(new Error("ID is required for RESULT_UPDATE_KV."));
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const matchedIndex = computeMatchedItemIndex(
|
|
648
|
+
_id,
|
|
649
|
+
newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
|
|
650
|
+
_uniqueKey
|
|
651
|
+
);
|
|
652
|
+
if (matchedIndex >= 0) {
|
|
653
|
+
updateObjectDeepValue(
|
|
654
|
+
newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex],
|
|
655
|
+
_changeKey,
|
|
656
|
+
value
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
resolve(null);
|
|
660
|
+
} else if (method === enum_default.CHANGE_TYPE.RESULT_ITEM_MERGE) {
|
|
661
|
+
if (_id == null) {
|
|
662
|
+
reject(new Error("ID is required for RESULT_ITEM_MERGE."));
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const matchedIndex = computeMatchedItemIndex(
|
|
666
|
+
_id,
|
|
667
|
+
newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
|
|
668
|
+
_uniqueKey
|
|
669
|
+
);
|
|
670
|
+
if (matchedIndex >= 0) {
|
|
671
|
+
const currentItem = newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex];
|
|
672
|
+
newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex] = {
|
|
673
|
+
...currentItem,
|
|
674
|
+
...value
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
resolve(null);
|
|
678
|
+
} else if (method === enum_default.CHANGE_TYPE.RESET_FIELD) {
|
|
679
|
+
updateObjectDeepValue(
|
|
680
|
+
newFieldData,
|
|
681
|
+
_changeKey,
|
|
682
|
+
value
|
|
683
|
+
);
|
|
684
|
+
resolve(null);
|
|
685
|
+
} else {
|
|
686
|
+
let modifyValue = getObjectDeepValue(
|
|
687
|
+
newFieldData,
|
|
688
|
+
_changeKey
|
|
689
|
+
);
|
|
690
|
+
if (modifyValue == null) {
|
|
691
|
+
modifyValue = [];
|
|
692
|
+
}
|
|
693
|
+
const matchedIndex = _id != null ? computeMatchedItemIndex(
|
|
694
|
+
_id,
|
|
695
|
+
modifyValue,
|
|
696
|
+
_uniqueKey
|
|
697
|
+
) : -1;
|
|
698
|
+
switch (method) {
|
|
699
|
+
case enum_default.CHANGE_TYPE.RESULT_ADD_AFTER:
|
|
700
|
+
if (isArray(modifyValue)) {
|
|
701
|
+
modifyValue = isArray(value) ? [...modifyValue, ...value] : [...modifyValue, value];
|
|
702
|
+
}
|
|
703
|
+
break;
|
|
704
|
+
case enum_default.CHANGE_TYPE.RESULT_ADD_BEFORE:
|
|
705
|
+
if (isArray(modifyValue)) {
|
|
706
|
+
modifyValue = isArray(value) ? [...value, ...modifyValue] : [value, ...modifyValue];
|
|
707
|
+
}
|
|
708
|
+
break;
|
|
709
|
+
case enum_default.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
|
|
710
|
+
if (isArray(modifyValue)) {
|
|
711
|
+
if (matchedIndex >= 0) {
|
|
712
|
+
modifyValue.splice(matchedIndex, 1);
|
|
713
|
+
} else if (isArray(_id)) {
|
|
714
|
+
const idSet = new Set(_id);
|
|
715
|
+
modifyValue = modifyValue.filter(
|
|
716
|
+
(item) => !idSet.has(
|
|
717
|
+
getObjectDeepValue(
|
|
718
|
+
item,
|
|
719
|
+
_uniqueKey
|
|
720
|
+
)
|
|
721
|
+
)
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
break;
|
|
726
|
+
case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
|
|
727
|
+
if (isArray(modifyValue) && matchedIndex >= 0) {
|
|
728
|
+
modifyValue.splice(matchedIndex, 0, value);
|
|
729
|
+
}
|
|
730
|
+
break;
|
|
731
|
+
case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
|
|
732
|
+
if (isArray(modifyValue) && matchedIndex >= 0) {
|
|
733
|
+
modifyValue.splice(matchedIndex + 1, 0, value);
|
|
734
|
+
}
|
|
735
|
+
break;
|
|
736
|
+
case enum_default.CHANGE_TYPE.RESULT_LIST_MERGE:
|
|
737
|
+
if (isArray(modifyValue)) {
|
|
738
|
+
combineArrayData(modifyValue, value, _uniqueKey);
|
|
739
|
+
}
|
|
740
|
+
break;
|
|
741
|
+
default:
|
|
742
|
+
resolve(null);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
newFieldData[_changeKey] = modifyValue;
|
|
746
|
+
resolve(null);
|
|
747
|
+
}
|
|
748
|
+
const afterLength = computeResultLength(
|
|
749
|
+
newFieldData[enum_default.FIELD_DATA.RESULT_KEY]
|
|
750
|
+
);
|
|
751
|
+
newFieldData.total = newFieldData.total + afterLength - beforeLength;
|
|
752
|
+
newFieldData.nothing = afterLength === 0;
|
|
753
|
+
setter({
|
|
754
|
+
key: fieldName,
|
|
755
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
756
|
+
value: newFieldData,
|
|
757
|
+
callback: () => {
|
|
758
|
+
resolve(null);
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
});
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
exports.ENUM = enum_default;
|
|
765
|
+
exports.initData = initData;
|
|
766
|
+
exports.initState = initState;
|
|
767
|
+
exports.loadMore = loadMore;
|
|
768
|
+
exports.updateState = updateState;
|
|
769
|
+
exports.utils = utils_exports;
|
|
770
|
+
|
|
771
|
+
return exports;
|
|
772
|
+
|
|
773
|
+
})({});
|
|
774
|
+
//# sourceMappingURL=index.global.js.map
|
|
775
|
+
//# sourceMappingURL=index.global.js.map
|