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