@flowlist/js-core 2.3.1 → 3.0.1

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.mjs ADDED
@@ -0,0 +1,763 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/utils.ts
8
+ var utils_exports = {};
9
+ __export(utils_exports, {
10
+ combineArrayData: () => combineArrayData,
11
+ computeMatchedItemIndex: () => computeMatchedItemIndex,
12
+ computeResultLength: () => computeResultLength,
13
+ generateDefaultField: () => generateDefaultField,
14
+ generateFieldName: () => generateFieldName,
15
+ generateRequestParams: () => generateRequestParams,
16
+ getObjectDeepValue: () => getObjectDeepValue,
17
+ isArray: () => isArray,
18
+ isObjectResult: () => isObjectResult,
19
+ searchValueByKey: () => searchValueByKey,
20
+ setReactivityField: () => setReactivityField,
21
+ updateObjectDeepValue: () => updateObjectDeepValue
22
+ });
23
+
24
+ // src/enum.ts
25
+ var FETCH_TYPE_ARRAY = ["jump", "sinceId", "page", "seenIds", "auto"];
26
+ var enum_default = {
27
+ SETTER_TYPE: {
28
+ RESET: 0,
29
+ MERGE: 1
30
+ },
31
+ FETCH_TYPE_ARRAY,
32
+ FETCH_TYPE: {
33
+ PAGINATION: FETCH_TYPE_ARRAY[0],
34
+ SINCE_FIRST_OR_END_ID: FETCH_TYPE_ARRAY[1],
35
+ SCROLL_LOAD_MORE: FETCH_TYPE_ARRAY[2],
36
+ HAS_LOADED_IDS: FETCH_TYPE_ARRAY[3],
37
+ AUTO: FETCH_TYPE_ARRAY[4]
38
+ },
39
+ CHANGE_TYPE: {
40
+ SEARCH_FIELD: "search",
41
+ RESET_FIELD: "reset",
42
+ RESULT_UPDATE_KV: "update",
43
+ RESULT_ADD_AFTER: "push",
44
+ RESULT_ADD_BEFORE: "unshift",
45
+ RESULT_REMOVE_BY_ID: "delete",
46
+ RESULT_INSERT_TO_BEFORE: "insert-before",
47
+ RESULT_INSERT_TO_AFTER: "insert-after",
48
+ RESULT_LIST_MERGE: "patch",
49
+ RESULT_ITEM_MERGE: "merge"
50
+ },
51
+ FIELD_DATA: {
52
+ RESULT_KEY: "result",
53
+ EXTRA_KEY: "extra"
54
+ },
55
+ DEFAULT_UNIQUE_KEY_NAME: "id"
56
+ };
57
+
58
+ // src/utils.ts
59
+ var isObjectResult = (data) => {
60
+ if (typeof data !== "object" || data === null) {
61
+ return false;
62
+ }
63
+ return !Object.prototype.hasOwnProperty.call(data, "result");
64
+ };
65
+ var generateDefaultField = (opts = {}) => ({
66
+ ...{
67
+ result: [],
68
+ // 用户需要确保 T 是兼容的类型,例如 T extends unknown[]
69
+ noMore: false,
70
+ nothing: false,
71
+ loading: false,
72
+ error: null,
73
+ extra: null,
74
+ fetched: false,
75
+ page: 0,
76
+ total: 0
77
+ },
78
+ ...opts
79
+ });
80
+ var generateFieldName = ({
81
+ func,
82
+ type,
83
+ query = {}
84
+ }) => {
85
+ const funcName = typeof func === "string" ? func : `api-${Math.random().toString(36).substring(2)}`;
86
+ const fetchType = type || "auto";
87
+ let result = `${funcName}-${fetchType}`;
88
+ const filteredKeys = Object.keys(query).filter(
89
+ (key) => !["undefined", "object", "function"].includes(typeof query[key]) && ![
90
+ "page",
91
+ "is_up",
92
+ "since_id",
93
+ "seen_ids",
94
+ "__refresh__",
95
+ "__reload__"
96
+ ].includes(key)
97
+ );
98
+ filteredKeys.sort().forEach((key) => {
99
+ result += `-${key}-${query[key]}`;
100
+ });
101
+ return result;
102
+ };
103
+ var getObjectDeepValue = (field, keys = "") => {
104
+ if (!keys || Array.isArray(keys) && keys.length === 0) {
105
+ return field;
106
+ }
107
+ const keysArr = Array.isArray(keys) ? keys : keys.split(".");
108
+ let result = field;
109
+ for (const key of keysArr) {
110
+ if (result == null || typeof result !== "object") {
111
+ return void 0;
112
+ }
113
+ result = result[key];
114
+ }
115
+ return result;
116
+ };
117
+ var updateObjectDeepValue = (field, changeKey, value) => {
118
+ if (!changeKey) return;
119
+ const keys = changeKey.split(".");
120
+ const lastKey = keys.pop();
121
+ let current = field;
122
+ for (const key of keys) {
123
+ if (current == null || typeof current !== "object") {
124
+ current[key] = {};
125
+ }
126
+ current = current[key];
127
+ }
128
+ if (current != null && typeof current === "object") {
129
+ current[lastKey] = value;
130
+ }
131
+ };
132
+ var searchValueByKey = (result, id, key) => {
133
+ if (isArray(result)) {
134
+ const index = computeMatchedItemIndex(id, result, key);
135
+ return index >= 0 ? result[index] : void 0;
136
+ } else {
137
+ return result[id];
138
+ }
139
+ };
140
+ var computeMatchedItemIndex = (itemId, fieldArr, changingKey) => {
141
+ const stringifiedItemId = String(itemId);
142
+ const len = fieldArr?.length;
143
+ if (typeof len !== "number" || len <= 0) return -1;
144
+ for (let i = 0; i < len; i++) {
145
+ const item = fieldArr[i];
146
+ if (typeof item !== "object" || item === null) continue;
147
+ const itemValue = getObjectDeepValue(item, changingKey);
148
+ if (String(itemValue) === stringifiedItemId) {
149
+ return i;
150
+ }
151
+ }
152
+ return -1;
153
+ };
154
+ var combineArrayData = (fieldArray, value, changingKey) => {
155
+ if (isArray(value)) {
156
+ for (const col of value) {
157
+ if (typeof col !== "object" || col === null) continue;
158
+ const stringifyId = String(getObjectDeepValue(col, changingKey));
159
+ const index = fieldArray.findIndex(
160
+ (item) => typeof item === "object" && item !== null && String(getObjectDeepValue(item, changingKey)) === stringifyId
161
+ );
162
+ if (index !== -1) {
163
+ fieldArray[index] = { ...fieldArray[index], ...col };
164
+ }
165
+ }
166
+ } else {
167
+ for (const [uniqueId, col] of Object.entries(value)) {
168
+ if (typeof col !== "object" || col === null) continue;
169
+ const stringifyId = String(uniqueId);
170
+ const index = fieldArray.findIndex(
171
+ (item) => typeof item === "object" && item !== null && String(getObjectDeepValue(item, changingKey)) === stringifyId
172
+ );
173
+ if (index !== -1) {
174
+ fieldArray[index] = { ...fieldArray[index], ...col };
175
+ }
176
+ }
177
+ }
178
+ };
179
+ var isArray = (data) => {
180
+ return Array.isArray(data);
181
+ };
182
+ var setReactivityField = (field, key, value, type, insertBefore) => {
183
+ if (type === enum_default.FETCH_TYPE.PAGINATION) {
184
+ field[key] = value;
185
+ return;
186
+ }
187
+ if (isArray(value)) {
188
+ const current = field[key];
189
+ const currentArr = isArray(current) ? current : [];
190
+ const newValue = insertBefore ? value.concat(currentArr) : currentArr.concat(value);
191
+ field[key] = newValue;
192
+ return;
193
+ }
194
+ if (key !== enum_default.FIELD_DATA.RESULT_KEY) {
195
+ field[key] = value;
196
+ return;
197
+ }
198
+ const resultField = field.result;
199
+ if (isArray(resultField)) {
200
+ field.result = {};
201
+ }
202
+ const valueObj = value;
203
+ const target = field.result;
204
+ Object.keys(valueObj).forEach((subKey) => {
205
+ const existing = target[subKey];
206
+ const incoming = valueObj[subKey];
207
+ if (existing !== void 0) {
208
+ if (insertBefore) {
209
+ target[subKey] = isArray(incoming) && isArray(existing) ? incoming.concat(existing) : incoming;
210
+ } else {
211
+ target[subKey] = isArray(existing) && isArray(incoming) ? existing.concat(incoming) : incoming;
212
+ }
213
+ } else {
214
+ target[subKey] = incoming;
215
+ }
216
+ });
217
+ };
218
+ var computeResultLength = (data) => {
219
+ if (isArray(data)) {
220
+ return data.length;
221
+ }
222
+ if (data && typeof data === "object") {
223
+ return Object.values(data).reduce((acc, val) => {
224
+ if (isArray(val)) {
225
+ return acc + val.length;
226
+ }
227
+ return acc;
228
+ }, 0);
229
+ }
230
+ return 0;
231
+ };
232
+ var generateRequestParams = ({
233
+ field,
234
+ uniqueKey,
235
+ query = {},
236
+ type
237
+ }) => {
238
+ const result = {};
239
+ const changing = uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
240
+ const isFetched = field.fetched;
241
+ const getSafeObjectKey = (item) => {
242
+ if (typeof item !== "object" || item === null) return void 0;
243
+ const val = getObjectDeepValue(item, changing);
244
+ if (typeof val === "string" || typeof val === "number") {
245
+ return val;
246
+ }
247
+ return void 0;
248
+ };
249
+ if (isFetched) {
250
+ if (type === enum_default.FETCH_TYPE.AUTO) {
251
+ if (isArray(field.result)) {
252
+ result.seen_ids = field.result.map((item) => getSafeObjectKey(item)).filter((id) => id !== void 0).join(",");
253
+ const targetIndex = query.is_up ? 0 : field.result.length - 1;
254
+ const targetItem = field.result[targetIndex];
255
+ result.since_id = getSafeObjectKey(targetItem);
256
+ }
257
+ result.is_up = query.is_up ? 1 : 0;
258
+ result.page = typeof query.page === "number" ? query.page : field.page + 1;
259
+ } else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
260
+ if (isArray(field.result)) {
261
+ result.seen_ids = field.result.map((item) => getSafeObjectKey(item)).filter((id) => id !== void 0).join(",");
262
+ }
263
+ } else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
264
+ if (isArray(field.result)) {
265
+ const targetIndex = query.is_up ? 0 : field.result.length - 1;
266
+ const targetItem = field.result[targetIndex];
267
+ result.since_id = getSafeObjectKey(targetItem);
268
+ }
269
+ result.is_up = query.is_up ? 1 : 0;
270
+ } else if (type === enum_default.FETCH_TYPE.PAGINATION) {
271
+ result.page = typeof query.page === "number" ? query.page : void 0;
272
+ } else if (type === enum_default.FETCH_TYPE.SCROLL_LOAD_MORE) {
273
+ result.page = field.page + 1;
274
+ }
275
+ } else {
276
+ if (type === enum_default.FETCH_TYPE.AUTO) {
277
+ result.seen_ids = "";
278
+ result.since_id = typeof query.sinceId === "string" || typeof query.sinceId === "number" ? query.sinceId : query.is_up ? 999999999 : 0;
279
+ result.is_up = query.is_up ? 1 : 0;
280
+ result.page = typeof query.page === "number" ? query.page : field.page || 1;
281
+ } else if (type === enum_default.FETCH_TYPE.HAS_LOADED_IDS) {
282
+ result.seen_ids = "";
283
+ } else if (type === enum_default.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
284
+ result.since_id = typeof query.sinceId === "string" || typeof query.sinceId === "number" ? query.sinceId : query.is_up ? 999999999 : 0;
285
+ result.is_up = query.is_up ? 1 : 0;
286
+ } else if (type === enum_default.FETCH_TYPE.PAGINATION) {
287
+ result.page = typeof query.page === "number" ? query.page : field.page;
288
+ } else if (type === enum_default.FETCH_TYPE.SCROLL_LOAD_MORE) {
289
+ result.page = 1;
290
+ }
291
+ }
292
+ return {
293
+ ...query,
294
+ ...result
295
+ };
296
+ };
297
+
298
+ // src/setters.ts
299
+ var SET_DATA = ({
300
+ getter,
301
+ setter,
302
+ data,
303
+ fieldName,
304
+ type,
305
+ page,
306
+ insertBefore
307
+ }) => {
308
+ return new Promise((resolve, reject) => {
309
+ const fieldData = getter(fieldName);
310
+ if (!fieldData) {
311
+ reject(new Error(`Field ${fieldName} not found.`));
312
+ return;
313
+ }
314
+ const field = fieldData;
315
+ let result;
316
+ let extra;
317
+ if (isObjectResult(data)) {
318
+ result = data;
319
+ field.nothing = false;
320
+ field.fetched = true;
321
+ field.noMore = true;
322
+ field.page = -1;
323
+ } else {
324
+ const apiResponse = data;
325
+ result = apiResponse.result;
326
+ extra = apiResponse.extra;
327
+ const isEmpty = computeResultLength(result) === 0;
328
+ field.nothing = field.fetched ? false : isEmpty;
329
+ field.fetched = true;
330
+ field.total = apiResponse.total || 0;
331
+ if (type === enum_default.FETCH_TYPE.PAGINATION) {
332
+ field.noMore = false;
333
+ field.page = +page;
334
+ } else {
335
+ field.noMore = typeof apiResponse.no_more === "undefined" ? isEmpty : apiResponse.no_more || isEmpty;
336
+ field.page = field.page + 1;
337
+ }
338
+ }
339
+ field.loading = false;
340
+ setReactivityField(
341
+ field,
342
+ enum_default.FIELD_DATA.RESULT_KEY,
343
+ result,
344
+ type,
345
+ insertBefore
346
+ );
347
+ if (extra !== void 0 && extra !== null) {
348
+ setReactivityField(
349
+ field,
350
+ enum_default.FIELD_DATA.EXTRA_KEY,
351
+ extra,
352
+ type,
353
+ insertBefore
354
+ );
355
+ }
356
+ setter({
357
+ key: fieldName,
358
+ type: enum_default.SETTER_TYPE.RESET,
359
+ // or MUTATE if you have such type
360
+ value: field,
361
+ // same reference
362
+ callback: () => {
363
+ resolve();
364
+ }
365
+ });
366
+ });
367
+ };
368
+ var SET_ERROR = ({ setter, fieldName, error }) => {
369
+ setter({
370
+ key: fieldName,
371
+ type: enum_default.SETTER_TYPE.MERGE,
372
+ value: {
373
+ error,
374
+ loading: false
375
+ }
376
+ });
377
+ };
378
+
379
+ // src/actions.ts
380
+ var initState = ({
381
+ getter,
382
+ setter,
383
+ func,
384
+ // string only
385
+ type,
386
+ query,
387
+ opts
388
+ }) => {
389
+ return new Promise((resolve) => {
390
+ const fieldName = generateFieldName({ func, type, query });
391
+ const fieldData = getter(fieldName);
392
+ if (fieldData) {
393
+ resolve();
394
+ return;
395
+ }
396
+ setter({
397
+ key: fieldName,
398
+ type: enum_default.SETTER_TYPE.RESET,
399
+ value: generateDefaultField(opts),
400
+ callback: () => {
401
+ resolve();
402
+ }
403
+ });
404
+ });
405
+ };
406
+ var initData = ({
407
+ getter,
408
+ setter,
409
+ func,
410
+ // string | function
411
+ type,
412
+ query,
413
+ api,
414
+ uniqueKey,
415
+ callback
416
+ }) => new Promise((resolve, reject) => {
417
+ const fieldName = generateFieldName({ func, type, query });
418
+ const fieldData = getter(fieldName);
419
+ const doRefresh = !!query?.__refresh__;
420
+ const needReset = !!query?.__reload__;
421
+ if (fieldData && fieldData.error && !doRefresh) {
422
+ resolve();
423
+ return;
424
+ }
425
+ if (fieldData && fieldData.loading) {
426
+ resolve();
427
+ return;
428
+ }
429
+ const dontFetch = fieldData && fieldData.fetched && !doRefresh;
430
+ if (dontFetch) {
431
+ resolve();
432
+ return;
433
+ }
434
+ const params = generateRequestParams({
435
+ field: {
436
+ ...fieldData || generateDefaultField(),
437
+ fetched: false
438
+ },
439
+ uniqueKey,
440
+ query,
441
+ type
442
+ });
443
+ const getData = () => {
444
+ const loadData = () => new Promise((res, rej) => {
445
+ const getDataFromAPI = () => {
446
+ const funcCaller = typeof func === "string" && api ? api[func] : func;
447
+ if (typeof funcCaller === "function") {
448
+ funcCaller(params).then(res).catch((error) => {
449
+ SET_ERROR({ setter, fieldName, error });
450
+ rej(error);
451
+ });
452
+ }
453
+ };
454
+ getDataFromAPI();
455
+ });
456
+ loadData().then((data) => {
457
+ const setData = () => {
458
+ SET_DATA({
459
+ getter,
460
+ setter,
461
+ data,
462
+ fieldName,
463
+ type,
464
+ page: params.page || 0,
465
+ insertBefore: false
466
+ }).then(() => {
467
+ if (callback) {
468
+ callback({
469
+ params,
470
+ data,
471
+ // <-- 'data' is now properly in scope
472
+ refresh: doRefresh
473
+ });
474
+ }
475
+ resolve();
476
+ });
477
+ };
478
+ if (needReset) {
479
+ setter({
480
+ key: fieldName,
481
+ type: enum_default.SETTER_TYPE.RESET,
482
+ value: generateDefaultField(),
483
+ callback: setData
484
+ });
485
+ } else {
486
+ setData();
487
+ }
488
+ }).catch(reject);
489
+ };
490
+ if (!dontFetch && !needReset) {
491
+ setter({
492
+ key: fieldName,
493
+ type: enum_default.SETTER_TYPE.RESET,
494
+ value: {
495
+ ...generateDefaultField(),
496
+ loading: true,
497
+ error: null
498
+ },
499
+ callback: getData
500
+ });
501
+ } else {
502
+ getData();
503
+ }
504
+ });
505
+ var loadMore = ({
506
+ getter,
507
+ setter,
508
+ query,
509
+ type,
510
+ func,
511
+ // string | function
512
+ api,
513
+ uniqueKey,
514
+ errorRetry,
515
+ callback
516
+ }) => new Promise((resolve, reject) => {
517
+ const fieldName = generateFieldName({ func, type, query });
518
+ const fieldData = getter(fieldName);
519
+ if (!fieldData) {
520
+ resolve();
521
+ return;
522
+ }
523
+ if (fieldData.loading) {
524
+ resolve();
525
+ return;
526
+ }
527
+ if (fieldData.nothing) {
528
+ resolve();
529
+ return;
530
+ }
531
+ if (fieldData.noMore && !errorRetry) {
532
+ resolve();
533
+ return;
534
+ }
535
+ if (type === enum_default.FETCH_TYPE.PAGINATION && query && query.page != null && +query.page === fieldData.page) {
536
+ resolve();
537
+ return;
538
+ }
539
+ let loadingState;
540
+ if (type === enum_default.FETCH_TYPE.PAGINATION) {
541
+ loadingState = {
542
+ loading: true,
543
+ error: null,
544
+ [enum_default.FIELD_DATA.RESULT_KEY]: [],
545
+ [enum_default.FIELD_DATA.EXTRA_KEY]: null
546
+ };
547
+ } else {
548
+ loadingState = {
549
+ loading: true,
550
+ error: null
551
+ };
552
+ }
553
+ const params = generateRequestParams({
554
+ field: fieldData,
555
+ uniqueKey,
556
+ query,
557
+ type
558
+ });
559
+ if ("extra" in fieldData) {
560
+ params[enum_default.FIELD_DATA.EXTRA_KEY] = fieldData.extra;
561
+ }
562
+ const getData = () => {
563
+ const funcCaller = typeof func === "string" && api ? api[func] : func;
564
+ if (typeof funcCaller === "function") {
565
+ funcCaller(params).then((data) => {
566
+ SET_DATA({
567
+ getter,
568
+ setter,
569
+ data,
570
+ fieldName,
571
+ type,
572
+ page: params.page || 0,
573
+ insertBefore: !!query?.is_up
574
+ }).then(() => {
575
+ if (callback) {
576
+ callback({
577
+ params,
578
+ data,
579
+ refresh: false
580
+ });
581
+ }
582
+ resolve();
583
+ });
584
+ }).catch((error) => {
585
+ SET_ERROR({ setter, fieldName, error });
586
+ reject(error);
587
+ });
588
+ }
589
+ };
590
+ setter({
591
+ key: fieldName,
592
+ type: enum_default.SETTER_TYPE.MERGE,
593
+ value: loadingState,
594
+ callback: getData
595
+ });
596
+ });
597
+ var updateState = ({
598
+ getter,
599
+ setter,
600
+ func,
601
+ // string only
602
+ type,
603
+ query,
604
+ method,
605
+ value,
606
+ id,
607
+ uniqueKey,
608
+ changeKey
609
+ }) => {
610
+ return new Promise((resolve, reject) => {
611
+ const fieldName = generateFieldName({ func, type, query });
612
+ const fieldData = getter(fieldName);
613
+ if (!fieldData) {
614
+ reject(new Error(`Field ${fieldName} not found.`));
615
+ return;
616
+ }
617
+ if (fieldData.page === -1) {
618
+ resolve(null);
619
+ return;
620
+ }
621
+ const _id = id;
622
+ const _uniqueKey = uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
623
+ const _changeKey = changeKey || enum_default.FIELD_DATA.RESULT_KEY;
624
+ const beforeLength = computeResultLength(
625
+ fieldData[enum_default.FIELD_DATA.RESULT_KEY]
626
+ );
627
+ const newFieldData = { ...fieldData };
628
+ if (method === enum_default.CHANGE_TYPE.SEARCH_FIELD) {
629
+ if (_id == null) {
630
+ reject(new Error("ID is required for SEARCH_FIELD."));
631
+ return;
632
+ }
633
+ const result = searchValueByKey(
634
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
635
+ _id,
636
+ _uniqueKey
637
+ );
638
+ resolve(result);
639
+ } else if (method === enum_default.CHANGE_TYPE.RESULT_UPDATE_KV) {
640
+ if (_id == null) {
641
+ reject(new Error("ID is required for RESULT_UPDATE_KV."));
642
+ return;
643
+ }
644
+ const matchedIndex = computeMatchedItemIndex(
645
+ _id,
646
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
647
+ _uniqueKey
648
+ );
649
+ if (matchedIndex >= 0) {
650
+ updateObjectDeepValue(
651
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex],
652
+ _changeKey,
653
+ value
654
+ );
655
+ }
656
+ resolve(null);
657
+ } else if (method === enum_default.CHANGE_TYPE.RESULT_ITEM_MERGE) {
658
+ if (_id == null) {
659
+ reject(new Error("ID is required for RESULT_ITEM_MERGE."));
660
+ return;
661
+ }
662
+ const matchedIndex = computeMatchedItemIndex(
663
+ _id,
664
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
665
+ _uniqueKey
666
+ );
667
+ if (matchedIndex >= 0) {
668
+ const currentItem = newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex];
669
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex] = {
670
+ ...currentItem,
671
+ ...value
672
+ };
673
+ }
674
+ resolve(null);
675
+ } else if (method === enum_default.CHANGE_TYPE.RESET_FIELD) {
676
+ updateObjectDeepValue(
677
+ newFieldData,
678
+ _changeKey,
679
+ value
680
+ );
681
+ resolve(null);
682
+ } else {
683
+ let modifyValue = getObjectDeepValue(
684
+ newFieldData,
685
+ _changeKey
686
+ );
687
+ if (modifyValue == null) {
688
+ modifyValue = [];
689
+ }
690
+ const matchedIndex = _id != null ? computeMatchedItemIndex(
691
+ _id,
692
+ modifyValue,
693
+ _uniqueKey
694
+ ) : -1;
695
+ switch (method) {
696
+ case enum_default.CHANGE_TYPE.RESULT_ADD_AFTER:
697
+ if (isArray(modifyValue)) {
698
+ modifyValue = isArray(value) ? [...modifyValue, ...value] : [...modifyValue, value];
699
+ }
700
+ break;
701
+ case enum_default.CHANGE_TYPE.RESULT_ADD_BEFORE:
702
+ if (isArray(modifyValue)) {
703
+ modifyValue = isArray(value) ? [...value, ...modifyValue] : [value, ...modifyValue];
704
+ }
705
+ break;
706
+ case enum_default.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
707
+ if (isArray(modifyValue)) {
708
+ if (matchedIndex >= 0) {
709
+ modifyValue.splice(matchedIndex, 1);
710
+ } else if (isArray(_id)) {
711
+ const idSet = new Set(_id);
712
+ modifyValue = modifyValue.filter(
713
+ (item) => !idSet.has(
714
+ getObjectDeepValue(
715
+ item,
716
+ _uniqueKey
717
+ )
718
+ )
719
+ );
720
+ }
721
+ }
722
+ break;
723
+ case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
724
+ if (isArray(modifyValue) && matchedIndex >= 0) {
725
+ modifyValue.splice(matchedIndex, 0, value);
726
+ }
727
+ break;
728
+ case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
729
+ if (isArray(modifyValue) && matchedIndex >= 0) {
730
+ modifyValue.splice(matchedIndex + 1, 0, value);
731
+ }
732
+ break;
733
+ case enum_default.CHANGE_TYPE.RESULT_LIST_MERGE:
734
+ if (isArray(modifyValue)) {
735
+ combineArrayData(modifyValue, value, _uniqueKey);
736
+ }
737
+ break;
738
+ default:
739
+ resolve(null);
740
+ return;
741
+ }
742
+ newFieldData[_changeKey] = modifyValue;
743
+ resolve(null);
744
+ }
745
+ const afterLength = computeResultLength(
746
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY]
747
+ );
748
+ newFieldData.total = newFieldData.total + afterLength - beforeLength;
749
+ newFieldData.nothing = afterLength === 0;
750
+ setter({
751
+ key: fieldName,
752
+ type: enum_default.SETTER_TYPE.RESET,
753
+ value: newFieldData,
754
+ callback: () => {
755
+ resolve(null);
756
+ }
757
+ });
758
+ });
759
+ };
760
+
761
+ export { enum_default as ENUM, initData, initState, loadMore, updateState, utils_exports as utils };
762
+ //# sourceMappingURL=index.mjs.map
763
+ //# sourceMappingURL=index.mjs.map