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