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