@flowlist/js-core 2.0.0 → 2.1.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 CHANGED
@@ -1,4 +1,4 @@
1
- export * from './actions';
2
- export * as utils from './utils';
3
- export { default as ENUM } from './enum';
4
- export * from './types';
1
+ export * from './src/actions';
2
+ export * as utils from './src/utils';
3
+ export { default as ENUM } from './src/enum';
4
+ export * from './src/types';
@@ -49,6 +49,9 @@ var ENUM = {
49
49
  },
50
50
  DEFAULT_UNIQUE_KEY_NAME: "id"
51
51
  };
52
+ const isObjectResult = (data) => {
53
+ return data.result === void 0;
54
+ };
52
55
  const generateDefaultField = (opts = {}) => __spreadValues(__spreadValues({}, {
53
56
  result: [],
54
57
  noMore: false,
@@ -180,11 +183,10 @@ const computeResultLength = (data) => {
180
183
  const generateRequestParams = ({
181
184
  field,
182
185
  uniqueKey,
183
- query,
186
+ query = {},
184
187
  type
185
188
  }) => {
186
189
  const result = {};
187
- query = query || {};
188
190
  if (field.fetched) {
189
191
  const changing = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME;
190
192
  if (type === ENUM.FETCH_TYPE.AUTO) {
@@ -223,6 +225,7 @@ const generateRequestParams = ({
223
225
  };
224
226
  var utils = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
225
227
  __proto__: null,
228
+ isObjectResult,
226
229
  generateDefaultField,
227
230
  generateFieldName,
228
231
  getObjectDeepValue,
@@ -250,17 +253,28 @@ const SET_DATA = ({
250
253
  reject();
251
254
  return;
252
255
  }
253
- const { result, extra } = data;
254
- const isEmpty = computeResultLength(result) === 0;
255
- fieldData.nothing = fieldData.fetched ? false : isEmpty;
256
- fieldData.fetched = true;
257
- fieldData.total = data.total || 0;
258
- if (type === ENUM.FETCH_TYPE.PAGINATION) {
259
- fieldData.noMore = false;
260
- fieldData.page = +page;
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;
261
264
  } else {
262
- fieldData.noMore = data.no_more || isEmpty;
263
- fieldData.page = fieldData.page + 1;
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 = data.no_more || isEmpty;
276
+ fieldData.page = fieldData.page + 1;
277
+ }
264
278
  }
265
279
  fieldData.loading = false;
266
280
  setReactivityField(fieldData, ENUM.FIELD_DATA.RESULT_KEY, result, type, insertBefore);
@@ -290,8 +304,8 @@ const initState = ({
290
304
  setter,
291
305
  func,
292
306
  type,
293
- query = {},
294
- opts = {}
307
+ query,
308
+ opts
295
309
  }) => {
296
310
  return new Promise((resolve) => {
297
311
  const fieldName = generateFieldName({ func, type, query });
@@ -315,15 +329,15 @@ const initData = ({
315
329
  setter,
316
330
  func,
317
331
  type,
318
- query = {},
332
+ query,
319
333
  api,
320
334
  uniqueKey,
321
335
  callback
322
336
  }) => new Promise((resolve, reject) => {
323
337
  const fieldName = generateFieldName({ func, type, query });
324
338
  const fieldData = getter(fieldName);
325
- const doRefresh = !!query.__refresh__;
326
- const needReset = !!query.__reload__;
339
+ const doRefresh = !!(query == null ? void 0 : query.__refresh__);
340
+ const needReset = !!(query == null ? void 0 : query.__reload__);
327
341
  if (fieldData && fieldData.error && !doRefresh) {
328
342
  return resolve(null);
329
343
  }
@@ -403,7 +417,7 @@ const initData = ({
403
417
  const loadMore = ({
404
418
  getter,
405
419
  setter,
406
- query = {},
420
+ query,
407
421
  type,
408
422
  func,
409
423
  api,
@@ -425,7 +439,7 @@ const loadMore = ({
425
439
  if (fieldData.noMore && !errorRetry) {
426
440
  return resolve(null);
427
441
  }
428
- if (type === ENUM.FETCH_TYPE.PAGINATION && +query.page === fieldData.page) {
442
+ if (type === ENUM.FETCH_TYPE.PAGINATION && query && +query.page === fieldData.page) {
429
443
  return resolve(null);
430
444
  }
431
445
  let loadingState;
@@ -459,7 +473,7 @@ const loadMore = ({
459
473
  fieldName,
460
474
  type,
461
475
  page: params.page || 0,
462
- insertBefore: !!query.is_up
476
+ insertBefore: !!(query == null ? void 0 : query.is_up)
463
477
  }).then(() => {
464
478
  if (callback) {
465
479
  callback({
@@ -487,7 +501,7 @@ const updateState = ({
487
501
  setter,
488
502
  type,
489
503
  func,
490
- query = {},
504
+ query,
491
505
  method,
492
506
  value,
493
507
  id,
@@ -501,6 +515,10 @@ const updateState = ({
501
515
  reject();
502
516
  return;
503
517
  }
518
+ if (fieldData.page === -1) {
519
+ resolve(null);
520
+ return;
521
+ }
504
522
  const _id = id || "";
505
523
  const _uniqueKey = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME;
506
524
  const _changeKey = changeKey || ENUM.FIELD_DATA.RESULT_KEY;
@@ -1 +1 @@
1
- var z=Object.defineProperty,J=Object.defineProperties;var W=Object.getOwnPropertyDescriptors;var k=Object.getOwnPropertySymbols;var Z=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var x=(l,o,E)=>o in l?z(l,o,{enumerable:!0,configurable:!0,writable:!0,value:E}):l[o]=E,I=(l,o)=>{for(var E in o||(o={}))Z.call(o,E)&&x(l,E,o[E]);if(k)for(var E of k(o))v.call(o,E)&&x(l,E,o[E]);return l},b=(l,o)=>J(l,W(o));(function(l,o){typeof exports=="object"&&typeof module!="undefined"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(l=typeof globalThis!="undefined"?globalThis:l||self,o(l.JsCore={}))})(this,function(l){"use strict";const o=["jump","sinceId","page","seenIds","auto"];var E={SETTER_TYPE:{RESET:0,MERGE:1},FETCH_TYPE_ARRAY:o,FETCH_TYPE:{PAGINATION:o[0],SINCE_FIRST_OR_END_ID:o[1],SCROLL_LOAD_MORE:o[2],HAS_LOADED_IDS:o[3],AUTO:o[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 N=(e={})=>I({result:[],noMore:!1,nothing:!1,loading:!1,error:null,extra:null,fetched:!1,page:0,total:0},e),h=({func:e,type:n,query:t={}})=>{e=typeof e=="string"?e:`api-${Math.random().toString(36).substring(2)}`,n=n||"auto";let s=`${e}-${n}`;return Object.keys(t).filter(_=>!~["undefined","object","function"].indexOf(typeof t[_])&&!~["page","is_up","since_id","seen_ids","__refresh__","__reload__"].indexOf(_)).sort().forEach(_=>{s+=`-${_}-${t[_]}`}),s},L=(e,n)=>{if(!n)return e||"";let t=e||"";return(p(n)?n:n.split(".")).forEach(_=>{t=t[_]}),t||""},m=(e,n,t)=>{if(/\./.test(n)){const s=n.split("."),_=s.pop();let a=e;s.forEach(i=>{a=a[i]}),a[_]=t}else e[n]=t},G=(e,n,t)=>{if(p(e)){const s=O(n,e,t);return s<0?void 0:e[s]}return e[n]},O=(e,n,t)=>{let s=-1;for(let _=0;_<n.length;_++)if(L(n[_],t.toString()).toString()===(e||"").toString()){s=_;break}return s},j=(e,n,t)=>{p(n)?n.forEach(s=>{const _=L(s,t).toString();e.forEach((a,i)=>{L(a,t).toString()===_&&(e[i]=I(I({},a),s))})}):Object.keys(n).forEach(s=>{const _=(s||"").toString();e.forEach((a,i)=>{L(a,t).toString()===_&&(e[i]=I(I({},a),n[s]))})})},p=e=>Object.prototype.toString.call(e)==="[object Array]",H=(e,n,t,s,_)=>{if(s===E.FETCH_TYPE.PAGINATION){e[n]=t;return}if(p(t)){e[n]=_?t.concat(e[n]||[]):(e[n]||[]).concat(t);return}if(n!==E.FIELD_DATA.RESULT_KEY){e[n]=t;return}p(e[n])&&(e[n]={}),Object.keys(t).forEach(a=>{e[n][a]=e[n][a]?_?t[a].concat(e[n][a]):e[n][a].concat(t[a]):t[a]})},U=e=>{let n=0;return p(e)?n=e.length:Object.keys(e).forEach(t=>{n+=e[t].length}),n},M=({field:e,uniqueKey:n,query:t,type:s})=>{const _={};if(t=t||{},e.fetched){const a=n||E.DEFAULT_UNIQUE_KEY_NAME;s===E.FETCH_TYPE.AUTO?(_.seen_ids=e.result.map(i=>L(i,a)).join(","),_.since_id=L(e.result[t.is_up?0:e.result.length-1],a),_.is_up=t.is_up?1:0,_.page=t.page||e.page+1):s===E.FETCH_TYPE.HAS_LOADED_IDS?_.seen_ids=e.result.map(i=>L(i,a)).join(","):s===E.FETCH_TYPE.SINCE_FIRST_OR_END_ID?(_.since_id=L(e.result[t.is_up?0:e.result.length-1],a),_.is_up=t.is_up?1:0):s===E.FETCH_TYPE.PAGINATION?_.page=t.page:s===E.FETCH_TYPE.SCROLL_LOAD_MORE&&(_.page=e.page+1)}else s===E.FETCH_TYPE.AUTO?(_.seen_ids="",_.since_id=t.sinceId||(t.is_up?999999999:0),_.is_up=t.is_up?1:0,_.page=t.page||e.page||1):s===E.FETCH_TYPE.HAS_LOADED_IDS?_.seen_ids="":s===E.FETCH_TYPE.SINCE_FIRST_OR_END_ID?(_.since_id=t.sinceId||(t.is_up?999999999:0),_.is_up=t.is_up?1:0):s===E.FETCH_TYPE.PAGINATION?_.page=t.page||e.page:s===E.FETCH_TYPE.SCROLL_LOAD_MORE&&(_.page=1);return I(I({},t),_)};var B=Object.freeze(Object.defineProperty({__proto__:null,generateDefaultField:N,generateFieldName:h,getObjectDeepValue:L,updateObjectDeepValue:m,searchValueByKey:G,computeMatchedItemIndex:O,combineArrayData:j,isArray:p,setReactivityField:H,computeResultLength:U,generateRequestParams:M},Symbol.toStringTag,{value:"Module"}));const K=({getter:e,setter:n,data:t,fieldName:s,type:_,page:a,insertBefore:i})=>new Promise((u,S)=>{const T=e(s);if(!T){S();return}const{result:D,extra:A}=t,f=U(D)===0;T.nothing=T.fetched?!1:f,T.fetched=!0,T.total=t.total||0,_===E.FETCH_TYPE.PAGINATION?(T.noMore=!1,T.page=+a):(T.noMore=t.no_more||f,T.page=T.page+1),T.loading=!1,H(T,E.FIELD_DATA.RESULT_KEY,D,_,i),A&&H(T,E.FIELD_DATA.EXTRA_KEY,A,_,i),n({key:s,type:E.SETTER_TYPE.RESET,value:T,callback:()=>{u(null)}})}),V=({setter:e,fieldName:n,error:t})=>{e({key:n,type:E.SETTER_TYPE.MERGE,value:{error:t,loading:!1}})},w=({getter:e,setter:n,func:t,type:s,query:_={},opts:a={}})=>new Promise(i=>{const u=h({func:t,type:s,query:_});if(e(u)){i(null);return}n({key:u,type:E.SETTER_TYPE.RESET,value:N(a),callback:()=>{i(null)}})}),X=({getter:e,setter:n,func:t,type:s,query:_={},api:a,uniqueKey:i,callback:u})=>new Promise((S,T)=>{const D=h({func:t,type:s,query:_}),A=e(D),f=!!_.__refresh__,c=!!_.__reload__;if(A&&A.error&&!f||A&&A.loading)return S(null);const R=A&&A.fetched&&!f;if(R)return S(null);const g=M({field:b(I({},A),{fetched:!1}),uniqueKey:i,query:_,type:s}),F=()=>{(()=>new Promise(Y=>{(()=>{(typeof t=="string"?a[t]:t)(g).then(Y).catch(C=>{V({setter:n,fieldName:D,error:C}),T(C)})})()}))().then(Y=>{const r=()=>{K({getter:e,setter:n,data:Y,fieldName:D,type:s,page:g.page||0,insertBefore:!1}).then(()=>{u&&u({params:g,data:Y,refresh:f}),S(null)})};c?n({key:D,type:E.SETTER_TYPE.RESET,value:N(),callback:r}):r()})};!R&&!c?n({key:D,type:E.SETTER_TYPE.RESET,value:b(I({},N()),{loading:!0,error:null}),callback:F}):F()}),$=({getter:e,setter:n,query:t={},type:s,func:_,api:a,uniqueKey:i,errorRetry:u,callback:S})=>new Promise((T,D)=>{const A=h({func:_,type:s,query:t}),f=e(A);if(!f||f.loading||f.nothing||f.noMore&&!u||s===E.FETCH_TYPE.PAGINATION&&+t.page===f.page)return T(null);let c;s===E.FETCH_TYPE.PAGINATION?c={loading:!0,error:null,[E.FIELD_DATA.RESULT_KEY]:[],[E.FIELD_DATA.EXTRA_KEY]:null}:c={loading:!0,error:null};const R=M({field:f,uniqueKey:i,query:t,type:s});R[E.FIELD_DATA.EXTRA_KEY]=f[E.FIELD_DATA.EXTRA_KEY];const g=()=>{(typeof _=="string"?a[_]:_)(R).then(d=>{K({getter:e,setter:n,data:d,fieldName:A,type:s,page:R.page||0,insertBefore:!!t.is_up}).then(()=>{S&&S({params:R,data:d,refresh:!1}),T(null)})}).catch(d=>{V({setter:n,fieldName:A,error:d}),D(d)})};n({key:A,type:E.SETTER_TYPE.MERGE,value:c,callback:g})}),Q=({getter:e,setter:n,type:t,func:s,query:_={},method:a,value:i,id:u,uniqueKey:S,changeKey:T})=>new Promise((D,A)=>{const f=h({func:s,type:t,query:_}),c=e(f);if(!c){A();return}const R=u||"",g=S||E.DEFAULT_UNIQUE_KEY_NAME,F=T||E.FIELD_DATA.RESULT_KEY,d=U(c[E.FIELD_DATA.RESULT_KEY]);if(a===E.CHANGE_TYPE.SEARCH_FIELD)D(G(c[E.FIELD_DATA.RESULT_KEY],R,g));else if(a===E.CHANGE_TYPE.RESULT_UPDATE_KV){const r=O(R,c[E.FIELD_DATA.RESULT_KEY],g);m(c[E.FIELD_DATA.RESULT_KEY][r],F,i)}else if(a===E.CHANGE_TYPE.RESULT_ITEM_MERGE){const r=O(R,c[E.FIELD_DATA.RESULT_KEY],g);c[E.FIELD_DATA.RESULT_KEY][r]=I(I({},c[E.FIELD_DATA.RESULT_KEY][r]),i)}else if(a===E.CHANGE_TYPE.RESET_FIELD)m(c,F,i);else{let r=L(c,F);const P=O(R,r,g);switch(a){case E.CHANGE_TYPE.RESULT_ADD_AFTER:p(i)?r=r.concat(i):r.push(i);break;case E.CHANGE_TYPE.RESULT_ADD_BEFORE:p(i)?r=i.concat(r):r.unshift(i);break;case E.CHANGE_TYPE.RESULT_REMOVE_BY_ID:P>=0?r.splice(P,1):p(R)&&(r=r.filter(C=>R.indexOf(C[g])===-1));break;case E.CHANGE_TYPE.RESULT_INSERT_TO_BEFORE:P>=0&&r.splice(P,0,i);break;case E.CHANGE_TYPE.RESULT_INSERT_TO_AFTER:P>=0&&r.splice(P+1,0,i);break;case E.CHANGE_TYPE.RESULT_LIST_MERGE:j(r,i,g);break}c[F]=r}const Y=U(c[E.FIELD_DATA.RESULT_KEY]);c.total=c.total+Y-d,c.nothing=Y===0,n({key:f,type:E.SETTER_TYPE.MERGE,value:c,callback:()=>{D(null)}})});l.ENUM=E,l.initData=X,l.initState=w,l.loadMore=$,l.updateState=Q,l.utils=B,Object.defineProperties(l,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
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,A;if(G(e))R=e,T.nothing=!1,T.fetched=!0,T.noMore=!0,T.page=-1;else{R=e.result,A=e.extra;const f=U(R)===0;T.nothing=T.fetched?!1:f,T.fetched=!0,T.total=e.total||0,E===n.FETCH_TYPE.PAGINATION?(T.noMore=!1,T.page=+i):(T.noMore=e.no_more||f,T.page=T.page+1)}T.loading=!1,M(T,n.FIELD_DATA.RESULT_KEY,R,E,a),A&&M(T,n.FIELD_DATA.EXTRA_KEY,A,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}),A=t(R),f=!!(E!=null&&E.__refresh__),o=!!(E!=null&&E.__reload__);if(A&&A.error&&!f||A&&A.loading)return S(null);const D=A&&A.fetched&&!f;if(D)return S(null);const g=H({field:b(I({},A),{fetched:!1}),uniqueKey:a,query:E,type:s}),F=()=>{(()=>new Promise(d=>{(()=>{(typeof e=="string"?i[e]:e)(g).then(d).catch(C=>{V({setter:_,fieldName:R,error:C}),T(C)})})()}))().then(d=>{const l=()=>{x({getter:t,setter:_,data:d,fieldName:R,type:s,page:g.page||0,insertBefore:!1}).then(()=>{p&&p({params:g,data:d,refresh:f}),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 A=h({func:E,type:s,query:e}),f=t(A);if(!f||f.loading||f.nothing||f.noMore&&!p||s===n.FETCH_TYPE.PAGINATION&&e&&+e.page===f.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:f,uniqueKey:a,query:e,type:s});D[n.FIELD_DATA.EXTRA_KEY]=f[n.FIELD_DATA.EXTRA_KEY];const g=()=>{(typeof E=="string"?i[E]:E)(D).then(Y=>{x({getter:t,setter:_,data:Y,fieldName:A,type:s,page:D.page||0,insertBefore:!!(e!=null&&e.is_up)}).then(()=>{S&&S({params:D,data:Y,refresh:!1}),T(null)})}).catch(Y=>{V({setter:_,fieldName:A,error:Y}),R(Y)})};_({key:A,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,A)=>{const f=h({func:s,type:e,query:E}),o=t(f);if(!o){A();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,Y=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 d=U(o[n.FIELD_DATA.RESULT_KEY]);o.total=o.total+d-Y,o.nothing=d===0,_({key:f,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"}})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowlist/js-core",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "main": "dist/js-core.umd.js",
5
5
  "module": "dist/js-core.es.js",
6
6
  "types": "dist/index.d.ts",
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 })
@@ -52,7 +52,7 @@ export const initData = ({
52
52
  setter,
53
53
  func,
54
54
  type,
55
- query = {},
55
+ query,
56
56
  api,
57
57
  uniqueKey,
58
58
  callback
@@ -60,8 +60,8 @@ export const initData = ({
60
60
  new Promise((resolve, reject) => {
61
61
  const fieldName = generateFieldName({ func, type, query })
62
62
  const fieldData = getter(fieldName)
63
- const doRefresh = !!query.__refresh__
64
- const needReset = !!query.__reload__
63
+ const doRefresh = !!query?.__refresh__
64
+ const needReset = !!query?.__reload__
65
65
  // 如果 error 了,就不再请求
66
66
  if (fieldData && fieldData.error && !doRefresh) {
67
67
  return resolve(null)
@@ -159,7 +159,7 @@ export const initData = ({
159
159
  export const loadMore = ({
160
160
  getter,
161
161
  setter,
162
- query = {},
162
+ query,
163
163
  type,
164
164
  func,
165
165
  api,
@@ -187,7 +187,11 @@ export const loadMore = ({
187
187
  return resolve(null)
188
188
  }
189
189
 
190
- if (type === ENUM.FETCH_TYPE.PAGINATION && +query.page === fieldData.page) {
190
+ if (
191
+ type === ENUM.FETCH_TYPE.PAGINATION &&
192
+ query &&
193
+ +query.page === fieldData.page
194
+ ) {
191
195
  return resolve(null)
192
196
  }
193
197
 
@@ -229,7 +233,7 @@ export const loadMore = ({
229
233
  fieldName,
230
234
  type,
231
235
  page: params.page || 0,
232
- insertBefore: !!query.is_up
236
+ insertBefore: !!query?.is_up
233
237
  }).then(() => {
234
238
  if (callback) {
235
239
  callback({
@@ -260,7 +264,7 @@ export const updateState = ({
260
264
  setter,
261
265
  type,
262
266
  func,
263
- query = {},
267
+ query,
264
268
  method,
265
269
  value,
266
270
  id,
@@ -275,6 +279,11 @@ export const updateState = ({
275
279
  return
276
280
  }
277
281
 
282
+ if (fieldData.page === -1) {
283
+ resolve(null)
284
+ return
285
+ }
286
+
278
287
  const _id = id || ''
279
288
  const _uniqueKey = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME
280
289
  const _changeKey = changeKey || ENUM.FIELD_DATA.RESULT_KEY
package/src/setters.ts CHANGED
@@ -1,4 +1,4 @@
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
 
@@ -18,18 +18,31 @@ export const SET_DATA = ({
18
18
  return
19
19
  }
20
20
 
21
- const { result, extra } = data
22
- const isEmpty = computeResultLength(result) === 0
23
- fieldData.nothing = fieldData.fetched ? false : isEmpty
24
- fieldData.fetched = true
25
- fieldData.total = data.total || 0
26
- if (type === ENUM.FETCH_TYPE.PAGINATION) {
27
- fieldData.noMore = false
28
- fieldData.page = +page
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
29
30
  } else {
30
- fieldData.noMore = data.no_more || isEmpty
31
- fieldData.page = fieldData.page + 1
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
+ }
32
44
  }
45
+
33
46
  fieldData.loading = false
34
47
  setReactivityField(
35
48
  fieldData,
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: keyMap
36
+ query?: keyMap
37
37
  }
38
38
 
39
39
  export type generateParamsType = {
40
40
  field: defaultField
41
41
  uniqueKey: string
42
- query: keyMap
42
+ query?: keyMap
43
43
  type: fetchTypes
44
44
  }
45
45
 
package/src/utils.ts CHANGED
@@ -12,6 +12,10 @@ 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: [],
@@ -265,11 +269,10 @@ export const computeResultLength = (data: fieldResult): number => {
265
269
  export const generateRequestParams = ({
266
270
  field,
267
271
  uniqueKey,
268
- query,
272
+ query = {},
269
273
  type
270
274
  }: generateParamsType): generateParamsResp => {
271
275
  const result: generateParamsResp = {}
272
- query = query || {}
273
276
  if (field.fetched) {
274
277
  const changing = uniqueKey || ENUM.DEFAULT_UNIQUE_KEY_NAME
275
278
  if (type === ENUM.FETCH_TYPE.AUTO) {
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './actions'
2
- export * as utils from './utils'
3
- export { default as ENUM } from './enum'
4
- export * from './types'