@flowlist/js-core 3.0.11 → 4.0.1-beta.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.mts +95 -37
- package/dist/index.d.ts +95 -37
- package/dist/index.global.js +243 -245
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +243 -245
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +243 -246
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
package/dist/index.js
CHANGED
|
@@ -6,23 +6,6 @@ var __export = (target, all) => {
|
|
|
6
6
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
// src/utils.ts
|
|
10
|
-
var utils_exports = {};
|
|
11
|
-
__export(utils_exports, {
|
|
12
|
-
combineArrayData: () => combineArrayData,
|
|
13
|
-
computeMatchedItemIndex: () => computeMatchedItemIndex,
|
|
14
|
-
computeResultLength: () => computeResultLength,
|
|
15
|
-
generateDefaultField: () => generateDefaultField,
|
|
16
|
-
generateFieldName: () => generateFieldName,
|
|
17
|
-
generateRequestParams: () => generateRequestParams,
|
|
18
|
-
getObjectDeepValue: () => getObjectDeepValue,
|
|
19
|
-
isArray: () => isArray,
|
|
20
|
-
isObjectResult: () => isObjectResult,
|
|
21
|
-
searchValueByKey: () => searchValueByKey,
|
|
22
|
-
setReactivityField: () => setReactivityField,
|
|
23
|
-
updateObjectDeepValue: () => updateObjectDeepValue
|
|
24
|
-
});
|
|
25
|
-
|
|
26
9
|
// src/enum.ts
|
|
27
10
|
var FETCH_TYPE_ARRAY = ["jump", "sinceId", "page", "seenIds", "auto"];
|
|
28
11
|
var enum_default = {
|
|
@@ -58,36 +41,66 @@ var enum_default = {
|
|
|
58
41
|
};
|
|
59
42
|
|
|
60
43
|
// src/utils.ts
|
|
44
|
+
var utils_exports = {};
|
|
45
|
+
__export(utils_exports, {
|
|
46
|
+
combineArrayData: () => combineArrayData,
|
|
47
|
+
computeMatchedItemIndex: () => computeMatchedItemIndex,
|
|
48
|
+
computeResultLength: () => computeResultLength,
|
|
49
|
+
generateDefaultField: () => generateDefaultField,
|
|
50
|
+
generateFieldName: () => generateFieldName,
|
|
51
|
+
generateRequestParams: () => generateRequestParams,
|
|
52
|
+
getObjectDeepValue: () => getObjectDeepValue,
|
|
53
|
+
isArray: () => isArray,
|
|
54
|
+
isKeyMap: () => isKeyMap,
|
|
55
|
+
isKeyMapArray: () => isKeyMapArray,
|
|
56
|
+
isObjectKey: () => isObjectKey,
|
|
57
|
+
isObjectKeyArray: () => isObjectKeyArray,
|
|
58
|
+
isObjectResult: () => isObjectResult,
|
|
59
|
+
isResultArray: () => isResultArray,
|
|
60
|
+
isResultObject: () => isResultObject,
|
|
61
|
+
searchValueByKey: () => searchValueByKey,
|
|
62
|
+
setReactivityField: () => setReactivityField,
|
|
63
|
+
updateObjectDeepValue: () => updateObjectDeepValue
|
|
64
|
+
});
|
|
61
65
|
var isArray = (data) => Array.isArray(data);
|
|
66
|
+
var isResultArray = (data) => Array.isArray(data);
|
|
67
|
+
var isResultObject = (data) => !Array.isArray(data) && typeof data === "object" && data !== null;
|
|
68
|
+
var isObjectKey = (value) => typeof value === "string" || typeof value === "number";
|
|
69
|
+
var isKeyMap = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
70
|
+
var isKeyMapArray = (value) => Array.isArray(value) && value.every((item) => typeof item === "object" && item !== null);
|
|
71
|
+
var isObjectKeyArray = (value) => Array.isArray(value) && value.every((item) => typeof item === "string" || typeof item === "number");
|
|
62
72
|
var stableSerialize = (value) => {
|
|
63
73
|
if (value === null || typeof value !== "object") {
|
|
64
74
|
return String(value);
|
|
65
75
|
}
|
|
66
76
|
try {
|
|
67
|
-
if (
|
|
77
|
+
if (isArray(value)) {
|
|
68
78
|
return JSON.stringify(value);
|
|
69
79
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
80
|
+
if (isKeyMap(value)) {
|
|
81
|
+
const keys = Object.keys(value).sort();
|
|
82
|
+
const obj = {};
|
|
83
|
+
const len = keys.length;
|
|
84
|
+
for (let i = 0; i < len; i++) {
|
|
85
|
+
const k = keys[i];
|
|
86
|
+
obj[k] = value[k];
|
|
87
|
+
}
|
|
88
|
+
return JSON.stringify(obj);
|
|
76
89
|
}
|
|
77
|
-
return
|
|
90
|
+
return "[Unsupported Object]";
|
|
78
91
|
} catch {
|
|
79
92
|
return "[Circular Object/Value]";
|
|
80
93
|
}
|
|
81
94
|
};
|
|
82
95
|
var extractUniqueKey = (item, uniqueKey) => {
|
|
83
|
-
if (
|
|
96
|
+
if (!isKeyMap(item)) return void 0;
|
|
84
97
|
const val = item[uniqueKey];
|
|
85
|
-
if (
|
|
98
|
+
if (isObjectKey(val)) {
|
|
86
99
|
return val;
|
|
87
100
|
}
|
|
88
101
|
if (uniqueKey.includes(".")) {
|
|
89
102
|
const deepVal = getObjectDeepValue(item, uniqueKey);
|
|
90
|
-
if (
|
|
103
|
+
if (isObjectKey(deepVal)) {
|
|
91
104
|
return deepVal;
|
|
92
105
|
}
|
|
93
106
|
}
|
|
@@ -113,16 +126,13 @@ var generateDefaultField = (opts = {}) => ({
|
|
|
113
126
|
});
|
|
114
127
|
var generateFieldName = ({
|
|
115
128
|
func,
|
|
116
|
-
|
|
117
|
-
query = {}
|
|
129
|
+
query
|
|
118
130
|
}) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
filteredKeys.sort();
|
|
131
|
+
let result = func.id;
|
|
132
|
+
if (!query) {
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
const filteredKeys = Object.keys(query).filter((key) => !func.paramsIgnore.includes(key)).sort();
|
|
126
136
|
const len = filteredKeys.length;
|
|
127
137
|
for (let i = 0; i < len; i++) {
|
|
128
138
|
const key = filteredKeys[i];
|
|
@@ -139,17 +149,21 @@ var generateFieldName = ({
|
|
|
139
149
|
return result;
|
|
140
150
|
};
|
|
141
151
|
var getObjectDeepValue = (field, keys) => {
|
|
142
|
-
if (!keys ||
|
|
152
|
+
if (!keys || isArray(keys) && keys.length === 0) {
|
|
143
153
|
return field;
|
|
144
154
|
}
|
|
145
|
-
const keysArr =
|
|
155
|
+
const keysArr = isArray(keys) ? keys : keys.split(".");
|
|
146
156
|
let result = field;
|
|
147
157
|
const len = keysArr.length;
|
|
148
158
|
for (let i = 0; i < len; i++) {
|
|
149
159
|
if (result == null || typeof result !== "object") {
|
|
150
160
|
return void 0;
|
|
151
161
|
}
|
|
152
|
-
result
|
|
162
|
+
if (isKeyMap(result)) {
|
|
163
|
+
result = result[keysArr[i]];
|
|
164
|
+
} else {
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
153
167
|
}
|
|
154
168
|
return result;
|
|
155
169
|
};
|
|
@@ -157,32 +171,42 @@ var updateObjectDeepValue = (field, changeKey, value) => {
|
|
|
157
171
|
if (!changeKey) return;
|
|
158
172
|
const keys = changeKey.split(".");
|
|
159
173
|
const lastKey = keys.pop();
|
|
174
|
+
if (!lastKey) return;
|
|
160
175
|
let current = field;
|
|
161
176
|
const len = keys.length;
|
|
162
177
|
for (let i = 0; i < len; i++) {
|
|
163
178
|
const key = keys[i];
|
|
164
|
-
|
|
179
|
+
const currentVal = current[key];
|
|
180
|
+
if (currentVal == null || typeof currentVal !== "object") {
|
|
165
181
|
current[key] = {};
|
|
166
182
|
}
|
|
167
|
-
|
|
183
|
+
const next = current[key];
|
|
184
|
+
if (isKeyMap(next)) {
|
|
185
|
+
current = next;
|
|
186
|
+
} else {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
168
189
|
}
|
|
169
190
|
if (current != null && typeof current === "object") {
|
|
170
191
|
current[lastKey] = value;
|
|
171
192
|
}
|
|
172
193
|
};
|
|
173
194
|
var searchValueByKey = (result, id, key) => {
|
|
174
|
-
if (
|
|
195
|
+
if (isResultArray(result)) {
|
|
175
196
|
const index = computeMatchedItemIndex(id, result, key);
|
|
176
197
|
return index >= 0 ? result[index] : void 0;
|
|
177
198
|
}
|
|
178
|
-
|
|
199
|
+
if (isResultObject(result)) {
|
|
200
|
+
return result[String(id)];
|
|
201
|
+
}
|
|
202
|
+
return void 0;
|
|
179
203
|
};
|
|
180
204
|
var computeMatchedItemIndex = (itemId, fieldArr, changingKey) => {
|
|
181
205
|
const stringifiedItemId = String(itemId);
|
|
182
206
|
const len = fieldArr.length;
|
|
183
207
|
for (let i = 0; i < len; i++) {
|
|
184
208
|
const item = fieldArr[i];
|
|
185
|
-
if (
|
|
209
|
+
if (!isKeyMap(item)) continue;
|
|
186
210
|
const itemValue = getObjectDeepValue(item, changingKey);
|
|
187
211
|
if (String(itemValue) === stringifiedItemId) {
|
|
188
212
|
return i;
|
|
@@ -195,7 +219,7 @@ var combineArrayData = (fieldArray, value, changingKey) => {
|
|
|
195
219
|
const arrLen = fieldArray.length;
|
|
196
220
|
for (let i = 0; i < arrLen; i++) {
|
|
197
221
|
const item = fieldArray[i];
|
|
198
|
-
if (
|
|
222
|
+
if (!isKeyMap(item)) continue;
|
|
199
223
|
const id = getObjectDeepValue(item, changingKey);
|
|
200
224
|
if (id !== void 0) {
|
|
201
225
|
fieldArrayMap.set(String(id), i);
|
|
@@ -205,19 +229,19 @@ var combineArrayData = (fieldArray, value, changingKey) => {
|
|
|
205
229
|
const valLen = value.length;
|
|
206
230
|
for (let i = 0; i < valLen; i++) {
|
|
207
231
|
const col = value[i];
|
|
208
|
-
if (
|
|
232
|
+
if (!isKeyMap(col)) continue;
|
|
209
233
|
const stringifyId = String(getObjectDeepValue(col, changingKey));
|
|
210
234
|
const index = fieldArrayMap.get(stringifyId);
|
|
211
235
|
if (index !== void 0) {
|
|
212
236
|
fieldArray[index] = { ...fieldArray[index], ...col };
|
|
213
237
|
}
|
|
214
238
|
}
|
|
215
|
-
} else {
|
|
239
|
+
} else if (isKeyMap(value)) {
|
|
216
240
|
const entries = Object.entries(value);
|
|
217
241
|
const entLen = entries.length;
|
|
218
242
|
for (let i = 0; i < entLen; i++) {
|
|
219
243
|
const [uniqueId, col] = entries[i];
|
|
220
|
-
if (
|
|
244
|
+
if (!isKeyMap(col)) continue;
|
|
221
245
|
const index = fieldArrayMap.get(uniqueId);
|
|
222
246
|
if (index !== void 0) {
|
|
223
247
|
fieldArray[index] = { ...fieldArray[index], ...col };
|
|
@@ -278,13 +302,14 @@ var computeResultLength = (data) => {
|
|
|
278
302
|
if (isArray(data)) {
|
|
279
303
|
return data.length;
|
|
280
304
|
}
|
|
281
|
-
if (data
|
|
305
|
+
if (isKeyMap(data)) {
|
|
282
306
|
let acc = 0;
|
|
283
307
|
const values = Object.values(data);
|
|
284
308
|
const len = values.length;
|
|
285
309
|
for (let i = 0; i < len; i++) {
|
|
286
|
-
|
|
287
|
-
|
|
310
|
+
const val = values[i];
|
|
311
|
+
if (isArray(val)) {
|
|
312
|
+
acc += val.length;
|
|
288
313
|
}
|
|
289
314
|
}
|
|
290
315
|
return acc;
|
|
@@ -316,7 +341,7 @@ var generateRequestParams = ({
|
|
|
316
341
|
};
|
|
317
342
|
if (isFetched) {
|
|
318
343
|
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
319
|
-
if (
|
|
344
|
+
if (isResultArray(field.result)) {
|
|
320
345
|
result.seen_ids = getSeenIdsString(field.result, uniqueKey);
|
|
321
346
|
const targetIndex = query.is_up ? 0 : field.result.length - 1;
|
|
322
347
|
const targetItem = field.result[targetIndex];
|
|
@@ -325,11 +350,11 @@ var generateRequestParams = ({
|
|
|
325
350
|
result.is_up = query.is_up ? 1 : 0;
|
|
326
351
|
result.page = typeof query.page === "number" ? query.page : field.page + 1;
|
|
327
352
|
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
328
|
-
if (
|
|
353
|
+
if (isResultArray(field.result)) {
|
|
329
354
|
result.seen_ids = getSeenIdsString(field.result, uniqueKey);
|
|
330
355
|
}
|
|
331
356
|
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
332
|
-
if (
|
|
357
|
+
if (isResultArray(field.result)) {
|
|
333
358
|
const targetIndex = query.is_up ? 0 : field.result.length - 1;
|
|
334
359
|
const targetItem = field.result[targetIndex];
|
|
335
360
|
result.since_id = getSafeObjectKey(targetItem);
|
|
@@ -343,13 +368,13 @@ var generateRequestParams = ({
|
|
|
343
368
|
} else {
|
|
344
369
|
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
345
370
|
result.seen_ids = "";
|
|
346
|
-
result.since_id =
|
|
371
|
+
result.since_id = isObjectKey(query.sinceId) ? query.sinceId : query.is_up ? 999999999 : 0;
|
|
347
372
|
result.is_up = query.is_up ? 1 : 0;
|
|
348
373
|
result.page = typeof query.page === "number" ? query.page : field.page || 1;
|
|
349
374
|
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
350
375
|
result.seen_ids = "";
|
|
351
376
|
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
352
|
-
result.since_id =
|
|
377
|
+
result.since_id = isObjectKey(query.sinceId) ? query.sinceId : query.is_up ? 999999999 : 0;
|
|
353
378
|
result.is_up = query.is_up ? 1 : 0;
|
|
354
379
|
} else if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
355
380
|
result.page = typeof query.page === "number" ? query.page : field.page;
|
|
@@ -444,12 +469,11 @@ var initState = ({
|
|
|
444
469
|
getter,
|
|
445
470
|
setter,
|
|
446
471
|
func,
|
|
447
|
-
type,
|
|
448
472
|
query,
|
|
449
473
|
opts
|
|
450
474
|
}) => {
|
|
451
475
|
return new Promise((resolve) => {
|
|
452
|
-
const fieldName = generateFieldName({ func,
|
|
476
|
+
const fieldName = generateFieldName({ func, query });
|
|
453
477
|
const fieldData = getter(fieldName);
|
|
454
478
|
if (fieldData) {
|
|
455
479
|
resolve();
|
|
@@ -469,69 +493,39 @@ var initData = ({
|
|
|
469
493
|
getter,
|
|
470
494
|
setter,
|
|
471
495
|
func,
|
|
472
|
-
type,
|
|
473
496
|
query,
|
|
474
|
-
api,
|
|
475
|
-
uniqueKey,
|
|
476
497
|
callback
|
|
477
498
|
}) => new Promise((resolve, reject) => {
|
|
478
|
-
const fieldName = generateFieldName({ func,
|
|
499
|
+
const fieldName = generateFieldName({ func, query });
|
|
479
500
|
const fieldData = getter(fieldName);
|
|
480
501
|
const doRefresh = !!query?.__refresh__;
|
|
481
502
|
const needReset = !!query?.__reload__;
|
|
482
|
-
if (fieldData && fieldData.
|
|
483
|
-
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
if (fieldData && fieldData.loading) {
|
|
487
|
-
resolve();
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
const dontFetch = fieldData && fieldData.fetched && !doRefresh;
|
|
491
|
-
if (dontFetch) {
|
|
492
|
-
resolve();
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
503
|
+
if (fieldData && fieldData.loading) return resolve();
|
|
504
|
+
if (fieldData && fieldData.fetched && !doRefresh) return resolve();
|
|
495
505
|
const params = generateRequestParams({
|
|
496
|
-
field:
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
query,
|
|
502
|
-
type
|
|
506
|
+
field: fieldData || generateDefaultField(),
|
|
507
|
+
uniqueKey: func.uniqueKey,
|
|
508
|
+
// 自动从契约获取
|
|
509
|
+
type: func.type,
|
|
510
|
+
query
|
|
503
511
|
});
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
const
|
|
507
|
-
const funcCaller = typeof func === "string" && api ? api[func] : func;
|
|
508
|
-
if (typeof funcCaller === "function") {
|
|
509
|
-
funcCaller(params).then(res).catch((error) => {
|
|
510
|
-
SET_ERROR({ setter, fieldName, error });
|
|
511
|
-
rej(error);
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
};
|
|
515
|
-
getDataFromAPI();
|
|
516
|
-
});
|
|
517
|
-
apiCaller().then((data) => {
|
|
518
|
-
const setData = () => {
|
|
512
|
+
const executeFetch = () => {
|
|
513
|
+
func(params).then((data) => {
|
|
514
|
+
const commitData = () => {
|
|
519
515
|
SET_DATA({
|
|
520
516
|
getter,
|
|
521
517
|
setter,
|
|
522
518
|
data,
|
|
523
519
|
fieldName,
|
|
524
|
-
type,
|
|
520
|
+
type: func.type,
|
|
525
521
|
page: params.page || 0,
|
|
526
522
|
insertBefore: false
|
|
527
523
|
}).then(() => {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
});
|
|
534
|
-
}
|
|
524
|
+
callback?.({
|
|
525
|
+
params,
|
|
526
|
+
data,
|
|
527
|
+
refresh: doRefresh
|
|
528
|
+
});
|
|
535
529
|
resolve();
|
|
536
530
|
});
|
|
537
531
|
};
|
|
@@ -540,105 +534,68 @@ var initData = ({
|
|
|
540
534
|
key: fieldName,
|
|
541
535
|
type: enum_default.SETTER_TYPE.RESET,
|
|
542
536
|
value: generateDefaultField(),
|
|
543
|
-
callback:
|
|
537
|
+
callback: commitData
|
|
544
538
|
});
|
|
545
539
|
} else {
|
|
546
|
-
|
|
540
|
+
commitData();
|
|
547
541
|
}
|
|
548
|
-
}).catch(
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
getData();
|
|
552
|
-
} else {
|
|
553
|
-
setter({
|
|
554
|
-
key: fieldName,
|
|
555
|
-
type: enum_default.SETTER_TYPE.RESET,
|
|
556
|
-
value: {
|
|
557
|
-
...generateDefaultField(),
|
|
558
|
-
loading: true,
|
|
559
|
-
error: null,
|
|
560
|
-
extra: null
|
|
561
|
-
},
|
|
562
|
-
callback: getData
|
|
542
|
+
}).catch((error) => {
|
|
543
|
+
SET_ERROR({ setter, fieldName, error });
|
|
544
|
+
reject(error);
|
|
563
545
|
});
|
|
564
|
-
}
|
|
546
|
+
};
|
|
547
|
+
setter({
|
|
548
|
+
key: fieldName,
|
|
549
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
550
|
+
value: {
|
|
551
|
+
...generateDefaultField(),
|
|
552
|
+
loading: true,
|
|
553
|
+
error: null
|
|
554
|
+
},
|
|
555
|
+
callback: executeFetch
|
|
556
|
+
});
|
|
565
557
|
});
|
|
566
558
|
var loadMore = ({
|
|
567
559
|
getter,
|
|
568
560
|
setter,
|
|
569
561
|
query,
|
|
570
|
-
type,
|
|
571
562
|
func,
|
|
572
|
-
api,
|
|
573
|
-
uniqueKey,
|
|
574
563
|
errorRetry,
|
|
575
564
|
callback
|
|
576
565
|
}) => new Promise((resolve, reject) => {
|
|
577
|
-
const fieldName = generateFieldName({ func,
|
|
566
|
+
const fieldName = generateFieldName({ func, query });
|
|
578
567
|
const fieldData = getter(fieldName);
|
|
579
|
-
if (!fieldData)
|
|
580
|
-
|
|
581
|
-
return;
|
|
582
|
-
}
|
|
583
|
-
if (fieldData.loading) {
|
|
584
|
-
resolve();
|
|
585
|
-
return;
|
|
586
|
-
}
|
|
587
|
-
if (fieldData.nothing) {
|
|
588
|
-
resolve();
|
|
589
|
-
return;
|
|
590
|
-
}
|
|
591
|
-
if (fieldData.noMore && !errorRetry) {
|
|
592
|
-
resolve();
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
|
-
if (type === enum_default.FETCH_TYPE.PAGINATION && query && query.page != null && +query.page === fieldData.page) {
|
|
596
|
-
resolve();
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
let loadingState;
|
|
600
|
-
if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
601
|
-
loadingState = {
|
|
602
|
-
loading: true,
|
|
603
|
-
error: null,
|
|
604
|
-
[enum_default.FIELD_DATA.RESULT_KEY]: [],
|
|
605
|
-
[enum_default.FIELD_DATA.EXTRA_KEY]: null
|
|
606
|
-
};
|
|
607
|
-
} else {
|
|
608
|
-
loadingState = {
|
|
609
|
-
loading: true,
|
|
610
|
-
error: null
|
|
611
|
-
};
|
|
612
|
-
}
|
|
568
|
+
if (!fieldData || fieldData.loading || fieldData.nothing) return resolve();
|
|
569
|
+
if (fieldData.noMore && !errorRetry) return resolve();
|
|
613
570
|
const params = generateRequestParams({
|
|
614
571
|
field: fieldData,
|
|
615
|
-
uniqueKey,
|
|
616
|
-
|
|
617
|
-
|
|
572
|
+
uniqueKey: func.uniqueKey,
|
|
573
|
+
type: func.type,
|
|
574
|
+
query
|
|
618
575
|
});
|
|
619
|
-
if (
|
|
576
|
+
if (fieldData.extra) {
|
|
620
577
|
params[enum_default.FIELD_DATA.EXTRA_KEY] = fieldData.extra;
|
|
621
578
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
579
|
+
setter({
|
|
580
|
+
key: fieldName,
|
|
581
|
+
type: enum_default.SETTER_TYPE.MERGE,
|
|
582
|
+
value: { loading: true, error: null },
|
|
583
|
+
callback: () => {
|
|
584
|
+
func(params).then((data) => {
|
|
626
585
|
SET_DATA({
|
|
627
586
|
getter,
|
|
628
587
|
setter,
|
|
629
588
|
data,
|
|
630
589
|
fieldName,
|
|
631
|
-
type,
|
|
590
|
+
type: func.type,
|
|
632
591
|
page: params.page || 0,
|
|
633
592
|
insertBefore: !!query?.is_up
|
|
634
593
|
}).then(() => {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
});
|
|
641
|
-
}
|
|
594
|
+
callback?.({
|
|
595
|
+
params,
|
|
596
|
+
data,
|
|
597
|
+
refresh: false
|
|
598
|
+
});
|
|
642
599
|
resolve();
|
|
643
600
|
});
|
|
644
601
|
}).catch((error) => {
|
|
@@ -646,28 +603,38 @@ var loadMore = ({
|
|
|
646
603
|
reject(error);
|
|
647
604
|
});
|
|
648
605
|
}
|
|
649
|
-
};
|
|
650
|
-
setter({
|
|
651
|
-
key: fieldName,
|
|
652
|
-
type: enum_default.SETTER_TYPE.MERGE,
|
|
653
|
-
value: loadingState,
|
|
654
|
-
callback: getData
|
|
655
606
|
});
|
|
656
607
|
});
|
|
608
|
+
var toObjectKey = (id) => {
|
|
609
|
+
if (id === void 0) return void 0;
|
|
610
|
+
if (isObjectKey(id)) return id;
|
|
611
|
+
if (isObjectKeyArray(id) && id.length > 0) return id[0];
|
|
612
|
+
return void 0;
|
|
613
|
+
};
|
|
614
|
+
var getResultAsArray = (field) => {
|
|
615
|
+
const result = field[enum_default.FIELD_DATA.RESULT_KEY];
|
|
616
|
+
return isResultArray(result) ? result : null;
|
|
617
|
+
};
|
|
618
|
+
var updateArrayItem = (arr, index, updater) => {
|
|
619
|
+
if (index >= 0 && index < arr.length) {
|
|
620
|
+
const item = arr[index];
|
|
621
|
+
if (isKeyMap(item)) {
|
|
622
|
+
arr[index] = updater(item);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
};
|
|
657
626
|
var updateState = ({
|
|
658
627
|
getter,
|
|
659
628
|
setter,
|
|
660
629
|
func,
|
|
661
|
-
type,
|
|
662
630
|
query,
|
|
663
631
|
method,
|
|
664
632
|
value,
|
|
665
633
|
id,
|
|
666
|
-
uniqueKey,
|
|
667
634
|
changeKey
|
|
668
635
|
}) => {
|
|
669
636
|
return new Promise((resolve, reject) => {
|
|
670
|
-
const fieldName = generateFieldName({ func,
|
|
637
|
+
const fieldName = generateFieldName({ func, query });
|
|
671
638
|
const fieldData = getter(fieldName);
|
|
672
639
|
if (!fieldData) {
|
|
673
640
|
reject(new Error(`Field ${fieldName} not found.`));
|
|
@@ -678,79 +645,78 @@ var updateState = ({
|
|
|
678
645
|
return;
|
|
679
646
|
}
|
|
680
647
|
const _id = id;
|
|
681
|
-
const _uniqueKey = uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
|
|
648
|
+
const _uniqueKey = func.uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
|
|
682
649
|
const _changeKey = changeKey || enum_default.FIELD_DATA.RESULT_KEY;
|
|
683
650
|
const beforeLength = computeResultLength(
|
|
684
651
|
fieldData[enum_default.FIELD_DATA.RESULT_KEY]
|
|
685
652
|
);
|
|
686
653
|
const newFieldData = { ...fieldData };
|
|
654
|
+
const resultArray = getResultAsArray(newFieldData);
|
|
687
655
|
if (method === enum_default.CHANGE_TYPE.SEARCH_FIELD) {
|
|
688
|
-
|
|
656
|
+
const objectKeyId = toObjectKey(_id);
|
|
657
|
+
if (objectKeyId === void 0) {
|
|
689
658
|
reject(new Error("ID is required for SEARCH_FIELD."));
|
|
690
659
|
return;
|
|
691
660
|
}
|
|
692
|
-
const
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
_uniqueKey
|
|
696
|
-
);
|
|
697
|
-
resolve(result);
|
|
661
|
+
const searchResult = resultArray ? searchValueByKey(resultArray, objectKeyId, _uniqueKey) : void 0;
|
|
662
|
+
resolve(searchResult);
|
|
663
|
+
return;
|
|
698
664
|
} else if (method === enum_default.CHANGE_TYPE.RESULT_UPDATE_KV) {
|
|
699
|
-
|
|
665
|
+
const objectKeyId = toObjectKey(_id);
|
|
666
|
+
if (objectKeyId === void 0) {
|
|
700
667
|
reject(new Error("ID is required for RESULT_UPDATE_KV."));
|
|
701
668
|
return;
|
|
702
669
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
if (matchedIndex >= 0) {
|
|
709
|
-
updateObjectDeepValue(
|
|
710
|
-
newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex],
|
|
711
|
-
_changeKey,
|
|
712
|
-
value
|
|
670
|
+
if (resultArray) {
|
|
671
|
+
const matchedIndex = computeMatchedItemIndex(
|
|
672
|
+
objectKeyId,
|
|
673
|
+
resultArray,
|
|
674
|
+
_uniqueKey
|
|
713
675
|
);
|
|
676
|
+
if (matchedIndex >= 0 && isKeyMap(resultArray[matchedIndex])) {
|
|
677
|
+
updateObjectDeepValue(resultArray[matchedIndex], _changeKey, value);
|
|
678
|
+
}
|
|
714
679
|
}
|
|
715
680
|
resolve(null);
|
|
716
681
|
} else if (method === enum_default.CHANGE_TYPE.RESULT_ITEM_MERGE) {
|
|
717
|
-
|
|
682
|
+
const objectKeyId = toObjectKey(_id);
|
|
683
|
+
if (objectKeyId === void 0) {
|
|
718
684
|
reject(new Error("ID is required for RESULT_ITEM_MERGE."));
|
|
719
685
|
return;
|
|
720
686
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
...currentItem,
|
|
687
|
+
if (resultArray && isKeyMap(value)) {
|
|
688
|
+
const matchedIndex = computeMatchedItemIndex(
|
|
689
|
+
objectKeyId,
|
|
690
|
+
resultArray,
|
|
691
|
+
_uniqueKey
|
|
692
|
+
);
|
|
693
|
+
updateArrayItem(resultArray, matchedIndex, (item) => ({
|
|
694
|
+
...item,
|
|
730
695
|
...value
|
|
731
|
-
};
|
|
696
|
+
}));
|
|
732
697
|
}
|
|
733
698
|
resolve(null);
|
|
734
699
|
} else if (method === enum_default.CHANGE_TYPE.RESET_FIELD) {
|
|
735
|
-
|
|
736
|
-
newFieldData
|
|
737
|
-
|
|
738
|
-
value
|
|
739
|
-
|
|
700
|
+
if (_changeKey === enum_default.FIELD_DATA.RESULT_KEY && isKeyMapArray(value)) {
|
|
701
|
+
newFieldData.result = value;
|
|
702
|
+
} else if (_changeKey === enum_default.FIELD_DATA.EXTRA_KEY && isKeyMap(value)) {
|
|
703
|
+
newFieldData.extra = value;
|
|
704
|
+
}
|
|
740
705
|
resolve(null);
|
|
741
706
|
} else {
|
|
742
|
-
let modifyValue
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
)
|
|
707
|
+
let modifyValue;
|
|
708
|
+
if (_changeKey === enum_default.FIELD_DATA.RESULT_KEY) {
|
|
709
|
+
modifyValue = newFieldData.result;
|
|
710
|
+
} else if (_changeKey === enum_default.FIELD_DATA.EXTRA_KEY) {
|
|
711
|
+
modifyValue = newFieldData.extra;
|
|
712
|
+
} else {
|
|
713
|
+
modifyValue = getObjectDeepValue(newFieldData, _changeKey);
|
|
714
|
+
}
|
|
746
715
|
if (modifyValue == null) {
|
|
747
716
|
modifyValue = [];
|
|
748
717
|
}
|
|
749
|
-
const
|
|
750
|
-
|
|
751
|
-
modifyValue,
|
|
752
|
-
_uniqueKey
|
|
753
|
-
) : -1;
|
|
718
|
+
const objectKeyId = toObjectKey(_id);
|
|
719
|
+
const matchedIndex = objectKeyId !== void 0 && isKeyMapArray(modifyValue) ? computeMatchedItemIndex(objectKeyId, modifyValue, _uniqueKey) : -1;
|
|
754
720
|
switch (method) {
|
|
755
721
|
case enum_default.CHANGE_TYPE.RESULT_ADD_AFTER:
|
|
756
722
|
if (isArray(modifyValue)) {
|
|
@@ -763,16 +729,15 @@ var updateState = ({
|
|
|
763
729
|
}
|
|
764
730
|
break;
|
|
765
731
|
case enum_default.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
|
|
766
|
-
if (
|
|
732
|
+
if (isKeyMapArray(modifyValue)) {
|
|
767
733
|
if (matchedIndex >= 0) {
|
|
768
734
|
modifyValue.splice(matchedIndex, 1);
|
|
769
|
-
} else if (
|
|
735
|
+
} else if (isObjectKeyArray(_id)) {
|
|
770
736
|
const idSet = new Set(_id);
|
|
771
|
-
modifyValue = modifyValue.filter(
|
|
772
|
-
(item)
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
);
|
|
737
|
+
modifyValue = modifyValue.filter((item) => {
|
|
738
|
+
const itemKey = getObjectDeepValue(item, _uniqueKey);
|
|
739
|
+
return !isObjectKey(itemKey) || !idSet.has(itemKey);
|
|
740
|
+
});
|
|
776
741
|
}
|
|
777
742
|
}
|
|
778
743
|
break;
|
|
@@ -787,19 +752,29 @@ var updateState = ({
|
|
|
787
752
|
}
|
|
788
753
|
break;
|
|
789
754
|
case enum_default.CHANGE_TYPE.RESULT_LIST_MERGE:
|
|
790
|
-
if (
|
|
791
|
-
|
|
792
|
-
modifyValue,
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
755
|
+
if (isKeyMapArray(modifyValue)) {
|
|
756
|
+
if (isKeyMapArray(value)) {
|
|
757
|
+
combineArrayData(modifyValue, value, _uniqueKey);
|
|
758
|
+
} else if (isKeyMap(value)) {
|
|
759
|
+
const valueAsRecord = {};
|
|
760
|
+
for (const [k, v] of Object.entries(value)) {
|
|
761
|
+
if (isKeyMap(v)) {
|
|
762
|
+
valueAsRecord[k] = v;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
combineArrayData(modifyValue, valueAsRecord, _uniqueKey);
|
|
766
|
+
}
|
|
796
767
|
}
|
|
797
768
|
break;
|
|
798
769
|
default:
|
|
799
770
|
resolve(null);
|
|
800
771
|
return;
|
|
801
772
|
}
|
|
802
|
-
|
|
773
|
+
if (_changeKey === enum_default.FIELD_DATA.RESULT_KEY && isKeyMapArray(modifyValue)) {
|
|
774
|
+
newFieldData.result = modifyValue;
|
|
775
|
+
} else if (_changeKey === enum_default.FIELD_DATA.EXTRA_KEY && isKeyMap(modifyValue)) {
|
|
776
|
+
newFieldData.extra = modifyValue;
|
|
777
|
+
}
|
|
803
778
|
resolve(null);
|
|
804
779
|
}
|
|
805
780
|
const afterLength = computeResultLength(
|
|
@@ -818,7 +793,30 @@ var updateState = ({
|
|
|
818
793
|
});
|
|
819
794
|
};
|
|
820
795
|
|
|
796
|
+
// src/types.ts
|
|
797
|
+
function createApi(options) {
|
|
798
|
+
const fn = Object.assign(
|
|
799
|
+
(params) => options.fetcher(params),
|
|
800
|
+
{
|
|
801
|
+
id: options.id,
|
|
802
|
+
type: options.type || enum_default.FETCH_TYPE.SCROLL_LOAD_MORE,
|
|
803
|
+
uniqueKey: options.uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME,
|
|
804
|
+
paramsIgnore: [
|
|
805
|
+
"page",
|
|
806
|
+
"is_up",
|
|
807
|
+
"since_id",
|
|
808
|
+
"seen_ids",
|
|
809
|
+
"__refresh__",
|
|
810
|
+
"__reload__",
|
|
811
|
+
...options.paramsIgnore || []
|
|
812
|
+
]
|
|
813
|
+
}
|
|
814
|
+
);
|
|
815
|
+
return Object.freeze(fn);
|
|
816
|
+
}
|
|
817
|
+
|
|
821
818
|
exports.ENUM = enum_default;
|
|
819
|
+
exports.createApi = createApi;
|
|
822
820
|
exports.initData = initData;
|
|
823
821
|
exports.initState = initState;
|
|
824
822
|
exports.loadMore = loadMore;
|