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