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