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