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