@flowlist/js-core 4.0.6-beta.0 → 4.0.8-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 +59 -34
- package/dist/index.d.ts +59 -34
- package/dist/index.global.js +112 -69
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +112 -69
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +112 -69
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -67,8 +67,29 @@ var isArray = (data) => Array.isArray(data);
|
|
|
67
67
|
var isResultObject = (data) => !isArray(data) && typeof data === "object" && data !== null;
|
|
68
68
|
var isObjectKey = (value) => typeof value === "string" || typeof value === "number";
|
|
69
69
|
var isKeyMap = (value) => typeof value === "object" && value !== null && !isArray(value);
|
|
70
|
-
var isKeyMapArray = (value) =>
|
|
71
|
-
|
|
70
|
+
var isKeyMapArray = (value) => {
|
|
71
|
+
if (!isArray(value)) return false;
|
|
72
|
+
const len = value.length;
|
|
73
|
+
for (let i = 0; i < len; i++) {
|
|
74
|
+
const item = value[i];
|
|
75
|
+
if (typeof item !== "object" || item === null) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
};
|
|
81
|
+
var isObjectKeyArray = (value) => {
|
|
82
|
+
if (!isArray(value)) return false;
|
|
83
|
+
const len = value.length;
|
|
84
|
+
for (let i = 0; i < len; i++) {
|
|
85
|
+
const item = value[i];
|
|
86
|
+
const itemType = typeof item;
|
|
87
|
+
if (itemType !== "string" && itemType !== "number") {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
72
93
|
var stableSerialize = (value) => {
|
|
73
94
|
if (value === null || typeof value !== "object") {
|
|
74
95
|
return String(value);
|
|
@@ -129,16 +150,25 @@ var generateFieldName = ({
|
|
|
129
150
|
func,
|
|
130
151
|
query
|
|
131
152
|
}) => {
|
|
132
|
-
let result = func.id;
|
|
133
153
|
if (!query) {
|
|
134
|
-
return
|
|
154
|
+
return func.id;
|
|
135
155
|
}
|
|
136
|
-
|
|
137
|
-
const
|
|
156
|
+
let result = func.id;
|
|
157
|
+
const keys = Object.keys(query);
|
|
158
|
+
const paramsIgnore = func.paramsIgnore;
|
|
159
|
+
const filteredKeys = [];
|
|
160
|
+
const keysLen = keys.length;
|
|
161
|
+
for (let i = 0; i < keysLen; i++) {
|
|
162
|
+
const key = keys[i];
|
|
163
|
+
if (!paramsIgnore.includes(key)) {
|
|
164
|
+
filteredKeys.push(key);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
filteredKeys.sort();
|
|
138
168
|
const len = filteredKeys.length;
|
|
139
169
|
for (let i = 0; i < len; i++) {
|
|
140
170
|
const key = filteredKeys[i];
|
|
141
|
-
const value =
|
|
171
|
+
const value = query[key];
|
|
142
172
|
let safeValue;
|
|
143
173
|
if (typeof value === "object" && value !== null) {
|
|
144
174
|
safeValue = stableSerialize(value);
|
|
@@ -151,19 +181,15 @@ var generateFieldName = ({
|
|
|
151
181
|
return result;
|
|
152
182
|
};
|
|
153
183
|
var getObjectDeepValue = (field, keys) => {
|
|
154
|
-
if (!keys || isArray(keys) && keys.length === 0)
|
|
184
|
+
if (!keys || Array.isArray(keys) && keys.length === 0)
|
|
155
185
|
return field;
|
|
186
|
+
const keysArr = Array.isArray(keys) ? keys : keys.split(".");
|
|
187
|
+
let cur = field;
|
|
188
|
+
for (let i = 0, n = keysArr.length; i < n; ++i) {
|
|
189
|
+
if (cur == null || typeof cur !== "object") return void 0;
|
|
190
|
+
cur = cur[keysArr[i]];
|
|
156
191
|
}
|
|
157
|
-
|
|
158
|
-
let result = field;
|
|
159
|
-
const len = keysArr.length;
|
|
160
|
-
for (let i = 0; i < len; i++) {
|
|
161
|
-
if (result == null || typeof result !== "object") {
|
|
162
|
-
return void 0;
|
|
163
|
-
}
|
|
164
|
-
result = result[keysArr[i]];
|
|
165
|
-
}
|
|
166
|
-
return result;
|
|
192
|
+
return cur;
|
|
167
193
|
};
|
|
168
194
|
var updateObjectDeepValue = (field, changeKey, value) => {
|
|
169
195
|
if (!changeKey) return;
|
|
@@ -202,12 +228,23 @@ var searchValueByKey = (result, id, key) => {
|
|
|
202
228
|
var computeMatchedItemIndex = (itemId, fieldArr, changingKey) => {
|
|
203
229
|
const stringifiedItemId = String(itemId);
|
|
204
230
|
const len = fieldArr.length;
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
231
|
+
const isSimpleKey = !changingKey.includes(".");
|
|
232
|
+
if (isSimpleKey) {
|
|
233
|
+
for (let i = 0; i < len; i++) {
|
|
234
|
+
const item = fieldArr[i];
|
|
235
|
+
if (!isKeyMap(item)) continue;
|
|
236
|
+
if (String(item[changingKey]) === stringifiedItemId) {
|
|
237
|
+
return i;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
for (let i = 0; i < len; i++) {
|
|
242
|
+
const item = fieldArr[i];
|
|
243
|
+
if (!isKeyMap(item)) continue;
|
|
244
|
+
const itemValue = getObjectDeepValue(item, changingKey);
|
|
245
|
+
if (String(itemValue) === stringifiedItemId) {
|
|
246
|
+
return i;
|
|
247
|
+
}
|
|
211
248
|
}
|
|
212
249
|
}
|
|
213
250
|
return -1;
|
|
@@ -233,7 +270,7 @@ var combineArrayData = (fieldArray, value, changingKey) => {
|
|
|
233
270
|
if (index !== void 0) {
|
|
234
271
|
const existingItem = fieldArray[index];
|
|
235
272
|
if (isKeyMap(existingItem)) {
|
|
236
|
-
|
|
273
|
+
fieldArray[index] = { ...existingItem, ...col };
|
|
237
274
|
}
|
|
238
275
|
}
|
|
239
276
|
}
|
|
@@ -247,7 +284,7 @@ var combineArrayData = (fieldArray, value, changingKey) => {
|
|
|
247
284
|
if (index !== void 0) {
|
|
248
285
|
const existingItem = fieldArray[index];
|
|
249
286
|
if (isKeyMap(existingItem)) {
|
|
250
|
-
|
|
287
|
+
fieldArray[index] = { ...existingItem, ...col };
|
|
251
288
|
}
|
|
252
289
|
}
|
|
253
290
|
}
|
|
@@ -263,40 +300,29 @@ var setReactivityField = (field, key, value, type, insertBefore) => {
|
|
|
263
300
|
if (isArray(value)) {
|
|
264
301
|
const current = fieldAny[key];
|
|
265
302
|
const currentArr = isArray(current) ? current : [];
|
|
266
|
-
|
|
267
|
-
fieldAny[key] = newValue;
|
|
303
|
+
fieldAny[key] = insertBefore ? value.concat(currentArr) : currentArr.concat(value);
|
|
268
304
|
} else {
|
|
269
305
|
fieldAny[key] = value;
|
|
270
306
|
}
|
|
271
307
|
return;
|
|
272
308
|
}
|
|
273
309
|
const resultField = field.result;
|
|
274
|
-
const valueObj = value;
|
|
275
310
|
if (isArray(value)) {
|
|
276
|
-
const currentArr = isArray(resultField) ? resultField : [];
|
|
277
311
|
const valueArr = value;
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
} else {
|
|
286
|
-
if (valueArr.length === 0) return;
|
|
287
|
-
if (currentArr.length === 0) {
|
|
288
|
-
field.result = valueArr;
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
|
-
field.result = currentArr.concat(valueArr);
|
|
312
|
+
const valueLen = valueArr.length;
|
|
313
|
+
if (valueLen === 0) return;
|
|
314
|
+
const currentArr = isArray(resultField) ? resultField : [];
|
|
315
|
+
const currentLen = currentArr.length;
|
|
316
|
+
if (currentLen === 0) {
|
|
317
|
+
field.result = valueArr;
|
|
318
|
+
return;
|
|
292
319
|
}
|
|
320
|
+
field.result = insertBefore ? valueArr.concat(currentArr) : currentArr.concat(valueArr);
|
|
293
321
|
return;
|
|
294
322
|
}
|
|
323
|
+
const valueObj = value;
|
|
295
324
|
let target = resultField;
|
|
296
|
-
if (isArray(resultField)) {
|
|
297
|
-
target = {};
|
|
298
|
-
field.result = target;
|
|
299
|
-
} else if (typeof resultField !== "object" || resultField === null) {
|
|
325
|
+
if (isArray(resultField) || typeof resultField !== "object" || resultField === null) {
|
|
300
326
|
target = {};
|
|
301
327
|
field.result = target;
|
|
302
328
|
}
|
|
@@ -307,10 +333,10 @@ var setReactivityField = (field, key, value, type, insertBefore) => {
|
|
|
307
333
|
const existing = target[subKey];
|
|
308
334
|
const incoming = valueObj[subKey];
|
|
309
335
|
if (existing !== void 0) {
|
|
310
|
-
if (
|
|
311
|
-
target[subKey] =
|
|
336
|
+
if (isArray(existing) && isArray(incoming)) {
|
|
337
|
+
target[subKey] = insertBefore ? incoming.concat(existing) : existing.concat(incoming);
|
|
312
338
|
} else {
|
|
313
|
-
target[subKey] =
|
|
339
|
+
target[subKey] = incoming;
|
|
314
340
|
}
|
|
315
341
|
} else {
|
|
316
342
|
target[subKey] = incoming;
|
|
@@ -351,6 +377,7 @@ var generateRequestParams = ({
|
|
|
351
377
|
field,
|
|
352
378
|
uniqueKey = enum_default.DEFAULT_UNIQUE_KEY_NAME,
|
|
353
379
|
query = {},
|
|
380
|
+
is_up = false,
|
|
354
381
|
type
|
|
355
382
|
}) => {
|
|
356
383
|
const result = { ...query };
|
|
@@ -363,11 +390,11 @@ var generateRequestParams = ({
|
|
|
363
390
|
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
364
391
|
if (isArray(fieldResultAny)) {
|
|
365
392
|
result.seen_ids = getSeenIdsString(fieldResultAny, uniqueKey);
|
|
366
|
-
const targetIndex =
|
|
393
|
+
const targetIndex = is_up ? 0 : fieldResultAny.length - 1;
|
|
367
394
|
const targetItem = fieldResultAny[targetIndex];
|
|
368
395
|
result.since_id = getSafeObjectKey(targetItem);
|
|
369
396
|
}
|
|
370
|
-
result.is_up =
|
|
397
|
+
result.is_up = is_up ? 1 : 0;
|
|
371
398
|
result.page = typeof query.page === "number" ? query.page : field.page + 1;
|
|
372
399
|
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
373
400
|
if (isArray(fieldResultAny)) {
|
|
@@ -375,11 +402,11 @@ var generateRequestParams = ({
|
|
|
375
402
|
}
|
|
376
403
|
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
377
404
|
if (isArray(fieldResultAny)) {
|
|
378
|
-
const targetIndex =
|
|
405
|
+
const targetIndex = is_up ? 0 : fieldResultAny.length - 1;
|
|
379
406
|
const targetItem = fieldResultAny[targetIndex];
|
|
380
407
|
result.since_id = getSafeObjectKey(targetItem);
|
|
381
408
|
}
|
|
382
|
-
result.is_up =
|
|
409
|
+
result.is_up = is_up ? 1 : 0;
|
|
383
410
|
} else if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
384
411
|
result.page = typeof query.page === "number" ? query.page : void 0;
|
|
385
412
|
} else if (type === enum_default.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
@@ -388,14 +415,14 @@ var generateRequestParams = ({
|
|
|
388
415
|
} else {
|
|
389
416
|
if (type === enum_default.FETCH_TYPE.AUTO) {
|
|
390
417
|
result.seen_ids = "";
|
|
391
|
-
result.since_id = isObjectKey(query.sinceId) ? query.sinceId :
|
|
392
|
-
result.is_up =
|
|
418
|
+
result.since_id = isObjectKey(query.sinceId) ? query.sinceId : "";
|
|
419
|
+
result.is_up = is_up ? 1 : 0;
|
|
393
420
|
result.page = typeof query.page === "number" ? query.page : field.page || 1;
|
|
394
421
|
} else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
395
422
|
result.seen_ids = "";
|
|
396
423
|
} else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
397
|
-
result.since_id = isObjectKey(query.sinceId) ? query.sinceId :
|
|
398
|
-
result.is_up =
|
|
424
|
+
result.since_id = isObjectKey(query.sinceId) ? query.sinceId : "";
|
|
425
|
+
result.is_up = is_up ? 1 : 0;
|
|
399
426
|
} else if (type === enum_default.FETCH_TYPE.PAGINATION) {
|
|
400
427
|
result.page = typeof query.page === "number" ? query.page : field.page;
|
|
401
428
|
} else if (type === enum_default.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
@@ -524,9 +551,9 @@ var createApi = (options) => {
|
|
|
524
551
|
id: options.id,
|
|
525
552
|
type: options.type || enum_default.FETCH_TYPE.SCROLL_LOAD_MORE,
|
|
526
553
|
uniqueKey: options.uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME,
|
|
554
|
+
is_up: options.is_up || false,
|
|
527
555
|
paramsIgnore: [
|
|
528
556
|
"page",
|
|
529
|
-
"is_up",
|
|
530
557
|
"since_id",
|
|
531
558
|
"seen_ids",
|
|
532
559
|
"__refresh__",
|
|
@@ -573,6 +600,7 @@ var initData = ({
|
|
|
573
600
|
field: generateDefaultField({ ...fieldData, fetched: false }),
|
|
574
601
|
uniqueKey: func.uniqueKey,
|
|
575
602
|
type: func.type,
|
|
603
|
+
is_up: func.is_up,
|
|
576
604
|
query
|
|
577
605
|
});
|
|
578
606
|
const executeFetch = () => {
|
|
@@ -638,6 +666,7 @@ var loadMore = ({
|
|
|
638
666
|
field: fieldData,
|
|
639
667
|
uniqueKey: func.uniqueKey,
|
|
640
668
|
type,
|
|
669
|
+
is_up: func.is_up,
|
|
641
670
|
query
|
|
642
671
|
});
|
|
643
672
|
if (fieldData.extra) params.extra = fieldData.extra;
|
|
@@ -654,7 +683,7 @@ var loadMore = ({
|
|
|
654
683
|
type,
|
|
655
684
|
fieldName,
|
|
656
685
|
page: params.page || 0,
|
|
657
|
-
insertBefore:
|
|
686
|
+
insertBefore: func.is_up
|
|
658
687
|
}).then(() => {
|
|
659
688
|
callback?.({ params, data, refresh: false });
|
|
660
689
|
resolve();
|
|
@@ -694,7 +723,11 @@ var updateState = ({
|
|
|
694
723
|
fieldData[enum_default.FIELD_DATA.RESULT_KEY]
|
|
695
724
|
);
|
|
696
725
|
const newFieldData = { ...fieldData };
|
|
697
|
-
|
|
726
|
+
let resultArray = getResultAsArray(fieldData);
|
|
727
|
+
if (resultArray) {
|
|
728
|
+
resultArray = [...resultArray];
|
|
729
|
+
newFieldData.result = resultArray;
|
|
730
|
+
}
|
|
698
731
|
if (method === enum_default.CHANGE_TYPE.SEARCH_FIELD) {
|
|
699
732
|
const objectKeyId = toObjectKey(_id);
|
|
700
733
|
if (objectKeyId === void 0) {
|
|
@@ -717,7 +750,9 @@ var updateState = ({
|
|
|
717
750
|
_uniqueKey
|
|
718
751
|
);
|
|
719
752
|
if (matchedIndex >= 0 && isKeyMap(resultArray[matchedIndex])) {
|
|
720
|
-
|
|
753
|
+
const newItem = { ...resultArray[matchedIndex] };
|
|
754
|
+
updateObjectDeepValue(newItem, _changeKey, value);
|
|
755
|
+
resultArray[matchedIndex] = newItem;
|
|
721
756
|
}
|
|
722
757
|
}
|
|
723
758
|
resolve(null);
|
|
@@ -749,7 +784,7 @@ var updateState = ({
|
|
|
749
784
|
} else {
|
|
750
785
|
let modifyValue;
|
|
751
786
|
if (_changeKey === enum_default.FIELD_DATA.RESULT_KEY) {
|
|
752
|
-
modifyValue = newFieldData.result;
|
|
787
|
+
modifyValue = resultArray || newFieldData.result;
|
|
753
788
|
} else if (_changeKey === enum_default.FIELD_DATA.EXTRA_KEY) {
|
|
754
789
|
modifyValue = newFieldData.extra;
|
|
755
790
|
} else {
|
|
@@ -774,7 +809,9 @@ var updateState = ({
|
|
|
774
809
|
case enum_default.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
|
|
775
810
|
if (isKeyMapArray(modifyValue)) {
|
|
776
811
|
if (matchedIndex >= 0) {
|
|
777
|
-
modifyValue
|
|
812
|
+
const newArray = [...modifyValue];
|
|
813
|
+
newArray.splice(matchedIndex, 1);
|
|
814
|
+
modifyValue = newArray;
|
|
778
815
|
} else if (isObjectKeyArray(_id)) {
|
|
779
816
|
const idSet = new Set(_id);
|
|
780
817
|
modifyValue = modifyValue.filter((item) => {
|
|
@@ -786,18 +823,23 @@ var updateState = ({
|
|
|
786
823
|
break;
|
|
787
824
|
case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
|
|
788
825
|
if (isArray(modifyValue) && matchedIndex >= 0) {
|
|
789
|
-
|
|
826
|
+
const newArray = [...modifyValue];
|
|
827
|
+
newArray.splice(matchedIndex, 0, value);
|
|
828
|
+
modifyValue = newArray;
|
|
790
829
|
}
|
|
791
830
|
break;
|
|
792
831
|
case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
|
|
793
832
|
if (isArray(modifyValue) && matchedIndex >= 0) {
|
|
794
|
-
|
|
833
|
+
const newArray = [...modifyValue];
|
|
834
|
+
newArray.splice(matchedIndex + 1, 0, value);
|
|
835
|
+
modifyValue = newArray;
|
|
795
836
|
}
|
|
796
837
|
break;
|
|
797
838
|
case enum_default.CHANGE_TYPE.RESULT_LIST_MERGE:
|
|
798
839
|
if (isKeyMapArray(modifyValue)) {
|
|
840
|
+
const newArray = [...modifyValue];
|
|
799
841
|
if (isKeyMapArray(value)) {
|
|
800
|
-
combineArrayData(
|
|
842
|
+
combineArrayData(newArray, value, _uniqueKey);
|
|
801
843
|
} else if (isKeyMap(value)) {
|
|
802
844
|
const valueAsRecord = {};
|
|
803
845
|
for (const [k, v] of Object.entries(value)) {
|
|
@@ -805,8 +847,9 @@ var updateState = ({
|
|
|
805
847
|
valueAsRecord[k] = v;
|
|
806
848
|
}
|
|
807
849
|
}
|
|
808
|
-
combineArrayData(
|
|
850
|
+
combineArrayData(newArray, valueAsRecord, _uniqueKey);
|
|
809
851
|
}
|
|
852
|
+
modifyValue = newArray;
|
|
810
853
|
}
|
|
811
854
|
break;
|
|
812
855
|
default:
|