@flowlist/js-core 1.7.6 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +4 -1
- package/dist/js-core.es.js +592 -1
- package/dist/js-core.umd.js +1 -1
- package/package.json +17 -16
- package/src/actions.ts +31 -51
- package/src/setters.ts +24 -38
- package/src/types.ts +2 -22
- package/src/utils.ts +10 -5
- package/dist/src/actions.d.ts +0 -5
- package/dist/src/enum.d.ts +0 -32
- package/dist/src/index.d.ts +0 -4
- package/dist/src/setters.d.ts +0 -3
- package/dist/src/types.d.ts +0 -117
- package/dist/src/utils.d.ts +0 -58
- package/dist/tests/unit/api.d.ts +0 -68
- package/dist/tests/unit/env.d.ts +0 -20
- package/dist/tests/unit/item-factory.d.ts +0 -23
- package/src/index.ts +0 -4
package/dist/js-core.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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 w=(l,c,n)=>c in l?J(l,c,{enumerable:!0,configurable:!0,writable:!0,value:n}):l[c]=n,u=(l,c)=>{for(var n in c||(c={}))v.call(c,n)&&w(l,n,c[n]);if(k)for(var n of k(c))q.call(c,n)&&w(l,n,c[n]);return l},G=(l,c)=>W(l,Z(c));(function(l,c){typeof exports=="object"&&typeof module!="undefined"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(l=typeof globalThis!="undefined"?globalThis:l||self,c(l.JsCore={}))})(this,function(l){"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 j=t=>t.result===void 0,U=(t={})=>u({result:[],noMore:!1,nothing:!1,loading:!0,error:null,extra:null,fetched:!1,page:0,total:0},t),O=({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},L=(t,_)=>{if(!_)return t||"";let e=t||"";return(F(_)?_:_.split(".")).forEach(E=>{e=e[E]}),e||""},M=(t,_,e)=>{if(/\./.test(_)){const s=_.split("."),E=s.pop();let i=t;s.forEach(o=>{i=i[o]}),i[E]=e}else t[_]=e},K=(t,_,e)=>{if(F(t)){const s=N(_,t,e);return s<0?void 0:t[s]}return t[_]},N=(t,_,e)=>{let s=-1;for(let E=0;E<_.length;E++)if(L(_[E],e.toString()).toString()===(t||"").toString()){s=E;break}return s},x=(t,_,e)=>{F(_)?_.forEach(s=>{const E=L(s,e).toString();t.forEach((i,o)=>{L(i,e).toString()===E&&(t[o]=u(u({},i),s))})}):Object.keys(_).forEach(s=>{const E=(s||"").toString();t.forEach((i,o)=>{L(i,e).toString()===E&&(t[o]=u(u({},i),_[s]))})})},F=t=>Object.prototype.toString.call(t)==="[object Array]",H=(t,_,e,s,E)=>{if(s===n.FETCH_TYPE.PAGINATION){t[_]=e;return}if(F(e)){t[_]=E?e.concat(t[_]||[]):(t[_]||[]).concat(e);return}if(_!==n.FIELD_DATA.RESULT_KEY){t[_]=e;return}F(t[_])&&(t[_]={}),Object.keys(e).forEach(i=>{t[_][i]=t[_][i]?E?e[i].concat(t[_][i]):t[_][i].concat(e[i]):e[i]})},C=t=>{let _=0;return F(t)?_=t.length:Object.keys(t).forEach(e=>{_+=t[e].length}),_},b=({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(o=>L(o,i)).join(","),E.since_id=L(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(o=>L(o,i)).join(","):s===n.FETCH_TYPE.SINCE_FIRST_OR_END_ID?(E.since_id=L(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 u(u({},e),E)};var B=Object.freeze(Object.defineProperty({__proto__:null,isObjectResult:j,generateDefaultField:U,generateFieldName:O,getObjectDeepValue:L,updateObjectDeepValue:M,searchValueByKey:K,computeMatchedItemIndex:N,combineArrayData:x,isArray:F,setReactivityField:H,computeResultLength:C,generateRequestParams:b},Symbol.toStringTag,{value:"Module"}));const V=({getter:t,setter:_,data:e,fieldName:s,type:E,page:i,insertBefore:o})=>new Promise((I,S)=>{const a=t(s);if(!a){S();return}let A,f;if(j(e))A=e,a.nothing=!1,a.fetched=!0,a.noMore=!0,a.page=-1;else{A=e.result,f=e.extra;const R=C(A)===0;a.nothing=a.fetched?!1:R,a.fetched=!0,a.total=e.total||0,E===n.FETCH_TYPE.PAGINATION?(a.noMore=!1,a.page=+i):(a.noMore=e.no_more||R,a.page=a.page+1)}a.loading=!1,H(a,n.FIELD_DATA.RESULT_KEY,A,E,o),f&&H(a,n.FIELD_DATA.EXTRA_KEY,f,E,o),_({key:s,type:n.SETTER_TYPE.RESET,value:a,callback:()=>{I(null)}})}),m=({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(o=>{const I=O({func:e,type:s,query:E});if(t(I)){o(null);return}_({key:I,type:n.SETTER_TYPE.RESET,value:U(i),callback:()=>{o(null)}})}),$=({getter:t,setter:_,func:e,type:s,query:E,api:i,uniqueKey:o,callback:I})=>new Promise((S,a)=>{const A=O({func:e,type:s,query:E}),f=t(A),R=!!(E!=null&&E.__refresh__),T=!!(E!=null&&E.__reload__);if(f&&f.error&&!R||f&&f.loading&&f.fetched&&!R&&!T)return S(null);const D=f&&f.fetched&&!R;if(D)return S(null);const g=b({field:G(u({},f),{fetched:!1}),uniqueKey:o,query:E,type:s}),d=()=>{(()=>new Promise(h=>{(()=>{const Y=typeof e=="string"?i[e]:e;if(!Y){const P=new Error("func is not a function");m({setter:_,fieldName:A,error:P}),a(P)}Y(g).then(h).catch(P=>{m({setter:_,fieldName:A,error:P}),a(P)})})()}))().then(h=>{const r=()=>{V({getter:t,setter:_,data:h,fieldName:A,type:s,page:g.page||0,insertBefore:!1}).then(()=>{I&&I({params:g,data:h,refresh:R}),S(null)})};T?_({key:A,type:n.SETTER_TYPE.RESET,value:U(),callback:r}):r()})};!D&&!T?_({key:A,type:n.SETTER_TYPE.RESET,value:G(u({},U()),{loading:!0,error:null}),callback:d}):d()}),Q=({getter:t,setter:_,query:e,type:s,func:E,api:i,uniqueKey:o,errorRetry:I,callback:S})=>new Promise((a,A)=>{const f=O({func:E,type:s,query:e}),R=t(f);if(!R||R.loading||R.nothing||R.noMore&&!I||s===n.FETCH_TYPE.PAGINATION&&e&&+e.page===R.page)return a(null);let T;s===n.FETCH_TYPE.PAGINATION?T={loading:!0,error:null,[n.FIELD_DATA.RESULT_KEY]:[],[n.FIELD_DATA.EXTRA_KEY]:null}:T={loading:!0,error:null};const D=b({field:R,uniqueKey:o,query:e,type:s});D[n.FIELD_DATA.EXTRA_KEY]=R[n.FIELD_DATA.EXTRA_KEY];const g=()=>{const d=typeof E=="string"?i[E]:E;if(!d){const p=new Error("func is not a function");m({setter:_,fieldName:f,error:p}),A(p)}d(D).then(p=>{V({getter:t,setter:_,data:p,fieldName:f,type:s,page:D.page||0,insertBefore:!!(e!=null&&e.is_up)}).then(()=>{S&&S({params:D,data:p,refresh:!1}),a(null)})}).catch(p=>{m({setter:_,fieldName:f,error:p}),A(p)})};_({key:f,type:n.SETTER_TYPE.MERGE,value:T,callback:g})}),z=({getter:t,setter:_,type:e,func:s,query:E,method:i,value:o,id:I,uniqueKey:S,changeKey:a})=>new Promise((A,f)=>{const R=O({func:s,type:e,query:E}),T=t(R);if(!T){f();return}if(T.page===-1){A(null);return}const D=I||"",g=S||n.DEFAULT_UNIQUE_KEY_NAME,d=a||n.FIELD_DATA.RESULT_KEY,p=C(T[n.FIELD_DATA.RESULT_KEY]);if(i===n.CHANGE_TYPE.SEARCH_FIELD)A(K(T[n.FIELD_DATA.RESULT_KEY],D,g));else if(i===n.CHANGE_TYPE.RESULT_UPDATE_KV){const r=N(D,T[n.FIELD_DATA.RESULT_KEY],g);M(T[n.FIELD_DATA.RESULT_KEY][r],d,o)}else if(i===n.CHANGE_TYPE.RESULT_ITEM_MERGE){const r=N(D,T[n.FIELD_DATA.RESULT_KEY],g);T[n.FIELD_DATA.RESULT_KEY][r]=u(u({},T[n.FIELD_DATA.RESULT_KEY][r]),o)}else if(i===n.CHANGE_TYPE.RESET_FIELD)M(T,d,o);else{let r=L(T,d);const Y=N(D,r,g);switch(i){case n.CHANGE_TYPE.RESULT_ADD_AFTER:F(o)?r=r.concat(o):r.push(o);break;case n.CHANGE_TYPE.RESULT_ADD_BEFORE:F(o)?r=o.concat(r):r.unshift(o);break;case n.CHANGE_TYPE.RESULT_REMOVE_BY_ID:Y>=0?r.splice(Y,1):F(D)&&(r=r.filter(P=>D.indexOf(P[g])===-1));break;case n.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:Y>=0&&r.splice(Y,0,o);break;case n.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:Y>=0&&r.splice(Y+1,0,o);break;case n.CHANGE_TYPE.RESULT_LIST_MERGE:x(r,o,g);break}T[d]=r}const h=C(T[n.FIELD_DATA.RESULT_KEY]);T.total=T.total+h-p,T.nothing=h===0,_({key:R,type:n.SETTER_TYPE.MERGE,value:T,callback:()=>{A(null)}})});l.ENUM=n,l.initData=$,l.initState=X,l.loadMore=Q,l.updateState=z,l.utils=B,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flowlist/js-core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"main": "dist/js-core.umd.js",
|
|
5
5
|
"module": "dist/js-core.es.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -31,25 +31,26 @@
|
|
|
31
31
|
"lint": "eslint --ext js,jsx,ts,tsx,vue . --fix"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@typescript-eslint/eslint-plugin": "^4.18.0",
|
|
35
|
-
"@typescript-eslint/parser": "^4.18.0",
|
|
36
|
-
"@vue/eslint-config-prettier": "^6.0.0",
|
|
37
|
-
"@vue/eslint-config-typescript": "^7.0.0",
|
|
38
|
-
"eslint": "^6.7.2",
|
|
39
|
-
"eslint-plugin-prettier": "^3.3.1",
|
|
40
|
-
"eslint-plugin-vue": "^7.0.0",
|
|
41
34
|
"@babel/preset-env": "^7.14.7",
|
|
42
35
|
"@babel/preset-typescript": "^7.14.5",
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
36
|
+
"@faker-js/faker": "^6.1.2",
|
|
37
|
+
"@types/jest": "^27.4.1",
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^5.19.0",
|
|
39
|
+
"@typescript-eslint/parser": "^5.19.0",
|
|
40
|
+
"@vue/cli-plugin-unit-jest": "~5.0.4",
|
|
41
|
+
"@vue/cli-service": "~5.0.4",
|
|
42
|
+
"@vue/eslint-config-prettier": "^7.0.0",
|
|
43
|
+
"@vue/eslint-config-typescript": "^10.0.0",
|
|
44
|
+
"@vue/test-utils": "^2.0.0-rc.19",
|
|
45
|
+
"eslint": "^8.13.0",
|
|
46
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
47
|
+
"eslint-plugin-vue": "^8.6.0",
|
|
48
48
|
"jest-localstorage-mock": "^2.4.8",
|
|
49
|
-
"ts-babel": "^6.1.7",
|
|
50
49
|
"prettier": "^2.3.2",
|
|
51
|
-
"
|
|
50
|
+
"ts-babel": "^6.1.7",
|
|
51
|
+
"ts-jest": "^27.1.4",
|
|
52
|
+
"typescript": "^4.6.3",
|
|
52
53
|
"vite": "^2.3.8",
|
|
53
|
-
"vite-plugin-dts": "^0.5
|
|
54
|
+
"vite-plugin-dts": "^1.0.5"
|
|
54
55
|
}
|
|
55
56
|
}
|
package/src/actions.ts
CHANGED
|
@@ -25,8 +25,8 @@ export const initState = ({
|
|
|
25
25
|
setter,
|
|
26
26
|
func,
|
|
27
27
|
type,
|
|
28
|
-
query
|
|
29
|
-
opts
|
|
28
|
+
query,
|
|
29
|
+
opts
|
|
30
30
|
}: initStateType): Promise<null> => {
|
|
31
31
|
return new Promise((resolve) => {
|
|
32
32
|
const fieldName = generateFieldName({ func, type, query })
|
|
@@ -50,26 +50,24 @@ export const initState = ({
|
|
|
50
50
|
export const initData = ({
|
|
51
51
|
getter,
|
|
52
52
|
setter,
|
|
53
|
-
cache,
|
|
54
53
|
func,
|
|
55
54
|
type,
|
|
56
|
-
query
|
|
55
|
+
query,
|
|
57
56
|
api,
|
|
58
|
-
cacheTimeout,
|
|
59
57
|
uniqueKey,
|
|
60
58
|
callback
|
|
61
59
|
}: initDataType): Promise<any> =>
|
|
62
60
|
new Promise((resolve, reject) => {
|
|
63
61
|
const fieldName = generateFieldName({ func, type, query })
|
|
64
62
|
const fieldData = getter(fieldName)
|
|
65
|
-
const doRefresh = !!query
|
|
66
|
-
const needReset = !!query
|
|
63
|
+
const doRefresh = !!query?.__refresh__
|
|
64
|
+
const needReset = !!query?.__reload__
|
|
67
65
|
// 如果 error 了,就不再请求
|
|
68
66
|
if (fieldData && fieldData.error && !doRefresh) {
|
|
69
67
|
return resolve(null)
|
|
70
68
|
}
|
|
71
69
|
// 正在请求中,return
|
|
72
|
-
if (fieldData && fieldData.loading) {
|
|
70
|
+
if (fieldData && fieldData.loading && fieldData.fetched && !doRefresh && !needReset) {
|
|
73
71
|
return resolve(null)
|
|
74
72
|
}
|
|
75
73
|
// 这个 field 已经请求过了
|
|
@@ -87,14 +85,17 @@ export const initData = ({
|
|
|
87
85
|
query,
|
|
88
86
|
type
|
|
89
87
|
})
|
|
90
|
-
let fromLocal = false
|
|
91
88
|
|
|
92
89
|
const getData = () => {
|
|
93
90
|
const loadData = () =>
|
|
94
91
|
new Promise((res) => {
|
|
95
92
|
const getDataFromAPI = () => {
|
|
96
93
|
const funcCaller = typeof func === 'string' ? api[func] : func
|
|
97
|
-
|
|
94
|
+
if (!funcCaller) {
|
|
95
|
+
const error = new Error('func is not a function')
|
|
96
|
+
SET_ERROR({ setter, fieldName, error })
|
|
97
|
+
reject(error)
|
|
98
|
+
}
|
|
98
99
|
funcCaller(params)
|
|
99
100
|
.then(res)
|
|
100
101
|
.catch((error: Error) => {
|
|
@@ -103,19 +104,7 @@ export const initData = ({
|
|
|
103
104
|
})
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
|
|
107
|
-
cache
|
|
108
|
-
.get({ key: fieldName })
|
|
109
|
-
.then((data) => {
|
|
110
|
-
fromLocal = true
|
|
111
|
-
res(data)
|
|
112
|
-
})
|
|
113
|
-
.catch(() => {
|
|
114
|
-
getDataFromAPI()
|
|
115
|
-
})
|
|
116
|
-
} else {
|
|
117
|
-
getDataFromAPI()
|
|
118
|
-
}
|
|
107
|
+
getDataFromAPI()
|
|
119
108
|
})
|
|
120
109
|
|
|
121
110
|
loadData().then((data) => {
|
|
@@ -123,12 +112,9 @@ export const initData = ({
|
|
|
123
112
|
SET_DATA({
|
|
124
113
|
getter,
|
|
125
114
|
setter,
|
|
126
|
-
cache,
|
|
127
115
|
data,
|
|
128
116
|
fieldName,
|
|
129
117
|
type,
|
|
130
|
-
fromLocal,
|
|
131
|
-
cacheTimeout,
|
|
132
118
|
page: params.page || 0,
|
|
133
119
|
insertBefore: false
|
|
134
120
|
}).then(() => {
|
|
@@ -177,12 +163,10 @@ export const initData = ({
|
|
|
177
163
|
export const loadMore = ({
|
|
178
164
|
getter,
|
|
179
165
|
setter,
|
|
180
|
-
|
|
181
|
-
query = {},
|
|
166
|
+
query,
|
|
182
167
|
type,
|
|
183
168
|
func,
|
|
184
169
|
api,
|
|
185
|
-
cacheTimeout,
|
|
186
170
|
uniqueKey,
|
|
187
171
|
errorRetry,
|
|
188
172
|
callback
|
|
@@ -207,7 +191,11 @@ export const loadMore = ({
|
|
|
207
191
|
return resolve(null)
|
|
208
192
|
}
|
|
209
193
|
|
|
210
|
-
if (
|
|
194
|
+
if (
|
|
195
|
+
type === ENUM.FETCH_TYPE.PAGINATION &&
|
|
196
|
+
query &&
|
|
197
|
+
+query.page === fieldData.page
|
|
198
|
+
) {
|
|
211
199
|
return resolve(null)
|
|
212
200
|
}
|
|
213
201
|
|
|
@@ -239,20 +227,21 @@ export const loadMore = ({
|
|
|
239
227
|
|
|
240
228
|
const getData = () => {
|
|
241
229
|
const funcCaller = typeof func === 'string' ? api[func] : func
|
|
242
|
-
|
|
230
|
+
if (!funcCaller) {
|
|
231
|
+
const error = new Error('func is not a function')
|
|
232
|
+
SET_ERROR({ setter, fieldName, error })
|
|
233
|
+
reject(error)
|
|
234
|
+
}
|
|
243
235
|
funcCaller(params)
|
|
244
236
|
.then((data: any) => {
|
|
245
237
|
SET_DATA({
|
|
246
238
|
getter,
|
|
247
239
|
setter,
|
|
248
|
-
cache,
|
|
249
240
|
data,
|
|
250
241
|
fieldName,
|
|
251
242
|
type,
|
|
252
|
-
fromLocal: false,
|
|
253
|
-
cacheTimeout,
|
|
254
243
|
page: params.page || 0,
|
|
255
|
-
insertBefore: !!query
|
|
244
|
+
insertBefore: !!query?.is_up
|
|
256
245
|
}).then(() => {
|
|
257
246
|
if (callback) {
|
|
258
247
|
callback({
|
|
@@ -281,16 +270,14 @@ export const loadMore = ({
|
|
|
281
270
|
export const updateState = ({
|
|
282
271
|
getter,
|
|
283
272
|
setter,
|
|
284
|
-
cache,
|
|
285
273
|
type,
|
|
286
274
|
func,
|
|
287
|
-
query
|
|
275
|
+
query,
|
|
288
276
|
method,
|
|
289
277
|
value,
|
|
290
278
|
id,
|
|
291
279
|
uniqueKey,
|
|
292
|
-
changeKey
|
|
293
|
-
cacheTimeout
|
|
280
|
+
changeKey
|
|
294
281
|
}: updateStateType) => {
|
|
295
282
|
return new Promise((resolve, reject) => {
|
|
296
283
|
const fieldName = generateFieldName({ func, type, query })
|
|
@@ -300,6 +287,11 @@ export const updateState = ({
|
|
|
300
287
|
return
|
|
301
288
|
}
|
|
302
289
|
|
|
290
|
+
if (fieldData.page === -1) {
|
|
291
|
+
resolve(null)
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
|
|
303
295
|
const _id = id || ''
|
|
304
296
|
const _uniqueKey = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME
|
|
305
297
|
const _changeKey = changeKey || ENUM.FIELD_DATA.RESULT_KEY
|
|
@@ -397,18 +389,6 @@ export const updateState = ({
|
|
|
397
389
|
type: ENUM.SETTER_TYPE.MERGE,
|
|
398
390
|
value: fieldData,
|
|
399
391
|
callback: () => {
|
|
400
|
-
if (cacheTimeout && cache) {
|
|
401
|
-
cache
|
|
402
|
-
.set({
|
|
403
|
-
key: fieldName,
|
|
404
|
-
value: fieldData,
|
|
405
|
-
timeout: cacheTimeout
|
|
406
|
-
})
|
|
407
|
-
.then(resolve)
|
|
408
|
-
.catch(resolve)
|
|
409
|
-
return
|
|
410
|
-
}
|
|
411
|
-
|
|
412
392
|
resolve(null)
|
|
413
393
|
}
|
|
414
394
|
})
|
package/src/setters.ts
CHANGED
|
@@ -1,50 +1,48 @@
|
|
|
1
|
-
import { computeResultLength, setReactivityField } from './utils'
|
|
1
|
+
import { computeResultLength, setReactivityField, isObjectResult } from './utils'
|
|
2
2
|
import ENUM from './enum'
|
|
3
3
|
import type { setDataType, setErrorType } from './types'
|
|
4
4
|
|
|
5
5
|
export const SET_DATA = ({
|
|
6
6
|
getter,
|
|
7
7
|
setter,
|
|
8
|
-
cache,
|
|
9
8
|
data,
|
|
10
9
|
fieldName,
|
|
11
10
|
type,
|
|
12
|
-
fromLocal,
|
|
13
|
-
cacheTimeout,
|
|
14
11
|
page,
|
|
15
12
|
insertBefore
|
|
16
13
|
}: setDataType): Promise<any> => {
|
|
17
14
|
return new Promise((resolve, reject) => {
|
|
18
|
-
if (fromLocal) {
|
|
19
|
-
setter({
|
|
20
|
-
key: fieldName,
|
|
21
|
-
type: ENUM.SETTER_TYPE.RESET,
|
|
22
|
-
value: data,
|
|
23
|
-
callback: () => {
|
|
24
|
-
resolve(null)
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
15
|
const fieldData = getter(fieldName)
|
|
31
16
|
if (!fieldData) {
|
|
32
17
|
reject()
|
|
33
18
|
return
|
|
34
19
|
}
|
|
35
20
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
fieldData.
|
|
43
|
-
fieldData.
|
|
21
|
+
let result
|
|
22
|
+
let extra
|
|
23
|
+
|
|
24
|
+
if (isObjectResult(data)) {
|
|
25
|
+
result = data
|
|
26
|
+
fieldData.nothing = false
|
|
27
|
+
fieldData.fetched = true
|
|
28
|
+
fieldData.noMore = true
|
|
29
|
+
fieldData.page = -1
|
|
44
30
|
} else {
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
result = data.result
|
|
32
|
+
extra = data.extra
|
|
33
|
+
const isEmpty = computeResultLength(result) === 0
|
|
34
|
+
fieldData.nothing = fieldData.fetched ? false : isEmpty
|
|
35
|
+
fieldData.fetched = true
|
|
36
|
+
fieldData.total = data.total || 0
|
|
37
|
+
if (type === ENUM.FETCH_TYPE.PAGINATION) {
|
|
38
|
+
fieldData.noMore = false
|
|
39
|
+
fieldData.page = +page
|
|
40
|
+
} else {
|
|
41
|
+
fieldData.noMore = data.no_more || isEmpty
|
|
42
|
+
fieldData.page = fieldData.page + 1
|
|
43
|
+
}
|
|
47
44
|
}
|
|
45
|
+
|
|
48
46
|
fieldData.loading = false
|
|
49
47
|
setReactivityField(
|
|
50
48
|
fieldData,
|
|
@@ -68,18 +66,6 @@ export const SET_DATA = ({
|
|
|
68
66
|
type: ENUM.SETTER_TYPE.RESET,
|
|
69
67
|
value: fieldData,
|
|
70
68
|
callback: () => {
|
|
71
|
-
if (cacheTimeout && cache && !fieldData.nothing) {
|
|
72
|
-
cache
|
|
73
|
-
.set({
|
|
74
|
-
key: fieldName,
|
|
75
|
-
value: fieldData,
|
|
76
|
-
timeout: cacheTimeout
|
|
77
|
-
})
|
|
78
|
-
.then(resolve)
|
|
79
|
-
.catch(resolve)
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
|
|
83
69
|
resolve(null)
|
|
84
70
|
}
|
|
85
71
|
})
|
package/src/types.ts
CHANGED
|
@@ -33,13 +33,13 @@ export type fieldKeys =
|
|
|
33
33
|
export type generateFieldProps = {
|
|
34
34
|
func: string | (() => {})
|
|
35
35
|
type: fetchTypes
|
|
36
|
-
query
|
|
36
|
+
query?: keyMap
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export type generateParamsType = {
|
|
40
40
|
field: defaultField
|
|
41
41
|
uniqueKey: string
|
|
42
|
-
query
|
|
42
|
+
query?: keyMap
|
|
43
43
|
type: fetchTypes
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -66,24 +66,9 @@ export type initStateType = {
|
|
|
66
66
|
opts?: keyMap
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
export type cacheType = {
|
|
70
|
-
get: ({ key }: { key: string }) => Promise<any>
|
|
71
|
-
set: ({
|
|
72
|
-
key,
|
|
73
|
-
value,
|
|
74
|
-
timeout
|
|
75
|
-
}: {
|
|
76
|
-
key: string
|
|
77
|
-
value: any
|
|
78
|
-
timeout?: number
|
|
79
|
-
}) => Promise<any>
|
|
80
|
-
del: ({ key }: { key: string }) => void
|
|
81
|
-
}
|
|
82
|
-
|
|
83
69
|
export type initDataType = {
|
|
84
70
|
getter: (str: string) => defaultField
|
|
85
71
|
setter: (obj: setterFuncParams) => void
|
|
86
|
-
cache: cacheType
|
|
87
72
|
func: string | (() => {})
|
|
88
73
|
type: fetchTypes
|
|
89
74
|
query?: keyMap
|
|
@@ -96,7 +81,6 @@ export type initDataType = {
|
|
|
96
81
|
export type loadMoreType = {
|
|
97
82
|
getter: (str: string) => defaultField
|
|
98
83
|
setter: (obj: setterFuncParams) => void
|
|
99
|
-
cache: cacheType
|
|
100
84
|
func: string | (() => {})
|
|
101
85
|
type: fetchTypes
|
|
102
86
|
query?: keyMap
|
|
@@ -110,7 +94,6 @@ export type loadMoreType = {
|
|
|
110
94
|
export type updateStateType = {
|
|
111
95
|
getter: (str: string) => defaultField
|
|
112
96
|
setter: (obj: setterFuncParams) => void
|
|
113
|
-
cache: cacheType
|
|
114
97
|
func: string | (() => {})
|
|
115
98
|
type: fetchTypes
|
|
116
99
|
query?: keyMap
|
|
@@ -125,12 +108,9 @@ export type updateStateType = {
|
|
|
125
108
|
export type setDataType = {
|
|
126
109
|
getter: (str: string) => defaultField
|
|
127
110
|
setter: (obj: setterFuncParams) => void
|
|
128
|
-
cache: cacheType
|
|
129
111
|
data: any
|
|
130
112
|
fieldName: string
|
|
131
113
|
type: fetchTypes
|
|
132
|
-
fromLocal: boolean
|
|
133
|
-
cacheTimeout: number
|
|
134
114
|
page: number
|
|
135
115
|
insertBefore: boolean
|
|
136
116
|
}
|
package/src/utils.ts
CHANGED
|
@@ -12,12 +12,16 @@ import type {
|
|
|
12
12
|
generateParamsResp
|
|
13
13
|
} from './types'
|
|
14
14
|
|
|
15
|
+
export const isObjectResult = (data: Record<string, any>): boolean => {
|
|
16
|
+
return data.result === undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
15
19
|
export const generateDefaultField = (opts = {}): defaultField => ({
|
|
16
20
|
...{
|
|
17
21
|
result: [],
|
|
18
22
|
noMore: false,
|
|
19
23
|
nothing: false,
|
|
20
|
-
loading:
|
|
24
|
+
loading: true,
|
|
21
25
|
error: null,
|
|
22
26
|
extra: null,
|
|
23
27
|
fetched: false,
|
|
@@ -27,7 +31,6 @@ export const generateDefaultField = (opts = {}): defaultField => ({
|
|
|
27
31
|
...opts
|
|
28
32
|
})
|
|
29
33
|
|
|
30
|
-
let seed = 0
|
|
31
34
|
/**
|
|
32
35
|
* 根据参数生成 field 的 namespace
|
|
33
36
|
* @param {string} func
|
|
@@ -40,7 +43,10 @@ export const generateFieldName = ({
|
|
|
40
43
|
type,
|
|
41
44
|
query = {}
|
|
42
45
|
}: generateFieldProps): string => {
|
|
43
|
-
func =
|
|
46
|
+
func =
|
|
47
|
+
typeof func === 'string'
|
|
48
|
+
? func
|
|
49
|
+
: `api-${Math.random().toString(36).substring(2)}`
|
|
44
50
|
type = type || 'auto'
|
|
45
51
|
let result = `${func}-${type}`
|
|
46
52
|
Object.keys(query)
|
|
@@ -263,11 +269,10 @@ export const computeResultLength = (data: fieldResult): number => {
|
|
|
263
269
|
export const generateRequestParams = ({
|
|
264
270
|
field,
|
|
265
271
|
uniqueKey,
|
|
266
|
-
query,
|
|
272
|
+
query = {},
|
|
267
273
|
type
|
|
268
274
|
}: generateParamsType): generateParamsResp => {
|
|
269
275
|
const result: generateParamsResp = {}
|
|
270
|
-
query = query || {}
|
|
271
276
|
if (field.fetched) {
|
|
272
277
|
const changing = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME
|
|
273
278
|
if (type === ENUM.FETCH_TYPE.AUTO) {
|
package/dist/src/actions.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { initStateType, initDataType, loadMoreType, updateStateType } from './types';
|
|
2
|
-
export declare const initState: ({ getter, setter, func, type, query, opts }: initStateType) => Promise<null>;
|
|
3
|
-
export declare const initData: ({ getter, setter, cache, func, type, query, api, cacheTimeout, uniqueKey, callback }: initDataType) => Promise<any>;
|
|
4
|
-
export declare const loadMore: ({ getter, setter, cache, query, type, func, api, cacheTimeout, uniqueKey, errorRetry, callback }: loadMoreType) => Promise<any>;
|
|
5
|
-
export declare const updateState: ({ getter, setter, cache, type, func, query, method, value, id, uniqueKey, changeKey, cacheTimeout }: updateStateType) => Promise<unknown>;
|
package/dist/src/enum.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
declare const _default: {
|
|
2
|
-
SETTER_TYPE: {
|
|
3
|
-
RESET: number;
|
|
4
|
-
MERGE: number;
|
|
5
|
-
};
|
|
6
|
-
FETCH_TYPE_ARRAY: string[];
|
|
7
|
-
FETCH_TYPE: {
|
|
8
|
-
PAGINATION: string;
|
|
9
|
-
SINCE_FIRST_OR_END_ID: string;
|
|
10
|
-
SCROLL_LOAD_MORE: string;
|
|
11
|
-
HAS_LOADED_IDS: string;
|
|
12
|
-
AUTO: string;
|
|
13
|
-
};
|
|
14
|
-
CHANGE_TYPE: {
|
|
15
|
-
SEARCH_FIELD: string;
|
|
16
|
-
RESET_FIELD: string;
|
|
17
|
-
RESULT_UPDATE_KV: string;
|
|
18
|
-
RESULT_ADD_AFTER: string;
|
|
19
|
-
RESULT_ADD_BEFORE: string;
|
|
20
|
-
RESULT_REMOVE_BY_ID: string;
|
|
21
|
-
RESULT_INSERT_TO_BEFORE: string;
|
|
22
|
-
RESULT_INSERT_TO_AFTER: string;
|
|
23
|
-
RESULT_LIST_MERGE: string;
|
|
24
|
-
RESULT_ITEM_MERGE: string;
|
|
25
|
-
};
|
|
26
|
-
FIELD_DATA: {
|
|
27
|
-
RESULT_KEY: string;
|
|
28
|
-
EXTRA_KEY: string;
|
|
29
|
-
};
|
|
30
|
-
DEFAULT_UNIQUE_KEY_NAME: string;
|
|
31
|
-
};
|
|
32
|
-
export default _default;
|
package/dist/src/index.d.ts
DELETED
package/dist/src/setters.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { setDataType, setErrorType } from './types';
|
|
2
|
-
export declare const SET_DATA: ({ getter, setter, cache, data, fieldName, type, fromLocal, cacheTimeout, page, insertBefore }: setDataType) => Promise<any>;
|
|
3
|
-
export declare const SET_ERROR: ({ setter, fieldName, error }: setErrorType) => void;
|