@flowlist/js-core 4.0.4-beta.0 → 4.0.6-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 +121 -150
- package/dist/index.d.ts +121 -150
- package/dist/index.global.js +119 -121
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +119 -121
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +119 -122
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -50,25 +50,27 @@ __export(utils_exports, {
|
|
|
50
50
|
generateFieldName: () => generateFieldName,
|
|
51
51
|
generateRequestParams: () => generateRequestParams,
|
|
52
52
|
getObjectDeepValue: () => getObjectDeepValue,
|
|
53
|
+
getResultAsArray: () => getResultAsArray,
|
|
53
54
|
isArray: () => isArray,
|
|
54
55
|
isKeyMap: () => isKeyMap,
|
|
55
56
|
isKeyMapArray: () => isKeyMapArray,
|
|
56
57
|
isObjectKey: () => isObjectKey,
|
|
57
58
|
isObjectKeyArray: () => isObjectKeyArray,
|
|
58
59
|
isObjectResult: () => isObjectResult,
|
|
59
|
-
isResultArray: () => isResultArray,
|
|
60
60
|
isResultObject: () => isResultObject,
|
|
61
61
|
searchValueByKey: () => searchValueByKey,
|
|
62
62
|
setReactivityField: () => setReactivityField,
|
|
63
|
+
stableSerialize: () => stableSerialize,
|
|
64
|
+
toObjectKey: () => toObjectKey,
|
|
65
|
+
updateArrayItem: () => updateArrayItem,
|
|
63
66
|
updateObjectDeepValue: () => updateObjectDeepValue
|
|
64
67
|
});
|
|
65
68
|
var isArray = (data) => Array.isArray(data);
|
|
66
|
-
var
|
|
67
|
-
var isResultObject = (data) => !Array.isArray(data) && typeof data === "object" && data !== null;
|
|
69
|
+
var isResultObject = (data) => !isArray(data) && typeof data === "object" && data !== null;
|
|
68
70
|
var isObjectKey = (value) => typeof value === "string" || typeof value === "number";
|
|
69
|
-
var isKeyMap = (value) => typeof value === "object" && value !== null && !
|
|
70
|
-
var isKeyMapArray = (value) =>
|
|
71
|
-
var isObjectKeyArray = (value) =>
|
|
71
|
+
var isKeyMap = (value) => typeof value === "object" && value !== null && !isArray(value);
|
|
72
|
+
var isKeyMapArray = (value) => isArray(value) && value.every((item) => typeof item === "object" && item !== null);
|
|
73
|
+
var isObjectKeyArray = (value) => isArray(value) && value.every((item) => typeof item === "string" || typeof item === "number");
|
|
72
74
|
var stableSerialize = (value) => {
|
|
73
75
|
if (value === null || typeof value !== "object") {
|
|
74
76
|
return String(value);
|
|
@@ -114,6 +116,7 @@ var isObjectResult = (data) => {
|
|
|
114
116
|
};
|
|
115
117
|
var generateDefaultField = (opts = {}) => ({
|
|
116
118
|
result: [],
|
|
119
|
+
// 默认为空数组,但强制转换为泛型 T
|
|
117
120
|
noMore: false,
|
|
118
121
|
nothing: false,
|
|
119
122
|
loading: false,
|
|
@@ -160,11 +163,7 @@ var getObjectDeepValue = (field, keys) => {
|
|
|
160
163
|
if (result == null || typeof result !== "object") {
|
|
161
164
|
return void 0;
|
|
162
165
|
}
|
|
163
|
-
|
|
164
|
-
result = result[keysArr[i]];
|
|
165
|
-
} else {
|
|
166
|
-
return void 0;
|
|
167
|
-
}
|
|
166
|
+
result = result[keysArr[i]];
|
|
168
167
|
}
|
|
169
168
|
return result;
|
|
170
169
|
};
|
|
@@ -193,7 +192,7 @@ var updateObjectDeepValue = (field, changeKey, value) => {
|
|
|
193
192
|
}
|
|
194
193
|
};
|
|
195
194
|
var searchValueByKey = (result, id, key) => {
|
|
196
|
-
if (
|
|
195
|
+
if (isArray(result)) {
|
|
197
196
|
const index = computeMatchedItemIndex(id, result, key);
|
|
198
197
|
return index >= 0 ? result[index] : void 0;
|
|
199
198
|
}
|
|
@@ -257,18 +256,19 @@ var combineArrayData = (fieldArray, value, changingKey) => {
|
|
|
257
256
|
}
|
|
258
257
|
};
|
|
259
258
|
var setReactivityField = (field, key, value, type, insertBefore) => {
|
|
259
|
+
const fieldAny = field;
|
|
260
260
|
if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
261
|
-
|
|
261
|
+
fieldAny[key] = value;
|
|
262
262
|
return;
|
|
263
263
|
}
|
|
264
264
|
if (key !== enum_default.FIELD_DATA.RESULT_KEY) {
|
|
265
265
|
if (isArray(value)) {
|
|
266
|
-
const current =
|
|
266
|
+
const current = fieldAny[key];
|
|
267
267
|
const currentArr = isArray(current) ? current : [];
|
|
268
268
|
const newValue = insertBefore ? [...value, ...currentArr] : [...currentArr, ...value];
|
|
269
|
-
|
|
269
|
+
fieldAny[key] = newValue;
|
|
270
270
|
} else {
|
|
271
|
-
|
|
271
|
+
fieldAny[key] = value;
|
|
272
272
|
}
|
|
273
273
|
return;
|
|
274
274
|
}
|
|
@@ -276,24 +276,21 @@ var setReactivityField = (field, key, value, type, insertBefore) => {
|
|
|
276
276
|
const valueObj = value;
|
|
277
277
|
if (isArray(value)) {
|
|
278
278
|
const currentArr = isArray(resultField) ? resultField : [];
|
|
279
|
+
const valueArr = value;
|
|
279
280
|
if (insertBefore) {
|
|
280
|
-
if (
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
281
|
+
if (valueArr.length === 0) return;
|
|
283
282
|
if (currentArr.length === 0) {
|
|
284
|
-
field.result =
|
|
283
|
+
field.result = valueArr;
|
|
285
284
|
return;
|
|
286
285
|
}
|
|
287
|
-
field.result =
|
|
286
|
+
field.result = valueArr.concat(currentArr);
|
|
288
287
|
} else {
|
|
289
|
-
if (
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
288
|
+
if (valueArr.length === 0) return;
|
|
292
289
|
if (currentArr.length === 0) {
|
|
293
|
-
field.result =
|
|
290
|
+
field.result = valueArr;
|
|
294
291
|
return;
|
|
295
292
|
}
|
|
296
|
-
field.result = currentArr.concat(
|
|
293
|
+
field.result = currentArr.concat(valueArr);
|
|
297
294
|
}
|
|
298
295
|
return;
|
|
299
296
|
}
|
|
@@ -360,27 +357,28 @@ var generateRequestParams = ({
|
|
|
360
357
|
}) => {
|
|
361
358
|
const result = { ...query };
|
|
362
359
|
const isFetched = field.fetched;
|
|
360
|
+
const fieldResultAny = field.result;
|
|
363
361
|
const getSafeObjectKey = (item) => {
|
|
364
362
|
return extractUniqueKey(item, uniqueKey);
|
|
365
363
|
};
|
|
366
364
|
if (isFetched) {
|
|
367
365
|
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
368
|
-
if (
|
|
369
|
-
result.seen_ids = getSeenIdsString(
|
|
370
|
-
const targetIndex = query.is_up ? 0 :
|
|
371
|
-
const targetItem =
|
|
366
|
+
if (isArray(fieldResultAny)) {
|
|
367
|
+
result.seen_ids = getSeenIdsString(fieldResultAny, uniqueKey);
|
|
368
|
+
const targetIndex = query.is_up ? 0 : fieldResultAny.length - 1;
|
|
369
|
+
const targetItem = fieldResultAny[targetIndex];
|
|
372
370
|
result.since_id = getSafeObjectKey(targetItem);
|
|
373
371
|
}
|
|
374
372
|
result.is_up = query.is_up ? 1 : 0;
|
|
375
373
|
result.page = typeof query.page === "number" ? query.page : field.page + 1;
|
|
376
374
|
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
377
|
-
if (
|
|
378
|
-
result.seen_ids = getSeenIdsString(
|
|
375
|
+
if (isArray(fieldResultAny)) {
|
|
376
|
+
result.seen_ids = getSeenIdsString(fieldResultAny, uniqueKey);
|
|
379
377
|
}
|
|
380
378
|
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
381
|
-
if (
|
|
382
|
-
const targetIndex = query.is_up ? 0 :
|
|
383
|
-
const targetItem =
|
|
379
|
+
if (isArray(fieldResultAny)) {
|
|
380
|
+
const targetIndex = query.is_up ? 0 : fieldResultAny.length - 1;
|
|
381
|
+
const targetItem = fieldResultAny[targetIndex];
|
|
384
382
|
result.since_id = getSafeObjectKey(targetItem);
|
|
385
383
|
}
|
|
386
384
|
result.is_up = query.is_up ? 1 : 0;
|
|
@@ -408,6 +406,24 @@ var generateRequestParams = ({
|
|
|
408
406
|
}
|
|
409
407
|
return result;
|
|
410
408
|
};
|
|
409
|
+
var toObjectKey = (id) => {
|
|
410
|
+
if (id === void 0) return void 0;
|
|
411
|
+
if (isObjectKey(id)) return id;
|
|
412
|
+
if (isObjectKeyArray(id) && id.length > 0) return id[0];
|
|
413
|
+
return void 0;
|
|
414
|
+
};
|
|
415
|
+
var getResultAsArray = (field) => {
|
|
416
|
+
const result = field[enum_default.FIELD_DATA.RESULT_KEY];
|
|
417
|
+
return isArray(result) ? result : null;
|
|
418
|
+
};
|
|
419
|
+
var updateArrayItem = (arr, index, updater) => {
|
|
420
|
+
if (index >= 0 && index < arr.length) {
|
|
421
|
+
const item = arr[index];
|
|
422
|
+
if (isKeyMap(item)) {
|
|
423
|
+
arr[index] = updater(item);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
};
|
|
411
427
|
|
|
412
428
|
// src/setters.ts
|
|
413
429
|
var SET_DATA = ({
|
|
@@ -489,6 +505,39 @@ var SET_ERROR = ({ setter, fieldName, error }) => {
|
|
|
489
505
|
};
|
|
490
506
|
|
|
491
507
|
// src/actions.ts
|
|
508
|
+
var generateFieldName2 = ({
|
|
509
|
+
func,
|
|
510
|
+
query
|
|
511
|
+
}) => {
|
|
512
|
+
let result = func.id;
|
|
513
|
+
if (!query) return result;
|
|
514
|
+
const queryObj = query;
|
|
515
|
+
const filteredKeys = Object.keys(queryObj).filter((key) => !func.paramsIgnore.includes(key)).sort();
|
|
516
|
+
for (const key of filteredKeys) {
|
|
517
|
+
const value = queryObj[key];
|
|
518
|
+
const safeValue = typeof value === "object" && value !== null ? stableSerialize(value) : String(value);
|
|
519
|
+
result += `-${key}-${encodeURIComponent(safeValue)}`;
|
|
520
|
+
}
|
|
521
|
+
return result;
|
|
522
|
+
};
|
|
523
|
+
var createApi = (options) => {
|
|
524
|
+
const fn = ((params) => options.fetcher(params));
|
|
525
|
+
const metadata = {
|
|
526
|
+
id: options.id,
|
|
527
|
+
type: options.type || enum_default.FETCH_TYPE.SCROLL_LOAD_MORE,
|
|
528
|
+
uniqueKey: options.uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME,
|
|
529
|
+
paramsIgnore: [
|
|
530
|
+
"page",
|
|
531
|
+
"is_up",
|
|
532
|
+
"since_id",
|
|
533
|
+
"seen_ids",
|
|
534
|
+
"__refresh__",
|
|
535
|
+
"__reload__",
|
|
536
|
+
...options.paramsIgnore || []
|
|
537
|
+
]
|
|
538
|
+
};
|
|
539
|
+
return Object.freeze(Object.assign(fn, metadata));
|
|
540
|
+
};
|
|
492
541
|
var initState = ({
|
|
493
542
|
getter,
|
|
494
543
|
setter,
|
|
@@ -497,19 +546,13 @@ var initState = ({
|
|
|
497
546
|
opts
|
|
498
547
|
}) => {
|
|
499
548
|
return new Promise((resolve) => {
|
|
500
|
-
const fieldName =
|
|
501
|
-
|
|
502
|
-
if (fieldData) {
|
|
503
|
-
resolve();
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
549
|
+
const fieldName = generateFieldName2({ func, query });
|
|
550
|
+
if (getter(fieldName)) return resolve();
|
|
506
551
|
setter({
|
|
507
552
|
key: fieldName,
|
|
508
553
|
type: enum_default.SETTER_TYPE.RESET,
|
|
509
554
|
value: generateDefaultField(opts),
|
|
510
|
-
callback: () =>
|
|
511
|
-
resolve();
|
|
512
|
-
}
|
|
555
|
+
callback: () => resolve()
|
|
513
556
|
});
|
|
514
557
|
});
|
|
515
558
|
};
|
|
@@ -520,17 +563,17 @@ var initData = ({
|
|
|
520
563
|
query,
|
|
521
564
|
callback
|
|
522
565
|
}) => new Promise((resolve, reject) => {
|
|
523
|
-
const fieldName =
|
|
566
|
+
const fieldName = generateFieldName2({ func, query });
|
|
524
567
|
const fieldData = getter(fieldName);
|
|
525
|
-
const
|
|
526
|
-
const
|
|
527
|
-
const
|
|
568
|
+
const doRefresh = !!query?.__refresh__;
|
|
569
|
+
const needReset = !!query?.__reload__;
|
|
570
|
+
const directlyLoadData = doRefresh && !needReset;
|
|
571
|
+
if (fieldData && fieldData.error && !doRefresh) return resolve();
|
|
528
572
|
if (fieldData && fieldData.loading) return resolve();
|
|
529
573
|
if (fieldData && fieldData.fetched && !doRefresh) return resolve();
|
|
530
574
|
const params = generateRequestParams({
|
|
531
|
-
field: fieldData
|
|
575
|
+
field: generateDefaultField({ ...fieldData, fetched: false }),
|
|
532
576
|
uniqueKey: func.uniqueKey,
|
|
533
|
-
// 自动从契约获取
|
|
534
577
|
type: func.type,
|
|
535
578
|
query
|
|
536
579
|
});
|
|
@@ -546,15 +589,11 @@ var initData = ({
|
|
|
546
589
|
page: params.page || 0,
|
|
547
590
|
insertBefore: false
|
|
548
591
|
}).then(() => {
|
|
549
|
-
callback?.({
|
|
550
|
-
params,
|
|
551
|
-
data,
|
|
552
|
-
refresh: doRefresh
|
|
553
|
-
});
|
|
592
|
+
callback?.({ params, data, refresh: doRefresh });
|
|
554
593
|
resolve();
|
|
555
594
|
});
|
|
556
595
|
};
|
|
557
|
-
if (
|
|
596
|
+
if (directlyLoadData) {
|
|
558
597
|
setter({
|
|
559
598
|
key: fieldName,
|
|
560
599
|
type: enum_default.SETTER_TYPE.RESET,
|
|
@@ -569,16 +608,16 @@ var initData = ({
|
|
|
569
608
|
reject(error);
|
|
570
609
|
});
|
|
571
610
|
};
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
error: null
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
611
|
+
if (directlyLoadData) {
|
|
612
|
+
executeFetch();
|
|
613
|
+
} else {
|
|
614
|
+
setter({
|
|
615
|
+
key: fieldName,
|
|
616
|
+
type: enum_default.SETTER_TYPE.RESET,
|
|
617
|
+
value: { ...generateDefaultField(), loading: true, error: null },
|
|
618
|
+
callback: executeFetch
|
|
619
|
+
});
|
|
620
|
+
}
|
|
582
621
|
});
|
|
583
622
|
var loadMore = ({
|
|
584
623
|
getter,
|
|
@@ -588,40 +627,38 @@ var loadMore = ({
|
|
|
588
627
|
errorRetry,
|
|
589
628
|
callback
|
|
590
629
|
}) => new Promise((resolve, reject) => {
|
|
591
|
-
const fieldName =
|
|
630
|
+
const fieldName = generateFieldName2({ func, query });
|
|
592
631
|
const fieldData = getter(fieldName);
|
|
593
632
|
if (!fieldData || fieldData.loading || fieldData.nothing) return resolve();
|
|
594
633
|
if (fieldData.noMore && !errorRetry) return resolve();
|
|
634
|
+
const type = func.type;
|
|
635
|
+
if (type === enum_default.FETCH_TYPE.PAGINATION && query?.page != null && Number(query.page) === fieldData.page) {
|
|
636
|
+
return resolve();
|
|
637
|
+
}
|
|
638
|
+
const loadingState = type === enum_default.FETCH_TYPE.PAGINATION ? { loading: true, error: null, result: [], extra: null } : { loading: true, error: null };
|
|
595
639
|
const params = generateRequestParams({
|
|
596
640
|
field: fieldData,
|
|
597
641
|
uniqueKey: func.uniqueKey,
|
|
598
|
-
type
|
|
642
|
+
type,
|
|
599
643
|
query
|
|
600
644
|
});
|
|
601
|
-
if (fieldData.extra)
|
|
602
|
-
params[enum_default.FIELD_DATA.EXTRA_KEY] = fieldData.extra;
|
|
603
|
-
}
|
|
604
|
-
const queryAsParams = query;
|
|
645
|
+
if (fieldData.extra) params.extra = fieldData.extra;
|
|
605
646
|
setter({
|
|
606
647
|
key: fieldName,
|
|
607
648
|
type: enum_default.SETTER_TYPE.MERGE,
|
|
608
|
-
value:
|
|
649
|
+
value: loadingState,
|
|
609
650
|
callback: () => {
|
|
610
651
|
func(params).then((data) => {
|
|
611
652
|
SET_DATA({
|
|
612
653
|
getter,
|
|
613
654
|
setter,
|
|
614
655
|
data,
|
|
656
|
+
type,
|
|
615
657
|
fieldName,
|
|
616
|
-
type: func.type,
|
|
617
658
|
page: params.page || 0,
|
|
618
|
-
insertBefore: !!
|
|
659
|
+
insertBefore: !!query?.is_up
|
|
619
660
|
}).then(() => {
|
|
620
|
-
callback?.({
|
|
621
|
-
params,
|
|
622
|
-
data,
|
|
623
|
-
refresh: false
|
|
624
|
-
});
|
|
661
|
+
callback?.({ params, data, refresh: false });
|
|
625
662
|
resolve();
|
|
626
663
|
});
|
|
627
664
|
}).catch((error) => {
|
|
@@ -631,24 +668,6 @@ var loadMore = ({
|
|
|
631
668
|
}
|
|
632
669
|
});
|
|
633
670
|
});
|
|
634
|
-
var toObjectKey = (id) => {
|
|
635
|
-
if (id === void 0) return void 0;
|
|
636
|
-
if (isObjectKey(id)) return id;
|
|
637
|
-
if (isObjectKeyArray(id) && id.length > 0) return id[0];
|
|
638
|
-
return void 0;
|
|
639
|
-
};
|
|
640
|
-
var getResultAsArray = (field) => {
|
|
641
|
-
const result = field[enum_default.FIELD_DATA.RESULT_KEY];
|
|
642
|
-
return isResultArray(result) ? result : null;
|
|
643
|
-
};
|
|
644
|
-
var updateArrayItem = (arr, index, updater) => {
|
|
645
|
-
if (index >= 0 && index < arr.length) {
|
|
646
|
-
const item = arr[index];
|
|
647
|
-
if (isKeyMap(item)) {
|
|
648
|
-
arr[index] = updater(item);
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
};
|
|
652
671
|
var updateState = ({
|
|
653
672
|
getter,
|
|
654
673
|
setter,
|
|
@@ -660,7 +679,7 @@ var updateState = ({
|
|
|
660
679
|
changeKey
|
|
661
680
|
}) => {
|
|
662
681
|
return new Promise((resolve, reject) => {
|
|
663
|
-
const fieldName =
|
|
682
|
+
const fieldName = generateFieldName2({ func, query });
|
|
664
683
|
const fieldData = getter(fieldName);
|
|
665
684
|
if (!fieldData) {
|
|
666
685
|
reject(new Error(`Field ${fieldName} not found.`));
|
|
@@ -819,30 +838,9 @@ var updateState = ({
|
|
|
819
838
|
});
|
|
820
839
|
};
|
|
821
840
|
|
|
822
|
-
// src/types.ts
|
|
823
|
-
function createApi(options) {
|
|
824
|
-
const fn = Object.assign(
|
|
825
|
-
(params) => options.fetcher(params),
|
|
826
|
-
{
|
|
827
|
-
id: options.id,
|
|
828
|
-
type: options.type || enum_default.FETCH_TYPE.SCROLL_LOAD_MORE,
|
|
829
|
-
uniqueKey: options.uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME,
|
|
830
|
-
paramsIgnore: [
|
|
831
|
-
"page",
|
|
832
|
-
"is_up",
|
|
833
|
-
"since_id",
|
|
834
|
-
"seen_ids",
|
|
835
|
-
"__refresh__",
|
|
836
|
-
"__reload__",
|
|
837
|
-
...options.paramsIgnore || []
|
|
838
|
-
]
|
|
839
|
-
}
|
|
840
|
-
);
|
|
841
|
-
return Object.freeze(fn);
|
|
842
|
-
}
|
|
843
|
-
|
|
844
841
|
exports.ENUM = enum_default;
|
|
845
842
|
exports.createApi = createApi;
|
|
843
|
+
exports.generateFieldName = generateFieldName2;
|
|
846
844
|
exports.initData = initData;
|
|
847
845
|
exports.initState = initState;
|
|
848
846
|
exports.loadMore = loadMore;
|