@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/src/actions.ts DELETED
@@ -1,388 +0,0 @@
1
- import {
2
- generateDefaultField,
3
- generateFieldName,
4
- generateRequestParams,
5
- computeMatchedItemIndex,
6
- combineArrayData,
7
- updateObjectDeepValue,
8
- getObjectDeepValue,
9
- computeResultLength,
10
- searchValueByKey,
11
- isArray
12
- } from './utils'
13
- import { SET_DATA, SET_ERROR } from './setters'
14
- import ENUM from './enum'
15
- import type {
16
- objectKey,
17
- initStateType,
18
- initDataType,
19
- loadMoreType,
20
- updateStateType
21
- } from './types'
22
-
23
- export const initState = ({
24
- getter,
25
- setter,
26
- func,
27
- type,
28
- query,
29
- opts
30
- }: initStateType): Promise<null> => {
31
- return new Promise((resolve) => {
32
- const fieldName = generateFieldName({ func, type, query })
33
- const fieldData = getter(fieldName)
34
- if (fieldData) {
35
- resolve(null)
36
- return
37
- }
38
-
39
- setter({
40
- key: fieldName,
41
- type: ENUM.SETTER_TYPE.RESET,
42
- value: generateDefaultField(opts),
43
- callback: () => {
44
- resolve(null)
45
- }
46
- })
47
- })
48
- }
49
-
50
- export const initData = ({
51
- getter,
52
- setter,
53
- func,
54
- type,
55
- query,
56
- api,
57
- uniqueKey,
58
- callback
59
- }: initDataType): Promise<any> =>
60
- new Promise((resolve, reject) => {
61
- const fieldName = generateFieldName({ func, type, query })
62
- const fieldData = getter(fieldName)
63
- const doRefresh = !!query?.__refresh__
64
- const needReset = !!query?.__reload__
65
- // 如果 error 了,就不再请求
66
- if (fieldData && fieldData.error && !doRefresh) {
67
- return resolve(null)
68
- }
69
- // 正在请求中,return
70
- if (fieldData && fieldData.loading) {
71
- return resolve(null)
72
- }
73
- // 这个 field 已经请求过了
74
- const dontFetch = fieldData && fieldData.fetched && !doRefresh
75
- if (dontFetch) {
76
- return resolve(null)
77
- }
78
-
79
- const params = generateRequestParams({
80
- field: {
81
- ...fieldData,
82
- fetched: false
83
- },
84
- uniqueKey,
85
- query,
86
- type
87
- })
88
-
89
- const getData = () => {
90
- const loadData = () =>
91
- new Promise((res) => {
92
- const getDataFromAPI = () => {
93
- const funcCaller = typeof func === 'string' ? api[func] : func
94
-
95
- funcCaller(params)
96
- .then(res)
97
- .catch((error: Error) => {
98
- SET_ERROR({ setter, fieldName, error })
99
- reject(error)
100
- })
101
- }
102
-
103
- getDataFromAPI()
104
- })
105
-
106
- loadData().then((data) => {
107
- const setData = () => {
108
- SET_DATA({
109
- getter,
110
- setter,
111
- data,
112
- fieldName,
113
- type,
114
- page: params.page || 0,
115
- insertBefore: false
116
- }).then(() => {
117
- if (callback) {
118
- callback({
119
- params,
120
- data,
121
- refresh: doRefresh
122
- })
123
- }
124
- resolve(null)
125
- })
126
- }
127
-
128
- // 拿到数据后再重置 field
129
- if (needReset) {
130
- setter({
131
- key: fieldName,
132
- type: ENUM.SETTER_TYPE.RESET,
133
- value: generateDefaultField(),
134
- callback: setData
135
- })
136
- } else {
137
- setData()
138
- }
139
- })
140
- }
141
-
142
- // 需要预初始化 field
143
- if (!dontFetch && !needReset) {
144
- setter({
145
- key: fieldName,
146
- type: ENUM.SETTER_TYPE.RESET,
147
- value: {
148
- ...generateDefaultField(),
149
- loading: true,
150
- error: null
151
- },
152
- callback: getData
153
- })
154
- } else {
155
- getData()
156
- }
157
- })
158
-
159
- export const loadMore = ({
160
- getter,
161
- setter,
162
- query,
163
- type,
164
- func,
165
- api,
166
- uniqueKey,
167
- errorRetry,
168
- callback
169
- }: loadMoreType): Promise<any> =>
170
- new Promise((resolve, reject) => {
171
- const fieldName = generateFieldName({ func, type, query })
172
- const fieldData = getter(fieldName)
173
-
174
- if (!fieldData) {
175
- return resolve(null)
176
- }
177
-
178
- if (fieldData.loading) {
179
- return resolve(null)
180
- }
181
-
182
- if (fieldData.nothing) {
183
- return resolve(null)
184
- }
185
-
186
- if (fieldData.noMore && !errorRetry) {
187
- return resolve(null)
188
- }
189
-
190
- if (
191
- type === ENUM.FETCH_TYPE.PAGINATION &&
192
- query &&
193
- +query.page === fieldData.page
194
- ) {
195
- return resolve(null)
196
- }
197
-
198
- let loadingState
199
- if (type === ENUM.FETCH_TYPE.PAGINATION) {
200
- loadingState = {
201
- loading: true,
202
- error: null,
203
- [ENUM.FIELD_DATA.RESULT_KEY]: [],
204
- [ENUM.FIELD_DATA.EXTRA_KEY]: null
205
- }
206
- } else {
207
- loadingState = {
208
- loading: true,
209
- error: null
210
- }
211
- }
212
-
213
- const params = generateRequestParams({
214
- field: fieldData,
215
- uniqueKey,
216
- query,
217
- type
218
- })
219
-
220
- ;(params as any)[ENUM.FIELD_DATA.EXTRA_KEY] = (fieldData as any)[
221
- ENUM.FIELD_DATA.EXTRA_KEY
222
- ]
223
-
224
- const getData = () => {
225
- const funcCaller = typeof func === 'string' ? api[func] : func
226
-
227
- funcCaller(params)
228
- .then((data: any) => {
229
- SET_DATA({
230
- getter,
231
- setter,
232
- data,
233
- fieldName,
234
- type,
235
- page: params.page || 0,
236
- insertBefore: !!query?.is_up
237
- }).then(() => {
238
- if (callback) {
239
- callback({
240
- params,
241
- data,
242
- refresh: false
243
- })
244
- }
245
- resolve(null)
246
- })
247
- })
248
- .catch((error: Error) => {
249
- SET_ERROR({ setter, fieldName, error })
250
- reject(error)
251
- })
252
- }
253
-
254
- setter({
255
- key: fieldName,
256
- type: ENUM.SETTER_TYPE.MERGE,
257
- value: loadingState,
258
- callback: getData
259
- })
260
- })
261
-
262
- export const updateState = ({
263
- getter,
264
- setter,
265
- type,
266
- func,
267
- query,
268
- method,
269
- value,
270
- id,
271
- uniqueKey,
272
- changeKey
273
- }: updateStateType) => {
274
- return new Promise((resolve, reject) => {
275
- const fieldName = generateFieldName({ func, type, query })
276
- const fieldData = getter(fieldName)
277
- if (!fieldData) {
278
- reject()
279
- return
280
- }
281
-
282
- if (fieldData.page === -1) {
283
- resolve(null)
284
- return
285
- }
286
-
287
- const _id = id || ''
288
- const _uniqueKey = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME
289
- const _changeKey = changeKey || ENUM.FIELD_DATA.RESULT_KEY
290
- const beforeLength = computeResultLength(
291
- (fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY]
292
- )
293
-
294
- if (method === ENUM.CHANGE_TYPE.SEARCH_FIELD) {
295
- resolve(
296
- searchValueByKey(
297
- (fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY],
298
- _id as objectKey,
299
- _uniqueKey
300
- )
301
- )
302
- } else if (method === ENUM.CHANGE_TYPE.RESULT_UPDATE_KV) {
303
- // 修改 result 下的某个值的任意字段
304
- const matchedIndex = computeMatchedItemIndex(
305
- _id as objectKey,
306
- (fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY],
307
- _uniqueKey
308
- )
309
- updateObjectDeepValue(
310
- (fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex],
311
- _changeKey,
312
- value
313
- )
314
- } else if (method === ENUM.CHANGE_TYPE.RESULT_ITEM_MERGE) {
315
- // 修改 result 下的某个值的任意字段
316
- const matchedIndex = computeMatchedItemIndex(
317
- _id as objectKey,
318
- (fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY],
319
- _uniqueKey
320
- )
321
- ;(fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex] = {
322
- ...(fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY][matchedIndex],
323
- ...value
324
- }
325
- } else if (method === ENUM.CHANGE_TYPE.RESET_FIELD) {
326
- // 修改包括 field 下的任意字段
327
- updateObjectDeepValue(fieldData, _changeKey, value)
328
- } else {
329
- let modifyValue = getObjectDeepValue(fieldData, _changeKey)
330
- const matchedIndex = computeMatchedItemIndex(
331
- _id as objectKey,
332
- modifyValue,
333
- _uniqueKey
334
- )
335
-
336
- switch (method) {
337
- case ENUM.CHANGE_TYPE.RESULT_ADD_AFTER:
338
- isArray(value)
339
- ? (modifyValue = modifyValue.concat(value))
340
- : modifyValue.push(value)
341
- break
342
- case ENUM.CHANGE_TYPE.RESULT_ADD_BEFORE:
343
- isArray(value)
344
- ? (modifyValue = value.concat(modifyValue))
345
- : modifyValue.unshift(value)
346
- break
347
- case ENUM.CHANGE_TYPE.RESULT_REMOVE_BY_ID:
348
- if (matchedIndex >= 0) {
349
- modifyValue.splice(matchedIndex, 1)
350
- } else if (isArray(_id)) {
351
- modifyValue = modifyValue.filter(
352
- (_: any) => (_id as objectKey[]).indexOf(_[_uniqueKey]) === -1
353
- )
354
- }
355
- break
356
- case ENUM.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:
357
- if (matchedIndex >= 0) {
358
- modifyValue.splice(matchedIndex, 0, value)
359
- }
360
- break
361
- case ENUM.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:
362
- if (matchedIndex >= 0) {
363
- modifyValue.splice(matchedIndex + 1, 0, value)
364
- }
365
- break
366
- case ENUM.CHANGE_TYPE.RESULT_LIST_MERGE:
367
- combineArrayData(modifyValue, value, _uniqueKey)
368
- break
369
- }
370
- ;(fieldData as any)[_changeKey] = modifyValue
371
- }
372
-
373
- const afterLength = computeResultLength(
374
- (fieldData as any)[ENUM.FIELD_DATA.RESULT_KEY]
375
- )
376
- fieldData.total = fieldData.total + afterLength - beforeLength
377
- fieldData.nothing = afterLength === 0
378
-
379
- setter({
380
- key: fieldName,
381
- type: ENUM.SETTER_TYPE.MERGE,
382
- value: fieldData,
383
- callback: () => {
384
- resolve(null)
385
- }
386
- })
387
- })
388
- }
package/src/enum.ts DELETED
@@ -1,33 +0,0 @@
1
- const FETCH_TYPE_ARRAY = ['jump', 'sinceId', 'page', 'seenIds', 'auto']
2
-
3
- export default {
4
- SETTER_TYPE: {
5
- RESET: 0,
6
- MERGE: 1
7
- },
8
- FETCH_TYPE_ARRAY,
9
- FETCH_TYPE: {
10
- PAGINATION: FETCH_TYPE_ARRAY[0],
11
- SINCE_FIRST_OR_END_ID: FETCH_TYPE_ARRAY[1],
12
- SCROLL_LOAD_MORE: FETCH_TYPE_ARRAY[2],
13
- HAS_LOADED_IDS: FETCH_TYPE_ARRAY[3],
14
- AUTO: FETCH_TYPE_ARRAY[4]
15
- },
16
- CHANGE_TYPE: {
17
- SEARCH_FIELD: 'search',
18
- RESET_FIELD: 'reset',
19
- RESULT_UPDATE_KV: 'update',
20
- RESULT_ADD_AFTER: 'push',
21
- RESULT_ADD_BEFORE: 'unshift',
22
- RESULT_REMOVE_BY_ID: 'delete',
23
- RESULT_INSERT_TO_BEFORE: 'insert-before',
24
- RESULT_INSERT_TO_AFTER: 'insert-after',
25
- RESULT_LIST_MERGE: 'patch',
26
- RESULT_ITEM_MERGE: 'merge'
27
- },
28
- FIELD_DATA: {
29
- RESULT_KEY: 'result',
30
- EXTRA_KEY: 'extra'
31
- },
32
- DEFAULT_UNIQUE_KEY_NAME: 'id'
33
- }
package/src/setters.ts DELETED
@@ -1,91 +0,0 @@
1
- import {
2
- computeResultLength,
3
- setReactivityField,
4
- isObjectResult
5
- } from './utils'
6
- import ENUM from './enum'
7
- import type { setDataType, setErrorType } from './types'
8
-
9
- export const SET_DATA = ({
10
- getter,
11
- setter,
12
- data,
13
- fieldName,
14
- type,
15
- page,
16
- insertBefore
17
- }: setDataType): Promise<any> => {
18
- return new Promise((resolve, reject) => {
19
- const fieldData = getter(fieldName)
20
- if (!fieldData) {
21
- reject()
22
- return
23
- }
24
-
25
- let result
26
- let extra
27
-
28
- if (isObjectResult(data)) {
29
- result = data
30
- fieldData.nothing = false
31
- fieldData.fetched = true
32
- fieldData.noMore = true
33
- fieldData.page = -1
34
- } else {
35
- result = data.result
36
- extra = data.extra
37
- const isEmpty = computeResultLength(result) === 0
38
- fieldData.nothing = fieldData.fetched ? false : isEmpty
39
- fieldData.fetched = true
40
- fieldData.total = data.total || 0
41
- if (type === ENUM.FETCH_TYPE.PAGINATION) {
42
- fieldData.noMore = false
43
- fieldData.page = +page
44
- } else {
45
- fieldData.noMore =
46
- typeof data.no_more === 'undefined'
47
- ? isEmpty
48
- : data.no_more || isEmpty
49
- fieldData.page = fieldData.page + 1
50
- }
51
- }
52
-
53
- fieldData.loading = false
54
- setReactivityField(
55
- fieldData,
56
- // @ts-ignore
57
- ENUM.FIELD_DATA.RESULT_KEY,
58
- result,
59
- type,
60
- insertBefore
61
- )
62
- extra &&
63
- setReactivityField(
64
- fieldData,
65
- // @ts-ignore
66
- ENUM.FIELD_DATA.EXTRA_KEY,
67
- extra,
68
- type,
69
- insertBefore
70
- )
71
- setter({
72
- key: fieldName,
73
- type: ENUM.SETTER_TYPE.RESET,
74
- value: fieldData,
75
- callback: () => {
76
- resolve(null)
77
- }
78
- })
79
- })
80
- }
81
-
82
- export const SET_ERROR = ({ setter, fieldName, error }: setErrorType): void => {
83
- setter({
84
- key: fieldName,
85
- type: ENUM.SETTER_TYPE.MERGE,
86
- value: {
87
- error,
88
- loading: false
89
- }
90
- })
91
- }
package/src/types.ts DELETED
@@ -1,122 +0,0 @@
1
- export type objectKey = string | number
2
-
3
- export type keyMap = Record<objectKey, any>
4
-
5
- export type morphArray = any[]
6
-
7
- export type fieldResult = morphArray | keyMap
8
-
9
- export type defaultField = {
10
- result: fieldResult
11
- noMore: boolean
12
- nothing: boolean
13
- loading: boolean
14
- error: null | Error
15
- extra: null | any
16
- fetched: boolean
17
- page: number
18
- total: number
19
- }
20
-
21
- export type fetchTypes = 'jump' | 'sinceId' | 'page' | 'seenIds' | 'auto'
22
-
23
- export type fieldKeys =
24
- | 'result'
25
- | 'noMore'
26
- | 'nothing'
27
- | 'loading'
28
- | 'error'
29
- | 'extra'
30
- | 'page'
31
- | 'total'
32
-
33
- export type generateFieldProps = {
34
- func: string | (() => {})
35
- type: fetchTypes
36
- query?: keyMap
37
- }
38
-
39
- export type generateParamsType = {
40
- field: defaultField
41
- uniqueKey: string
42
- query?: keyMap
43
- type: fetchTypes
44
- }
45
-
46
- export type generateParamsResp = {
47
- seen_ids?: string
48
- since_id?: objectKey
49
- is_up?: 0 | 1
50
- page?: number
51
- }
52
-
53
- export type setterFuncParams = {
54
- key: string
55
- type: number
56
- value: any
57
- callback?: (obj?: keyMap) => void
58
- }
59
-
60
- export type initStateType = {
61
- getter: (str: string) => defaultField
62
- setter: (obj: setterFuncParams) => void
63
- func: string | (() => {})
64
- type: fetchTypes
65
- query?: keyMap
66
- opts?: keyMap
67
- }
68
-
69
- export type initDataType = {
70
- getter: (str: string) => defaultField
71
- setter: (obj: setterFuncParams) => void
72
- func: string | (() => {})
73
- type: fetchTypes
74
- query?: keyMap
75
- api: keyMap
76
- cacheTimeout: number
77
- uniqueKey: string
78
- callback: (obj?: keyMap) => void
79
- }
80
-
81
- export type loadMoreType = {
82
- getter: (str: string) => defaultField
83
- setter: (obj: setterFuncParams) => void
84
- func: string | (() => {})
85
- type: fetchTypes
86
- query?: keyMap
87
- api: keyMap
88
- cacheTimeout: number
89
- uniqueKey: string
90
- errorRetry: boolean
91
- callback: (obj?: keyMap) => void
92
- }
93
-
94
- export type updateStateType = {
95
- getter: (str: string) => defaultField
96
- setter: (obj: setterFuncParams) => void
97
- func: string | (() => {})
98
- type: fetchTypes
99
- query?: keyMap
100
- method: string
101
- value: any
102
- id: string | number | objectKey[]
103
- changeKey: string
104
- cacheTimeout: number
105
- uniqueKey: string
106
- }
107
-
108
- export type setDataType = {
109
- getter: (str: string) => defaultField
110
- setter: (obj: setterFuncParams) => void
111
- data: any
112
- fieldName: string
113
- type: fetchTypes
114
- page: number
115
- insertBefore: boolean
116
- }
117
-
118
- export type setErrorType = {
119
- setter: (obj: setterFuncParams) => void
120
- fieldName: string
121
- error: null | Error
122
- }