@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.
package/dist/index.js ADDED
@@ -0,0 +1,766 @@
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[func] : func;
449
+ funcCaller(params).then(res).catch((error) => {
450
+ SET_ERROR({ setter, fieldName, error });
451
+ rej(error);
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[func] : func;
564
+ funcCaller(params).then((data) => {
565
+ SET_DATA({
566
+ getter,
567
+ setter,
568
+ data,
569
+ fieldName,
570
+ type,
571
+ page: params.page || 0,
572
+ insertBefore: !!query?.is_up
573
+ }).then(() => {
574
+ if (callback) {
575
+ callback({
576
+ params,
577
+ data,
578
+ refresh: false
579
+ });
580
+ }
581
+ resolve();
582
+ });
583
+ }).catch((error) => {
584
+ SET_ERROR({ setter, fieldName, error });
585
+ reject(error);
586
+ });
587
+ };
588
+ setter({
589
+ key: fieldName,
590
+ type: enum_default.SETTER_TYPE.MERGE,
591
+ value: loadingState,
592
+ callback: getData
593
+ });
594
+ });
595
+ var updateState = ({
596
+ getter,
597
+ setter,
598
+ func,
599
+ // string only
600
+ type,
601
+ query,
602
+ method,
603
+ value,
604
+ id,
605
+ uniqueKey,
606
+ changeKey
607
+ }) => {
608
+ return new Promise((resolve, reject) => {
609
+ const fieldName = generateFieldName({ func, type, query });
610
+ const fieldData = getter(fieldName);
611
+ if (!fieldData) {
612
+ reject(new Error(`Field ${fieldName} not found.`));
613
+ return;
614
+ }
615
+ if (fieldData.page === -1) {
616
+ resolve(null);
617
+ return;
618
+ }
619
+ const _id = id;
620
+ const _uniqueKey = uniqueKey || enum_default.DEFAULT_UNIQUE_KEY_NAME;
621
+ const _changeKey = changeKey || enum_default.FIELD_DATA.RESULT_KEY;
622
+ const beforeLength = computeResultLength(
623
+ fieldData[enum_default.FIELD_DATA.RESULT_KEY]
624
+ );
625
+ const newFieldData = { ...fieldData };
626
+ if (method === enum_default.CHANGE_TYPE.SEARCH_FIELD) {
627
+ if (_id == null) {
628
+ reject(new Error("ID is required for SEARCH_FIELD."));
629
+ return;
630
+ }
631
+ const result = searchValueByKey(
632
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
633
+ _id,
634
+ _uniqueKey
635
+ );
636
+ resolve(result);
637
+ } else if (method === enum_default.CHANGE_TYPE.RESULT_UPDATE_KV) {
638
+ if (_id == null) {
639
+ reject(new Error("ID is required for RESULT_UPDATE_KV."));
640
+ return;
641
+ }
642
+ const matchedIndex = computeMatchedItemIndex(
643
+ _id,
644
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
645
+ _uniqueKey
646
+ );
647
+ if (matchedIndex >= 0) {
648
+ updateObjectDeepValue(
649
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex],
650
+ _changeKey,
651
+ value
652
+ );
653
+ }
654
+ resolve(null);
655
+ } else if (method === enum_default.CHANGE_TYPE.RESULT_ITEM_MERGE) {
656
+ if (_id == null) {
657
+ reject(new Error("ID is required for RESULT_ITEM_MERGE."));
658
+ return;
659
+ }
660
+ const matchedIndex = computeMatchedItemIndex(
661
+ _id,
662
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY],
663
+ _uniqueKey
664
+ );
665
+ if (matchedIndex >= 0) {
666
+ const currentItem = newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex];
667
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY][matchedIndex] = {
668
+ ...currentItem,
669
+ ...value
670
+ };
671
+ }
672
+ resolve(null);
673
+ } else if (method === enum_default.CHANGE_TYPE.RESET_FIELD) {
674
+ updateObjectDeepValue(
675
+ newFieldData,
676
+ _changeKey,
677
+ value
678
+ );
679
+ resolve(null);
680
+ } else {
681
+ let modifyValue = getObjectDeepValue(
682
+ newFieldData,
683
+ _changeKey
684
+ );
685
+ if (modifyValue == null) {
686
+ modifyValue = [];
687
+ }
688
+ const matchedIndex = _id != null ? computeMatchedItemIndex(
689
+ _id,
690
+ modifyValue,
691
+ _uniqueKey
692
+ ) : -1;
693
+ switch (method) {
694
+ case enum_default.CHANGE_TYPE.RESULT_ADD_AFTER:
695
+ if (isArray(modifyValue)) {
696
+ modifyValue = isArray(value) ? [...modifyValue, ...value] : [...modifyValue, value];
697
+ }
698
+ break;
699
+ case enum_default.CHANGE_TYPE.RESULT_ADD_BEFORE:
700
+ if (isArray(modifyValue)) {
701
+ modifyValue = isArray(value) ? [...value, ...modifyValue] : [value, ...modifyValue];
702
+ }
703
+ break;
704
+ case enum_default.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
705
+ if (isArray(modifyValue)) {
706
+ if (matchedIndex >= 0) {
707
+ modifyValue.splice(matchedIndex, 1);
708
+ } else if (isArray(_id)) {
709
+ const idSet = new Set(_id);
710
+ modifyValue = modifyValue.filter(
711
+ (item) => !idSet.has(
712
+ getObjectDeepValue(
713
+ item,
714
+ _uniqueKey
715
+ )
716
+ )
717
+ );
718
+ }
719
+ }
720
+ break;
721
+ case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
722
+ if (isArray(modifyValue) && matchedIndex >= 0) {
723
+ modifyValue.splice(matchedIndex, 0, value);
724
+ }
725
+ break;
726
+ case enum_default.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
727
+ if (isArray(modifyValue) && matchedIndex >= 0) {
728
+ modifyValue.splice(matchedIndex + 1, 0, value);
729
+ }
730
+ break;
731
+ case enum_default.CHANGE_TYPE.RESULT_LIST_MERGE:
732
+ if (isArray(modifyValue)) {
733
+ combineArrayData(modifyValue, value, _uniqueKey);
734
+ }
735
+ break;
736
+ default:
737
+ resolve(null);
738
+ return;
739
+ }
740
+ newFieldData[_changeKey] = modifyValue;
741
+ resolve(null);
742
+ }
743
+ const afterLength = computeResultLength(
744
+ newFieldData[enum_default.FIELD_DATA.RESULT_KEY]
745
+ );
746
+ newFieldData.total = newFieldData.total + afterLength - beforeLength;
747
+ newFieldData.nothing = afterLength === 0;
748
+ setter({
749
+ key: fieldName,
750
+ type: enum_default.SETTER_TYPE.RESET,
751
+ value: newFieldData,
752
+ callback: () => {
753
+ resolve(null);
754
+ }
755
+ });
756
+ });
757
+ };
758
+
759
+ exports.ENUM = enum_default;
760
+ exports.initData = initData;
761
+ exports.initState = initState;
762
+ exports.loadMore = loadMore;
763
+ exports.updateState = updateState;
764
+ exports.utils = utils_exports;
765
+ //# sourceMappingURL=index.js.map
766
+ //# sourceMappingURL=index.js.map