@flowlist/js-core 2.3.1 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -102
- package/dist/index.d.mts +263 -0
- package/dist/index.d.ts +263 -4
- package/dist/index.global.js +775 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +770 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +763 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +33 -30
- package/dist/js-core.es.js +0 -582
- package/dist/js-core.umd.js +0 -1
- package/src/actions.ts +0 -388
- package/src/enum.ts +0 -33
- package/src/setters.ts +0 -91
- package/src/types.ts +0 -122
- package/src/utils.ts +0 -325
package/dist/js-core.es.js
DELETED
|
@@ -1,582 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
const FETCH_TYPE_ARRAY = ["jump", "sinceId", "page", "seenIds", "auto"];
|
|
21
|
-
var ENUM = {
|
|
22
|
-
SETTER_TYPE: {
|
|
23
|
-
RESET: 0,
|
|
24
|
-
MERGE: 1
|
|
25
|
-
},
|
|
26
|
-
FETCH_TYPE_ARRAY,
|
|
27
|
-
FETCH_TYPE: {
|
|
28
|
-
PAGINATION: FETCH_TYPE_ARRAY[0],
|
|
29
|
-
SINCE_FIRST_OR_END_ID: FETCH_TYPE_ARRAY[1],
|
|
30
|
-
SCROLL_LOAD_MORE: FETCH_TYPE_ARRAY[2],
|
|
31
|
-
HAS_LOADED_IDS: FETCH_TYPE_ARRAY[3],
|
|
32
|
-
AUTO: FETCH_TYPE_ARRAY[4]
|
|
33
|
-
},
|
|
34
|
-
CHANGE_TYPE: {
|
|
35
|
-
SEARCH_FIELD: "search",
|
|
36
|
-
RESET_FIELD: "reset",
|
|
37
|
-
RESULT_UPDATE_KV: "update",
|
|
38
|
-
RESULT_ADD_AFTER: "push",
|
|
39
|
-
RESULT_ADD_BEFORE: "unshift",
|
|
40
|
-
RESULT_REMOVE_BY_ID: "delete",
|
|
41
|
-
RESULT_INSERT_TO_BEFORE: "insert-before",
|
|
42
|
-
RESULT_INSERT_TO_AFTER: "insert-after",
|
|
43
|
-
RESULT_LIST_MERGE: "patch",
|
|
44
|
-
RESULT_ITEM_MERGE: "merge"
|
|
45
|
-
},
|
|
46
|
-
FIELD_DATA: {
|
|
47
|
-
RESULT_KEY: "result",
|
|
48
|
-
EXTRA_KEY: "extra"
|
|
49
|
-
},
|
|
50
|
-
DEFAULT_UNIQUE_KEY_NAME: "id"
|
|
51
|
-
};
|
|
52
|
-
const isObjectResult = (data) => {
|
|
53
|
-
return data.result === void 0;
|
|
54
|
-
};
|
|
55
|
-
const generateDefaultField = (opts = {}) => __spreadValues(__spreadValues({}, {
|
|
56
|
-
result: [],
|
|
57
|
-
noMore: false,
|
|
58
|
-
nothing: false,
|
|
59
|
-
loading: false,
|
|
60
|
-
error: null,
|
|
61
|
-
extra: null,
|
|
62
|
-
fetched: false,
|
|
63
|
-
page: 0,
|
|
64
|
-
total: 0
|
|
65
|
-
}), opts);
|
|
66
|
-
const generateFieldName = ({
|
|
67
|
-
func,
|
|
68
|
-
type,
|
|
69
|
-
query = {}
|
|
70
|
-
}) => {
|
|
71
|
-
func = typeof func === "string" ? func : `api-${Math.random().toString(36).substring(2)}`;
|
|
72
|
-
type = type || "auto";
|
|
73
|
-
let result = `${func}-${type}`;
|
|
74
|
-
Object.keys(query).filter((_) => !~["undefined", "object", "function"].indexOf(typeof query[_]) && !~[
|
|
75
|
-
"page",
|
|
76
|
-
"is_up",
|
|
77
|
-
"since_id",
|
|
78
|
-
"seen_ids",
|
|
79
|
-
"__refresh__",
|
|
80
|
-
"__reload__"
|
|
81
|
-
].indexOf(_)).sort().forEach((key) => {
|
|
82
|
-
result += `-${key}-${query[key]}`;
|
|
83
|
-
});
|
|
84
|
-
return result;
|
|
85
|
-
};
|
|
86
|
-
const getObjectDeepValue = (field, keys) => {
|
|
87
|
-
if (!keys) {
|
|
88
|
-
return field || "";
|
|
89
|
-
}
|
|
90
|
-
let result = field || "";
|
|
91
|
-
const keysArr = isArray(keys) ? keys : keys.split(".");
|
|
92
|
-
keysArr.forEach((key) => {
|
|
93
|
-
result = result[key];
|
|
94
|
-
});
|
|
95
|
-
return result || "";
|
|
96
|
-
};
|
|
97
|
-
const updateObjectDeepValue = (field, changeKey, value) => {
|
|
98
|
-
if (/\./.test(changeKey)) {
|
|
99
|
-
const keys = changeKey.split(".");
|
|
100
|
-
const prefix = keys.pop();
|
|
101
|
-
let result = field;
|
|
102
|
-
keys.forEach((key) => {
|
|
103
|
-
result = result[key];
|
|
104
|
-
});
|
|
105
|
-
result[prefix] = value;
|
|
106
|
-
} else {
|
|
107
|
-
field[changeKey] = value;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
const searchValueByKey = (result, id, key) => {
|
|
111
|
-
if (isArray(result)) {
|
|
112
|
-
const index = computeMatchedItemIndex(id, result, key);
|
|
113
|
-
if (index < 0) {
|
|
114
|
-
return void 0;
|
|
115
|
-
}
|
|
116
|
-
return result[index];
|
|
117
|
-
}
|
|
118
|
-
return result[id];
|
|
119
|
-
};
|
|
120
|
-
const computeMatchedItemIndex = (itemId, fieldArr, changingKey) => {
|
|
121
|
-
let s = -1;
|
|
122
|
-
for (let i = 0; i < fieldArr.length; i++) {
|
|
123
|
-
if (getObjectDeepValue(fieldArr[i], changingKey.toString()).toString() === (itemId || "").toString()) {
|
|
124
|
-
s = i;
|
|
125
|
-
break;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return s;
|
|
129
|
-
};
|
|
130
|
-
const combineArrayData = (fieldArray, value, changingKey) => {
|
|
131
|
-
if (isArray(value)) {
|
|
132
|
-
value.forEach((col) => {
|
|
133
|
-
const stringifyId = getObjectDeepValue(col, changingKey).toString();
|
|
134
|
-
fieldArray.forEach((item, index) => {
|
|
135
|
-
if (getObjectDeepValue(item, changingKey).toString() === stringifyId) {
|
|
136
|
-
fieldArray[index] = __spreadValues(__spreadValues({}, item), col);
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
} else {
|
|
141
|
-
Object.keys(value).forEach((uniqueId) => {
|
|
142
|
-
const stringifyId = (uniqueId || "").toString();
|
|
143
|
-
fieldArray.forEach((item, index) => {
|
|
144
|
-
if (getObjectDeepValue(item, changingKey).toString() === stringifyId) {
|
|
145
|
-
fieldArray[index] = __spreadValues(__spreadValues({}, item), value[uniqueId]);
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
const isArray = (data) => Object.prototype.toString.call(data) === "[object Array]";
|
|
152
|
-
const setReactivityField = (field, key, value, type, insertBefore) => {
|
|
153
|
-
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
154
|
-
field[key] = value;
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (isArray(value)) {
|
|
158
|
-
field[key] = insertBefore ? value.concat(field[key] || []) : (field[key] || []).concat(value);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
if (key !== ENUM.FIELD_DATA.RESULT_KEY) {
|
|
162
|
-
field[key] = value;
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
if (isArray(field[key])) {
|
|
166
|
-
field[key] = {};
|
|
167
|
-
}
|
|
168
|
-
Object.keys(value).forEach((subKey) => {
|
|
169
|
-
field[key][subKey] = field[key][subKey] ? insertBefore ? value[subKey].concat(field[key][subKey]) : field[key][subKey].concat(value[subKey]) : value[subKey];
|
|
170
|
-
});
|
|
171
|
-
};
|
|
172
|
-
const computeResultLength = (data) => {
|
|
173
|
-
let result = 0;
|
|
174
|
-
if (isArray(data)) {
|
|
175
|
-
result = data.length;
|
|
176
|
-
} else {
|
|
177
|
-
Object.keys(data).forEach((key) => {
|
|
178
|
-
result += data[key].length;
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
return result;
|
|
182
|
-
};
|
|
183
|
-
const generateRequestParams = ({
|
|
184
|
-
field,
|
|
185
|
-
uniqueKey,
|
|
186
|
-
query = {},
|
|
187
|
-
type
|
|
188
|
-
}) => {
|
|
189
|
-
const result = {};
|
|
190
|
-
if (field.fetched) {
|
|
191
|
-
const changing = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME;
|
|
192
|
-
if (type === ENUM.FETCH_TYPE.AUTO) {
|
|
193
|
-
result.seen_ids = field.result.map((_) => getObjectDeepValue(_, changing)).join(",");
|
|
194
|
-
result.since_id = getObjectDeepValue(field.result[query.is_up ? 0 : field.result.length - 1], changing);
|
|
195
|
-
result.is_up = query.is_up ? 1 : 0;
|
|
196
|
-
result.page = query.page || field.page + 1;
|
|
197
|
-
} else if (type === ENUM.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
198
|
-
result.seen_ids = field.result.map((_) => getObjectDeepValue(_, changing)).join(",");
|
|
199
|
-
} else if (type === ENUM.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
200
|
-
result.since_id = getObjectDeepValue(field.result[query.is_up ? 0 : field.result.length - 1], changing);
|
|
201
|
-
result.is_up = query.is_up ? 1 : 0;
|
|
202
|
-
} else if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
203
|
-
result.page = query.page;
|
|
204
|
-
} else if (type === ENUM.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
205
|
-
result.page = field.page + 1;
|
|
206
|
-
}
|
|
207
|
-
} else {
|
|
208
|
-
if (type === ENUM.FETCH_TYPE.AUTO) {
|
|
209
|
-
result.seen_ids = "";
|
|
210
|
-
result.since_id = query.sinceId || (query.is_up ? 999999999 : 0);
|
|
211
|
-
result.is_up = query.is_up ? 1 : 0;
|
|
212
|
-
result.page = query.page || field.page || 1;
|
|
213
|
-
} else if (type === ENUM.FETCH_TYPE.HAS_LOADED_IDS) {
|
|
214
|
-
result.seen_ids = "";
|
|
215
|
-
} else if (type === ENUM.FETCH_TYPE.SINCE_FIRST_OR_END_ID) {
|
|
216
|
-
result.since_id = query.sinceId || (query.is_up ? 999999999 : 0);
|
|
217
|
-
result.is_up = query.is_up ? 1 : 0;
|
|
218
|
-
} else if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
219
|
-
result.page = query.page || field.page;
|
|
220
|
-
} else if (type === ENUM.FETCH_TYPE.SCROLL_LOAD_MORE) {
|
|
221
|
-
result.page = 1;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
return __spreadValues(__spreadValues({}, query), result);
|
|
225
|
-
};
|
|
226
|
-
var utils = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
227
|
-
__proto__: null,
|
|
228
|
-
isObjectResult,
|
|
229
|
-
generateDefaultField,
|
|
230
|
-
generateFieldName,
|
|
231
|
-
getObjectDeepValue,
|
|
232
|
-
updateObjectDeepValue,
|
|
233
|
-
searchValueByKey,
|
|
234
|
-
computeMatchedItemIndex,
|
|
235
|
-
combineArrayData,
|
|
236
|
-
isArray,
|
|
237
|
-
setReactivityField,
|
|
238
|
-
computeResultLength,
|
|
239
|
-
generateRequestParams
|
|
240
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
241
|
-
const SET_DATA = ({
|
|
242
|
-
getter,
|
|
243
|
-
setter,
|
|
244
|
-
data,
|
|
245
|
-
fieldName,
|
|
246
|
-
type,
|
|
247
|
-
page,
|
|
248
|
-
insertBefore
|
|
249
|
-
}) => {
|
|
250
|
-
return new Promise((resolve, reject) => {
|
|
251
|
-
const fieldData = getter(fieldName);
|
|
252
|
-
if (!fieldData) {
|
|
253
|
-
reject();
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
let result;
|
|
257
|
-
let extra;
|
|
258
|
-
if (isObjectResult(data)) {
|
|
259
|
-
result = data;
|
|
260
|
-
fieldData.nothing = false;
|
|
261
|
-
fieldData.fetched = true;
|
|
262
|
-
fieldData.noMore = true;
|
|
263
|
-
fieldData.page = -1;
|
|
264
|
-
} else {
|
|
265
|
-
result = data.result;
|
|
266
|
-
extra = data.extra;
|
|
267
|
-
const isEmpty = computeResultLength(result) === 0;
|
|
268
|
-
fieldData.nothing = fieldData.fetched ? false : isEmpty;
|
|
269
|
-
fieldData.fetched = true;
|
|
270
|
-
fieldData.total = data.total || 0;
|
|
271
|
-
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
272
|
-
fieldData.noMore = false;
|
|
273
|
-
fieldData.page = +page;
|
|
274
|
-
} else {
|
|
275
|
-
fieldData.noMore = typeof data.no_more === "undefined" ? isEmpty : data.no_more || isEmpty;
|
|
276
|
-
fieldData.page = fieldData.page + 1;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
fieldData.loading = false;
|
|
280
|
-
setReactivityField(fieldData, ENUM.FIELD_DATA.RESULT_KEY, result, type, insertBefore);
|
|
281
|
-
extra && setReactivityField(fieldData, ENUM.FIELD_DATA.EXTRA_KEY, extra, type, insertBefore);
|
|
282
|
-
setter({
|
|
283
|
-
key: fieldName,
|
|
284
|
-
type: ENUM.SETTER_TYPE.RESET,
|
|
285
|
-
value: fieldData,
|
|
286
|
-
callback: () => {
|
|
287
|
-
resolve(null);
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
};
|
|
292
|
-
const SET_ERROR = ({ setter, fieldName, error }) => {
|
|
293
|
-
setter({
|
|
294
|
-
key: fieldName,
|
|
295
|
-
type: ENUM.SETTER_TYPE.MERGE,
|
|
296
|
-
value: {
|
|
297
|
-
error,
|
|
298
|
-
loading: false
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
};
|
|
302
|
-
const initState = ({
|
|
303
|
-
getter,
|
|
304
|
-
setter,
|
|
305
|
-
func,
|
|
306
|
-
type,
|
|
307
|
-
query,
|
|
308
|
-
opts
|
|
309
|
-
}) => {
|
|
310
|
-
return new Promise((resolve) => {
|
|
311
|
-
const fieldName = generateFieldName({ func, type, query });
|
|
312
|
-
const fieldData = getter(fieldName);
|
|
313
|
-
if (fieldData) {
|
|
314
|
-
resolve(null);
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
setter({
|
|
318
|
-
key: fieldName,
|
|
319
|
-
type: ENUM.SETTER_TYPE.RESET,
|
|
320
|
-
value: generateDefaultField(opts),
|
|
321
|
-
callback: () => {
|
|
322
|
-
resolve(null);
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
};
|
|
327
|
-
const initData = ({
|
|
328
|
-
getter,
|
|
329
|
-
setter,
|
|
330
|
-
func,
|
|
331
|
-
type,
|
|
332
|
-
query,
|
|
333
|
-
api,
|
|
334
|
-
uniqueKey,
|
|
335
|
-
callback
|
|
336
|
-
}) => new Promise((resolve, reject) => {
|
|
337
|
-
const fieldName = generateFieldName({ func, type, query });
|
|
338
|
-
const fieldData = getter(fieldName);
|
|
339
|
-
const doRefresh = !!(query == null ? void 0 : query.__refresh__);
|
|
340
|
-
const needReset = !!(query == null ? void 0 : query.__reload__);
|
|
341
|
-
if (fieldData && fieldData.error && !doRefresh) {
|
|
342
|
-
return resolve(null);
|
|
343
|
-
}
|
|
344
|
-
if (fieldData && fieldData.loading) {
|
|
345
|
-
return resolve(null);
|
|
346
|
-
}
|
|
347
|
-
const dontFetch = fieldData && fieldData.fetched && !doRefresh;
|
|
348
|
-
if (dontFetch) {
|
|
349
|
-
return resolve(null);
|
|
350
|
-
}
|
|
351
|
-
const params = generateRequestParams({
|
|
352
|
-
field: __spreadProps(__spreadValues({}, fieldData), {
|
|
353
|
-
fetched: false
|
|
354
|
-
}),
|
|
355
|
-
uniqueKey,
|
|
356
|
-
query,
|
|
357
|
-
type
|
|
358
|
-
});
|
|
359
|
-
const getData = () => {
|
|
360
|
-
const loadData = () => new Promise((res) => {
|
|
361
|
-
const getDataFromAPI = () => {
|
|
362
|
-
const funcCaller = typeof func === "string" ? api[func] : func;
|
|
363
|
-
funcCaller(params).then(res).catch((error) => {
|
|
364
|
-
SET_ERROR({ setter, fieldName, error });
|
|
365
|
-
reject(error);
|
|
366
|
-
});
|
|
367
|
-
};
|
|
368
|
-
getDataFromAPI();
|
|
369
|
-
});
|
|
370
|
-
loadData().then((data) => {
|
|
371
|
-
const setData = () => {
|
|
372
|
-
SET_DATA({
|
|
373
|
-
getter,
|
|
374
|
-
setter,
|
|
375
|
-
data,
|
|
376
|
-
fieldName,
|
|
377
|
-
type,
|
|
378
|
-
page: params.page || 0,
|
|
379
|
-
insertBefore: false
|
|
380
|
-
}).then(() => {
|
|
381
|
-
if (callback) {
|
|
382
|
-
callback({
|
|
383
|
-
params,
|
|
384
|
-
data,
|
|
385
|
-
refresh: doRefresh
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
resolve(null);
|
|
389
|
-
});
|
|
390
|
-
};
|
|
391
|
-
if (needReset) {
|
|
392
|
-
setter({
|
|
393
|
-
key: fieldName,
|
|
394
|
-
type: ENUM.SETTER_TYPE.RESET,
|
|
395
|
-
value: generateDefaultField(),
|
|
396
|
-
callback: setData
|
|
397
|
-
});
|
|
398
|
-
} else {
|
|
399
|
-
setData();
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
};
|
|
403
|
-
if (!dontFetch && !needReset) {
|
|
404
|
-
setter({
|
|
405
|
-
key: fieldName,
|
|
406
|
-
type: ENUM.SETTER_TYPE.RESET,
|
|
407
|
-
value: __spreadProps(__spreadValues({}, generateDefaultField()), {
|
|
408
|
-
loading: true,
|
|
409
|
-
error: null
|
|
410
|
-
}),
|
|
411
|
-
callback: getData
|
|
412
|
-
});
|
|
413
|
-
} else {
|
|
414
|
-
getData();
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
const loadMore = ({
|
|
418
|
-
getter,
|
|
419
|
-
setter,
|
|
420
|
-
query,
|
|
421
|
-
type,
|
|
422
|
-
func,
|
|
423
|
-
api,
|
|
424
|
-
uniqueKey,
|
|
425
|
-
errorRetry,
|
|
426
|
-
callback
|
|
427
|
-
}) => new Promise((resolve, reject) => {
|
|
428
|
-
const fieldName = generateFieldName({ func, type, query });
|
|
429
|
-
const fieldData = getter(fieldName);
|
|
430
|
-
if (!fieldData) {
|
|
431
|
-
return resolve(null);
|
|
432
|
-
}
|
|
433
|
-
if (fieldData.loading) {
|
|
434
|
-
return resolve(null);
|
|
435
|
-
}
|
|
436
|
-
if (fieldData.nothing) {
|
|
437
|
-
return resolve(null);
|
|
438
|
-
}
|
|
439
|
-
if (fieldData.noMore && !errorRetry) {
|
|
440
|
-
return resolve(null);
|
|
441
|
-
}
|
|
442
|
-
if (type === ENUM.FETCH_TYPE.PAGINATION && query && +query.page === fieldData.page) {
|
|
443
|
-
return resolve(null);
|
|
444
|
-
}
|
|
445
|
-
let loadingState;
|
|
446
|
-
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
447
|
-
loadingState = {
|
|
448
|
-
loading: true,
|
|
449
|
-
error: null,
|
|
450
|
-
[ENUM.FIELD_DATA.RESULT_KEY]: [],
|
|
451
|
-
[ENUM.FIELD_DATA.EXTRA_KEY]: null
|
|
452
|
-
};
|
|
453
|
-
} else {
|
|
454
|
-
loadingState = {
|
|
455
|
-
loading: true,
|
|
456
|
-
error: null
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
const params = generateRequestParams({
|
|
460
|
-
field: fieldData,
|
|
461
|
-
uniqueKey,
|
|
462
|
-
query,
|
|
463
|
-
type
|
|
464
|
-
});
|
|
465
|
-
params[ENUM.FIELD_DATA.EXTRA_KEY] = fieldData[ENUM.FIELD_DATA.EXTRA_KEY];
|
|
466
|
-
const getData = () => {
|
|
467
|
-
const funcCaller = typeof func === "string" ? api[func] : func;
|
|
468
|
-
funcCaller(params).then((data) => {
|
|
469
|
-
SET_DATA({
|
|
470
|
-
getter,
|
|
471
|
-
setter,
|
|
472
|
-
data,
|
|
473
|
-
fieldName,
|
|
474
|
-
type,
|
|
475
|
-
page: params.page || 0,
|
|
476
|
-
insertBefore: !!(query == null ? void 0 : query.is_up)
|
|
477
|
-
}).then(() => {
|
|
478
|
-
if (callback) {
|
|
479
|
-
callback({
|
|
480
|
-
params,
|
|
481
|
-
data,
|
|
482
|
-
refresh: false
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
resolve(null);
|
|
486
|
-
});
|
|
487
|
-
}).catch((error) => {
|
|
488
|
-
SET_ERROR({ setter, fieldName, error });
|
|
489
|
-
reject(error);
|
|
490
|
-
});
|
|
491
|
-
};
|
|
492
|
-
setter({
|
|
493
|
-
key: fieldName,
|
|
494
|
-
type: ENUM.SETTER_TYPE.MERGE,
|
|
495
|
-
value: loadingState,
|
|
496
|
-
callback: getData
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
const updateState = ({
|
|
500
|
-
getter,
|
|
501
|
-
setter,
|
|
502
|
-
type,
|
|
503
|
-
func,
|
|
504
|
-
query,
|
|
505
|
-
method,
|
|
506
|
-
value,
|
|
507
|
-
id,
|
|
508
|
-
uniqueKey,
|
|
509
|
-
changeKey
|
|
510
|
-
}) => {
|
|
511
|
-
return new Promise((resolve, reject) => {
|
|
512
|
-
const fieldName = generateFieldName({ func, type, query });
|
|
513
|
-
const fieldData = getter(fieldName);
|
|
514
|
-
if (!fieldData) {
|
|
515
|
-
reject();
|
|
516
|
-
return;
|
|
517
|
-
}
|
|
518
|
-
if (fieldData.page === -1) {
|
|
519
|
-
resolve(null);
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
const _id = id || "";
|
|
523
|
-
const _uniqueKey = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME;
|
|
524
|
-
const _changeKey = changeKey || ENUM.FIELD_DATA.RESULT_KEY;
|
|
525
|
-
const beforeLength = computeResultLength(fieldData[ENUM.FIELD_DATA.RESULT_KEY]);
|
|
526
|
-
if (method === ENUM.CHANGE_TYPE.SEARCH_FIELD) {
|
|
527
|
-
resolve(searchValueByKey(fieldData[ENUM.FIELD_DATA.RESULT_KEY], _id, _uniqueKey));
|
|
528
|
-
} else if (method === ENUM.CHANGE_TYPE.RESULT_UPDATE_KV) {
|
|
529
|
-
const matchedIndex = computeMatchedItemIndex(_id, fieldData[ENUM.FIELD_DATA.RESULT_KEY], _uniqueKey);
|
|
530
|
-
updateObjectDeepValue(fieldData[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex], _changeKey, value);
|
|
531
|
-
} else if (method === ENUM.CHANGE_TYPE.RESULT_ITEM_MERGE) {
|
|
532
|
-
const matchedIndex = computeMatchedItemIndex(_id, fieldData[ENUM.FIELD_DATA.RESULT_KEY], _uniqueKey);
|
|
533
|
-
fieldData[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex] = __spreadValues(__spreadValues({}, fieldData[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex]), value);
|
|
534
|
-
} else if (method === ENUM.CHANGE_TYPE.RESET_FIELD) {
|
|
535
|
-
updateObjectDeepValue(fieldData, _changeKey, value);
|
|
536
|
-
} else {
|
|
537
|
-
let modifyValue = getObjectDeepValue(fieldData, _changeKey);
|
|
538
|
-
const matchedIndex = computeMatchedItemIndex(_id, modifyValue, _uniqueKey);
|
|
539
|
-
switch (method) {
|
|
540
|
-
case ENUM.CHANGE_TYPE.RESULT_ADD_AFTER:
|
|
541
|
-
isArray(value) ? modifyValue = modifyValue.concat(value) : modifyValue.push(value);
|
|
542
|
-
break;
|
|
543
|
-
case ENUM.CHANGE_TYPE.RESULT_ADD_BEFORE:
|
|
544
|
-
isArray(value) ? modifyValue = value.concat(modifyValue) : modifyValue.unshift(value);
|
|
545
|
-
break;
|
|
546
|
-
case ENUM.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
|
|
547
|
-
if (matchedIndex >= 0) {
|
|
548
|
-
modifyValue.splice(matchedIndex, 1);
|
|
549
|
-
} else if (isArray(_id)) {
|
|
550
|
-
modifyValue = modifyValue.filter((_) => _id.indexOf(_[_uniqueKey]) === -1);
|
|
551
|
-
}
|
|
552
|
-
break;
|
|
553
|
-
case ENUM.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
|
|
554
|
-
if (matchedIndex >= 0) {
|
|
555
|
-
modifyValue.splice(matchedIndex, 0, value);
|
|
556
|
-
}
|
|
557
|
-
break;
|
|
558
|
-
case ENUM.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
|
|
559
|
-
if (matchedIndex >= 0) {
|
|
560
|
-
modifyValue.splice(matchedIndex + 1, 0, value);
|
|
561
|
-
}
|
|
562
|
-
break;
|
|
563
|
-
case ENUM.CHANGE_TYPE.RESULT_LIST_MERGE:
|
|
564
|
-
combineArrayData(modifyValue, value, _uniqueKey);
|
|
565
|
-
break;
|
|
566
|
-
}
|
|
567
|
-
fieldData[_changeKey] = modifyValue;
|
|
568
|
-
}
|
|
569
|
-
const afterLength = computeResultLength(fieldData[ENUM.FIELD_DATA.RESULT_KEY]);
|
|
570
|
-
fieldData.total = fieldData.total + afterLength - beforeLength;
|
|
571
|
-
fieldData.nothing = afterLength === 0;
|
|
572
|
-
setter({
|
|
573
|
-
key: fieldName,
|
|
574
|
-
type: ENUM.SETTER_TYPE.MERGE,
|
|
575
|
-
value: fieldData,
|
|
576
|
-
callback: () => {
|
|
577
|
-
resolve(null);
|
|
578
|
-
}
|
|
579
|
-
});
|
|
580
|
-
});
|
|
581
|
-
};
|
|
582
|
-
export { ENUM, initData, initState, loadMore, updateState, utils };
|
package/dist/js-core.umd.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var J=Object.defineProperty,W=Object.defineProperties;var Z=Object.getOwnPropertyDescriptors;var k=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,q=Object.prototype.propertyIsEnumerable;var B=(r,c,n)=>c in r?J(r,c,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[c]=n,I=(r,c)=>{for(var n in c||(c={}))v.call(c,n)&&B(r,n,c[n]);if(k)for(var n of k(c))q.call(c,n)&&B(r,n,c[n]);return r},b=(r,c)=>W(r,Z(c));(function(r,c){typeof exports=="object"&&typeof module!="undefined"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(r=typeof globalThis!="undefined"?globalThis:r||self,c(r.JsCore={}))})(this,function(r){"use strict";const c=["jump","sinceId","page","seenIds","auto"];var n={SETTER_TYPE:{RESET:0,MERGE:1},FETCH_TYPE_ARRAY:c,FETCH_TYPE:{PAGINATION:c[0],SINCE_FIRST_OR_END_ID:c[1],SCROLL_LOAD_MORE:c[2],HAS_LOADED_IDS:c[3],AUTO:c[4]},CHANGE_TYPE:{SEARCH_FIELD:"search",RESET_FIELD:"reset",RESULT_UPDATE_KV:"update",RESULT_ADD_AFTER:"push",RESULT_ADD_BEFORE:"unshift",RESULT_REMOVE_BY_ID:"delete",RESULT_INSERT_TO_BEFORE:"insert-before",RESULT_INSERT_TO_AFTER:"insert-after",RESULT_LIST_MERGE:"patch",RESULT_ITEM_MERGE:"merge"},FIELD_DATA:{RESULT_KEY:"result",EXTRA_KEY:"extra"},DEFAULT_UNIQUE_KEY_NAME:"id"};const G=t=>t.result===void 0,N=(t={})=>I({result:[],noMore:!1,nothing:!1,loading:!1,error:null,extra:null,fetched:!1,page:0,total:0},t),h=({func:t,type:_,query:e={}})=>{t=typeof t=="string"?t:`api-${Math.random().toString(36).substring(2)}`,_=_||"auto";let s=`${t}-${_}`;return Object.keys(e).filter(E=>!~["undefined","object","function"].indexOf(typeof e[E])&&!~["page","is_up","since_id","seen_ids","__refresh__","__reload__"].indexOf(E)).sort().forEach(E=>{s+=`-${E}-${e[E]}`}),s},u=(t,_)=>{if(!_)return t||"";let e=t||"";return(L(_)?_:_.split(".")).forEach(E=>{e=e[E]}),e||""},m=(t,_,e)=>{if(/\./.test(_)){const s=_.split("."),E=s.pop();let i=t;s.forEach(a=>{i=i[a]}),i[E]=e}else t[_]=e},j=(t,_,e)=>{if(L(t)){const s=O(_,t,e);return s<0?void 0:t[s]}return t[_]},O=(t,_,e)=>{let s=-1;for(let E=0;E<_.length;E++)if(u(_[E],e.toString()).toString()===(t||"").toString()){s=E;break}return s},K=(t,_,e)=>{L(_)?_.forEach(s=>{const E=u(s,e).toString();t.forEach((i,a)=>{u(i,e).toString()===E&&(t[a]=I(I({},i),s))})}):Object.keys(_).forEach(s=>{const E=(s||"").toString();t.forEach((i,a)=>{u(i,e).toString()===E&&(t[a]=I(I({},i),_[s]))})})},L=t=>Object.prototype.toString.call(t)==="[object Array]",M=(t,_,e,s,E)=>{if(s===n.FETCH_TYPE.PAGINATION){t[_]=e;return}if(L(e)){t[_]=E?e.concat(t[_]||[]):(t[_]||[]).concat(e);return}if(_!==n.FIELD_DATA.RESULT_KEY){t[_]=e;return}L(t[_])&&(t[_]={}),Object.keys(e).forEach(i=>{t[_][i]=t[_][i]?E?e[i].concat(t[_][i]):t[_][i].concat(e[i]):e[i]})},U=t=>{let _=0;return L(t)?_=t.length:Object.keys(t).forEach(e=>{_+=t[e].length}),_},H=({field:t,uniqueKey:_,query:e={},type:s})=>{const E={};if(t.fetched){const i=_||n.DEFAULT_UNIQUE_KEY_NAME;s===n.FETCH_TYPE.AUTO?(E.seen_ids=t.result.map(a=>u(a,i)).join(","),E.since_id=u(t.result[e.is_up?0:t.result.length-1],i),E.is_up=e.is_up?1:0,E.page=e.page||t.page+1):s===n.FETCH_TYPE.HAS_LOADED_IDS?E.seen_ids=t.result.map(a=>u(a,i)).join(","):s===n.FETCH_TYPE.SINCE_FIRST_OR_END_ID?(E.since_id=u(t.result[e.is_up?0:t.result.length-1],i),E.is_up=e.is_up?1:0):s===n.FETCH_TYPE.PAGINATION?E.page=e.page:s===n.FETCH_TYPE.SCROLL_LOAD_MORE&&(E.page=t.page+1)}else s===n.FETCH_TYPE.AUTO?(E.seen_ids="",E.since_id=e.sinceId||(e.is_up?999999999:0),E.is_up=e.is_up?1:0,E.page=e.page||t.page||1):s===n.FETCH_TYPE.HAS_LOADED_IDS?E.seen_ids="":s===n.FETCH_TYPE.SINCE_FIRST_OR_END_ID?(E.since_id=e.sinceId||(e.is_up?999999999:0),E.is_up=e.is_up?1:0):s===n.FETCH_TYPE.PAGINATION?E.page=e.page||t.page:s===n.FETCH_TYPE.SCROLL_LOAD_MORE&&(E.page=1);return I(I({},e),E)};var w=Object.freeze(Object.defineProperty({__proto__:null,isObjectResult:G,generateDefaultField:N,generateFieldName:h,getObjectDeepValue:u,updateObjectDeepValue:m,searchValueByKey:j,computeMatchedItemIndex:O,combineArrayData:K,isArray:L,setReactivityField:M,computeResultLength:U,generateRequestParams:H},Symbol.toStringTag,{value:"Module"}));const x=({getter:t,setter:_,data:e,fieldName:s,type:E,page:i,insertBefore:a})=>new Promise((p,S)=>{const T=t(s);if(!T){S();return}let R,f;if(G(e))R=e,T.nothing=!1,T.fetched=!0,T.noMore=!0,T.page=-1;else{R=e.result,f=e.extra;const A=U(R)===0;T.nothing=T.fetched?!1:A,T.fetched=!0,T.total=e.total||0,E===n.FETCH_TYPE.PAGINATION?(T.noMore=!1,T.page=+i):(T.noMore=typeof e.no_more=="undefined"?A:e.no_more||A,T.page=T.page+1)}T.loading=!1,M(T,n.FIELD_DATA.RESULT_KEY,R,E,a),f&&M(T,n.FIELD_DATA.EXTRA_KEY,f,E,a),_({key:s,type:n.SETTER_TYPE.RESET,value:T,callback:()=>{p(null)}})}),V=({setter:t,fieldName:_,error:e})=>{t({key:_,type:n.SETTER_TYPE.MERGE,value:{error:e,loading:!1}})},X=({getter:t,setter:_,func:e,type:s,query:E,opts:i})=>new Promise(a=>{const p=h({func:e,type:s,query:E});if(t(p)){a(null);return}_({key:p,type:n.SETTER_TYPE.RESET,value:N(i),callback:()=>{a(null)}})}),$=({getter:t,setter:_,func:e,type:s,query:E,api:i,uniqueKey:a,callback:p})=>new Promise((S,T)=>{const R=h({func:e,type:s,query:E}),f=t(R),A=!!(E!=null&&E.__refresh__),o=!!(E!=null&&E.__reload__);if(f&&f.error&&!A||f&&f.loading)return S(null);const D=f&&f.fetched&&!A;if(D)return S(null);const g=H({field:b(I({},f),{fetched:!1}),uniqueKey:a,query:E,type:s}),F=()=>{(()=>new Promise(Y=>{(()=>{(typeof e=="string"?i[e]:e)(g).then(Y).catch(C=>{V({setter:_,fieldName:R,error:C}),T(C)})})()}))().then(Y=>{const l=()=>{x({getter:t,setter:_,data:Y,fieldName:R,type:s,page:g.page||0,insertBefore:!1}).then(()=>{p&&p({params:g,data:Y,refresh:A}),S(null)})};o?_({key:R,type:n.SETTER_TYPE.RESET,value:N(),callback:l}):l()})};!D&&!o?_({key:R,type:n.SETTER_TYPE.RESET,value:b(I({},N()),{loading:!0,error:null}),callback:F}):F()}),Q=({getter:t,setter:_,query:e,type:s,func:E,api:i,uniqueKey:a,errorRetry:p,callback:S})=>new Promise((T,R)=>{const f=h({func:E,type:s,query:e}),A=t(f);if(!A||A.loading||A.nothing||A.noMore&&!p||s===n.FETCH_TYPE.PAGINATION&&e&&+e.page===A.page)return T(null);let o;s===n.FETCH_TYPE.PAGINATION?o={loading:!0,error:null,[n.FIELD_DATA.RESULT_KEY]:[],[n.FIELD_DATA.EXTRA_KEY]:null}:o={loading:!0,error:null};const D=H({field:A,uniqueKey:a,query:e,type:s});D[n.FIELD_DATA.EXTRA_KEY]=A[n.FIELD_DATA.EXTRA_KEY];const g=()=>{(typeof E=="string"?i[E]:E)(D).then(d=>{x({getter:t,setter:_,data:d,fieldName:f,type:s,page:D.page||0,insertBefore:!!(e!=null&&e.is_up)}).then(()=>{S&&S({params:D,data:d,refresh:!1}),T(null)})}).catch(d=>{V({setter:_,fieldName:f,error:d}),R(d)})};_({key:f,type:n.SETTER_TYPE.MERGE,value:o,callback:g})}),z=({getter:t,setter:_,type:e,func:s,query:E,method:i,value:a,id:p,uniqueKey:S,changeKey:T})=>new Promise((R,f)=>{const A=h({func:s,type:e,query:E}),o=t(A);if(!o){f();return}if(o.page===-1){R(null);return}const D=p||"",g=S||n.DEFAULT_UNIQUE_KEY_NAME,F=T||n.FIELD_DATA.RESULT_KEY,d=U(o[n.FIELD_DATA.RESULT_KEY]);if(i===n.CHANGE_TYPE.SEARCH_FIELD)R(j(o[n.FIELD_DATA.RESULT_KEY],D,g));else if(i===n.CHANGE_TYPE.RESULT_UPDATE_KV){const l=O(D,o[n.FIELD_DATA.RESULT_KEY],g);m(o[n.FIELD_DATA.RESULT_KEY][l],F,a)}else if(i===n.CHANGE_TYPE.RESULT_ITEM_MERGE){const l=O(D,o[n.FIELD_DATA.RESULT_KEY],g);o[n.FIELD_DATA.RESULT_KEY][l]=I(I({},o[n.FIELD_DATA.RESULT_KEY][l]),a)}else if(i===n.CHANGE_TYPE.RESET_FIELD)m(o,F,a);else{let l=u(o,F);const P=O(D,l,g);switch(i){case n.CHANGE_TYPE.RESULT_ADD_AFTER:L(a)?l=l.concat(a):l.push(a);break;case n.CHANGE_TYPE.RESULT_ADD_BEFORE:L(a)?l=a.concat(l):l.unshift(a);break;case n.CHANGE_TYPE.RESULT_REMOVE_BY_ID:P>=0?l.splice(P,1):L(D)&&(l=l.filter(C=>D.indexOf(C[g])===-1));break;case n.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:P>=0&&l.splice(P,0,a);break;case n.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:P>=0&&l.splice(P+1,0,a);break;case n.CHANGE_TYPE.RESULT_LIST_MERGE:K(l,a,g);break}o[F]=l}const Y=U(o[n.FIELD_DATA.RESULT_KEY]);o.total=o.total+Y-d,o.nothing=Y===0,_({key:A,type:n.SETTER_TYPE.MERGE,value:o,callback:()=>{R(null)}})});r.ENUM=n,r.initData=$,r.initState=X,r.loadMore=Q,r.updateState=z,r.utils=w,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|