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