@marianmeres/fetch-store 2.10.0 → 2.12.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.cjs +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=t=>"function"==typeof t,e=(e,r="")=>{if(!t(e))throw new TypeError(`${r} Expecting function arg`.trim())},r=e=>t(e.subscribe),n=(r,n=null)=>{const s=e=>t(n
|
|
1
|
+
"use strict";const t=t=>"function"==typeof t,e=(e,r="")=>{if(!t(e))throw new TypeError(`${r} Expecting function arg`.trim())},r=e=>t(e.subscribe),n=(r,n=null)=>{const s=e=>t(null==n?void 0:n.persist)&&n.persist(e);let c=(()=>{const t=new Map,e=e=>(t.has(e)||t.set(e,new Set),t.get(e)),r=(t,r)=>{if(!t)throw new TypeError("Expecting valid event name.");if("function"!=typeof r)throw new TypeError("Expecting valid callback function.");return e(t).add(r),()=>e(t).delete(r)};return{publish:(t,r)=>{e(t).forEach(t=>t(r))},subscribe:r,subscribeOnce:(t,e)=>{const n=r(t,t=>{e(t),n()});return n},unsubscribe:(t,r)=>{e(t).delete(r)},unsubscribeAll:e=>t.delete(e)}})(),l=r;s(l);const a=()=>l,u=t=>{l!==t&&(l=t,s(l),c.publish("change",l))};return{set:u,get:a,update:t=>{e(t,"[update]"),u(t(a()))},subscribe:t=>(e(t,"[subscribe]"),t(l),c.subscribe("change",t))}},s=(s,c,l=null)=>{const a=e=>t(null==l?void 0:l.persist)&&l.persist(e),u=n(null==l?void 0:l.initialValue),o=[];if(s.forEach(t=>{if(!r(t))throw new TypeError("Expecting array of StoreLike objects");t.subscribe(t=>o.push(t))()}),!t(c))throw new TypeError("Expecting second argument to be the derivative function");if(!c.length||c.length>2)throw new TypeError("Expecting the derivative function to have exactly 1 or 2 arguments");let i=0,h=[];const F=t=>{e(t,"[derived.subscribe]"),i++||s.forEach((t,e)=>{h.push(t.subscribe(t=>{o[e]=t,1===c.length?(u.set(c(o)),a(u.get())):c(o,t=>{u.set(t),a(u.get())})}))});const r=u.subscribe(t);return()=>{--i||(h.forEach(t=>t()),h=[]),r()}};return{get:()=>{let t;return F(e=>t=e)(),t},subscribe:F}},c=t=>"function"==typeof t,l={},a={fetchOnceDefaultThresholdMs:3e5},u=t=>"function"==typeof t;exports.createFetchStore=(t,e=null,r=null,c={})=>{const{fetchOnceDefaultThresholdMs:l}={...a,...c||{}},o=(t,e)=>u(r)?r?.(t,e):t,i=n(o(e),c),h=n({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0,lastFetchSilentError:null}),{subscribe:F,get:g}=s([i,h],([t,e])=>({data:t,...e})),E=async(...e)=>{let r=h.get();r.isFetching=!0,r.lastFetchStart=new Date,r.lastFetchEnd=null,r.lastFetchError=null,h.set({...r});try{i.set(o(await t(...e),i.get())),r.successCounter++}catch(t){r.lastFetchError=t}finally{r.isFetching=!1,r.lastFetchEnd=new Date}return h.set({...r}),h.get().lastFetchError?null:i.get()},f=async(...e)=>{let r=h.get(),n=0;r.lastFetchSilentError&&(h.set({...r,lastFetchSilentError:null}),n++);try{i.set(o(await t(...e),i.get()))}catch(t){r.lastFetchSilentError=t,n++}return n&&h.set({...r}),h.get().lastFetchSilentError?null:i.get()},d=async(t,e=[],r=l)=>{const{successCounter:n,isFetching:s,lastFetchStart:c}=h.get();return Array.isArray(e)||(e=[e]),n||s?r&&!s&&c&&Date.now()-new Date(c).valueOf()>r?t?await f(...e):await E(...e):i.get():t?await f(...e):await E(...e)},b=(t=[],e=500)=>{let r,n=!1;return((t=[],e=500)=>{Array.isArray(t)||(t=[t]);const s=u(e)?e():e;return f(...t).then(()=>{r&&clearTimeout(r),s>0&&!n&&(r=setTimeout(()=>!n&&b(t,e),s))}),()=>{r&&clearTimeout(r),n=!0}})(t,e)};return{subscribe:F,get:g,fetch:E,fetchSilent:f,fetchOnce:async(t=[],e=l)=>await d(!1,t,e),fetchOnceSilent:async(t=[],e=l)=>await d(!0,t,e),fetchRecursive:b,reset:()=>{i.set(o(e)),h.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0,lastFetchSilentError:null}),"function"==typeof c.onReset&&c.onReset()},resetError:()=>h.update(t=>({...t,lastFetchError:null})),getInternalDataStore:()=>i,touch:t=>{t&&i.set(t);let e=h.get();const r=new Date;h.set({...e,lastFetchStart:r,lastFetchEnd:r,successCounter:e.successCounter+1})},fetchWorker:t}},exports.createFetchStreamStore=(t,e=null,r=null,a={})=>{a={...l,...a||{}};const u=(t,e)=>c(r)?r?.(t,e):t,o=n(u(e),a),i=n({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null}),{subscribe:h,get:F}=s([o,i],([t,e])=>({data:t,...e}));return{subscribe:h,get:F,fetchStream:(e=[],r=0)=>{let n,s,l=!1,a=()=>{"function"==typeof s?s():console.warn("`abort` is a noop (the fetchStreamWorker did not return a function)."),n&&clearTimeout(n),l=!0};const h=(e=[],r=0)=>{Array.isArray(e)||(e=[e]);const F=c(r)?r():r;i.update(t=>({...t,isFetching:!0,lastFetchStart:new Date,lastFetchEnd:null,lastFetchError:null}));try{s=t((t,s)=>{i.get().lastFetchError&&i.update(t=>({...t,lastFetchError:null})),"data"===t?o.set(u(s,o.get())):"error"===t?i.update(t=>({...t,lastFetchError:s})):"end"===t&&(i.update(t=>({...t,isFetching:!1,lastFetchEnd:new Date})),F>0&&!l&&(n&&clearTimeout(n),n=setTimeout(()=>{l||(a=h(e,r))},F)))},...e)}catch(t){i.update(e=>({...e,lastFetchError:t}))}return a};return h(e,r)},reset:()=>{o.set(u(e)),i.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null}),"function"==typeof a.onReset&&a.onReset()},resetError:()=>i.update(t=>({...t,lastFetchError:null})),getInternalDataStore:()=>o,fetchStreamWorker:t}};
|
package/dist/index.d.ts
CHANGED
|
@@ -15,11 +15,13 @@ export interface FetchStore<T, V> extends StoreReadable<T> {
|
|
|
15
15
|
fetch: (...args: any[]) => Promise<V | null>;
|
|
16
16
|
fetchSilent: (...args: any[]) => Promise<V | null>;
|
|
17
17
|
fetchOnce: (args?: any[], thresholdMs?: number) => Promise<V | null>;
|
|
18
|
+
fetchOnceSilent: (args?: any[], thresholdMs?: number) => Promise<V | null>;
|
|
18
19
|
fetchRecursive: (args?: any[], delayMs?: number) => () => void;
|
|
19
20
|
reset: () => void;
|
|
20
21
|
resetError: () => void;
|
|
21
22
|
getInternalDataStore: () => StoreLike<V>;
|
|
22
23
|
fetchWorker: (...args: any[]) => Promise<any>;
|
|
24
|
+
touch: (data?: V) => void;
|
|
23
25
|
}
|
|
24
26
|
interface FetchStoreOptions<T> extends CreateStoreOptions<T> {
|
|
25
27
|
fetchOnceDefaultThresholdMs: number;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=t=>"function"==typeof t,e=(e,r="")=>{if(!t(e))throw new TypeError(`${r} Expecting function arg`.trim())},r=e=>t(e.subscribe),n=(r,n=null)=>{const s=e=>t(n
|
|
1
|
+
const t=t=>"function"==typeof t,e=(e,r="")=>{if(!t(e))throw new TypeError(`${r} Expecting function arg`.trim())},r=e=>t(e.subscribe),n=(r,n=null)=>{const s=e=>t(null==n?void 0:n.persist)&&n.persist(e);let c=(()=>{const t=new Map,e=e=>(t.has(e)||t.set(e,new Set),t.get(e)),r=(t,r)=>{if(!t)throw new TypeError("Expecting valid event name.");if("function"!=typeof r)throw new TypeError("Expecting valid callback function.");return e(t).add(r),()=>e(t).delete(r)};return{publish:(t,r)=>{e(t).forEach(t=>t(r))},subscribe:r,subscribeOnce:(t,e)=>{const n=r(t,t=>{e(t),n()});return n},unsubscribe:(t,r)=>{e(t).delete(r)},unsubscribeAll:e=>t.delete(e)}})(),l=r;s(l);const a=()=>l,u=t=>{l!==t&&(l=t,s(l),c.publish("change",l))};return{set:u,get:a,update:t=>{e(t,"[update]"),u(t(a()))},subscribe:t=>(e(t,"[subscribe]"),t(l),c.subscribe("change",t))}},s=(s,c,l=null)=>{const a=e=>t(null==l?void 0:l.persist)&&l.persist(e),u=n(null==l?void 0:l.initialValue),o=[];if(s.forEach(t=>{if(!r(t))throw new TypeError("Expecting array of StoreLike objects");t.subscribe(t=>o.push(t))()}),!t(c))throw new TypeError("Expecting second argument to be the derivative function");if(!c.length||c.length>2)throw new TypeError("Expecting the derivative function to have exactly 1 or 2 arguments");let i=0,h=[];const g=t=>{e(t,"[derived.subscribe]"),i++||s.forEach((t,e)=>{h.push(t.subscribe(t=>{o[e]=t,1===c.length?(u.set(c(o)),a(u.get())):c(o,t=>{u.set(t),a(u.get())})}))});const r=u.subscribe(t);return()=>{--i||(h.forEach(t=>t()),h=[]),r()}};return{get:()=>{let t;return g(e=>t=e)(),t},subscribe:g}},c=t=>"function"==typeof t,l={},a=(t,e=null,r=null,a={})=>{a={...l,...a||{}};const u=(t,e)=>c(r)?r?.(t,e):t,o=n(u(e),a),i=n({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null}),{subscribe:h,get:g}=s([o,i],([t,e])=>({data:t,...e}));return{subscribe:h,get:g,fetchStream:(e=[],r=0)=>{let n,s,l=!1,a=()=>{"function"==typeof s?s():console.warn("`abort` is a noop (the fetchStreamWorker did not return a function)."),n&&clearTimeout(n),l=!0};const h=(e=[],r=0)=>{Array.isArray(e)||(e=[e]);const g=c(r)?r():r;i.update(t=>({...t,isFetching:!0,lastFetchStart:new Date,lastFetchEnd:null,lastFetchError:null}));try{s=t((t,s)=>{i.get().lastFetchError&&i.update(t=>({...t,lastFetchError:null})),"data"===t?o.set(u(s,o.get())):"error"===t?i.update(t=>({...t,lastFetchError:s})):"end"===t&&(i.update(t=>({...t,isFetching:!1,lastFetchEnd:new Date})),g>0&&!l&&(n&&clearTimeout(n),n=setTimeout(()=>{l||(a=h(e,r))},g)))},...e)}catch(t){i.update(e=>({...e,lastFetchError:t}))}return a};return h(e,r)},reset:()=>{o.set(u(e)),i.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null}),"function"==typeof a.onReset&&a.onReset()},resetError:()=>i.update(t=>({...t,lastFetchError:null})),getInternalDataStore:()=>o,fetchStreamWorker:t}},u={fetchOnceDefaultThresholdMs:3e5},o=t=>"function"==typeof t,i=(t,e=null,r=null,c={})=>{const{fetchOnceDefaultThresholdMs:l}={...u,...c||{}},a=(t,e)=>o(r)?r?.(t,e):t,i=n(a(e),c),h=n({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0,lastFetchSilentError:null}),{subscribe:g,get:E}=s([i,h],([t,e])=>({data:t,...e})),F=async(...e)=>{let r=h.get();r.isFetching=!0,r.lastFetchStart=new Date,r.lastFetchEnd=null,r.lastFetchError=null,h.set({...r});try{i.set(a(await t(...e),i.get())),r.successCounter++}catch(t){r.lastFetchError=t}finally{r.isFetching=!1,r.lastFetchEnd=new Date}return h.set({...r}),h.get().lastFetchError?null:i.get()},f=async(...e)=>{let r=h.get(),n=0;r.lastFetchSilentError&&(h.set({...r,lastFetchSilentError:null}),n++);try{i.set(a(await t(...e),i.get()))}catch(t){r.lastFetchSilentError=t,n++}return n&&h.set({...r}),h.get().lastFetchSilentError?null:i.get()},d=async(t,e=[],r=l)=>{const{successCounter:n,isFetching:s,lastFetchStart:c}=h.get();return Array.isArray(e)||(e=[e]),n||s?r&&!s&&c&&Date.now()-new Date(c).valueOf()>r?t?await f(...e):await F(...e):i.get():t?await f(...e):await F(...e)},b=(t=[],e=500)=>{let r,n=!1;return((t=[],e=500)=>{Array.isArray(t)||(t=[t]);const s=o(e)?e():e;return f(...t).then(()=>{r&&clearTimeout(r),s>0&&!n&&(r=setTimeout(()=>!n&&b(t,e),s))}),()=>{r&&clearTimeout(r),n=!0}})(t,e)};return{subscribe:g,get:E,fetch:F,fetchSilent:f,fetchOnce:async(t=[],e=l)=>await d(!1,t,e),fetchOnceSilent:async(t=[],e=l)=>await d(!0,t,e),fetchRecursive:b,reset:()=>{i.set(a(e)),h.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0,lastFetchSilentError:null}),"function"==typeof c.onReset&&c.onReset()},resetError:()=>h.update(t=>({...t,lastFetchError:null})),getInternalDataStore:()=>i,touch:t=>{t&&i.set(t);let e=h.get();const r=new Date;h.set({...e,lastFetchStart:r,lastFetchEnd:r,successCounter:e.successCounter+1})},fetchWorker:t}};export{i as createFetchStore,a as createFetchStreamStore};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["fetch-store"]={})}(this,
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["fetch-store"]={})}(this,function(t){"use strict";const e=t=>"function"==typeof t,r=(t,r="")=>{if(!e(t))throw new TypeError(`${r} Expecting function arg`.trim())},n=t=>e(t.subscribe),s=(t,n=null)=>{const s=t=>e(null==n?void 0:n.persist)&&n.persist(t);let c=(()=>{const t=new Map,e=e=>(t.has(e)||t.set(e,new Set),t.get(e)),r=(t,r)=>{if(!t)throw new TypeError("Expecting valid event name.");if("function"!=typeof r)throw new TypeError("Expecting valid callback function.");return e(t).add(r),()=>e(t).delete(r)};return{publish:(t,r)=>{e(t).forEach(t=>t(r))},subscribe:r,subscribeOnce:(t,e)=>{const n=r(t,t=>{e(t),n()});return n},unsubscribe:(t,r)=>{e(t).delete(r)},unsubscribeAll:e=>t.delete(e)}})(),l=t;s(l);const a=()=>l,o=t=>{l!==t&&(l=t,s(l),c.publish("change",l))};return{set:o,get:a,update:t=>{r(t,"[update]"),o(t(a()))},subscribe:t=>(r(t,"[subscribe]"),t(l),c.subscribe("change",t))}},c=(t,c,l=null)=>{const a=t=>e(null==l?void 0:l.persist)&&l.persist(t),o=s(null==l?void 0:l.initialValue),u=[];if(t.forEach(t=>{if(!n(t))throw new TypeError("Expecting array of StoreLike objects");t.subscribe(t=>u.push(t))()}),!e(c))throw new TypeError("Expecting second argument to be the derivative function");if(!c.length||c.length>2)throw new TypeError("Expecting the derivative function to have exactly 1 or 2 arguments");let i=0,h=[];const f=e=>{r(e,"[derived.subscribe]"),i++||t.forEach((t,e)=>{h.push(t.subscribe(t=>{u[e]=t,1===c.length?(o.set(c(u)),a(o.get())):c(u,t=>{o.set(t),a(o.get())})}))});const n=o.subscribe(e);return()=>{--i||(h.forEach(t=>t()),h=[]),n()}};return{get:()=>{let t;return f(e=>t=e)(),t},subscribe:f}},l=t=>"function"==typeof t,a={},o={fetchOnceDefaultThresholdMs:3e5},u=t=>"function"==typeof t;t.createFetchStore=(t,e=null,r=null,n={})=>{const{fetchOnceDefaultThresholdMs:l}={...o,...n||{}},a=(t,e)=>u(r)?r?.(t,e):t,i=s(a(e),n),h=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0,lastFetchSilentError:null}),{subscribe:f,get:d}=c([i,h],([t,e])=>({data:t,...e})),g=async(...e)=>{let r=h.get();r.isFetching=!0,r.lastFetchStart=new Date,r.lastFetchEnd=null,r.lastFetchError=null,h.set({...r});try{i.set(a(await t(...e),i.get())),r.successCounter++}catch(t){r.lastFetchError=t}finally{r.isFetching=!1,r.lastFetchEnd=new Date}return h.set({...r}),h.get().lastFetchError?null:i.get()},F=async(...e)=>{let r=h.get(),n=0;r.lastFetchSilentError&&(h.set({...r,lastFetchSilentError:null}),n++);try{i.set(a(await t(...e),i.get()))}catch(t){r.lastFetchSilentError=t,n++}return n&&h.set({...r}),h.get().lastFetchSilentError?null:i.get()},E=async(t,e=[],r=l)=>{const{successCounter:n,isFetching:s,lastFetchStart:c}=h.get();return Array.isArray(e)||(e=[e]),n||s?r&&!s&&c&&Date.now()-new Date(c).valueOf()>r?t?await F(...e):await g(...e):i.get():t?await F(...e):await g(...e)},p=(t=[],e=500)=>{let r,n=!1;return((t=[],e=500)=>{Array.isArray(t)||(t=[t]);const s=u(e)?e():e;return F(...t).then(()=>{r&&clearTimeout(r),s>0&&!n&&(r=setTimeout(()=>!n&&p(t,e),s))}),()=>{r&&clearTimeout(r),n=!0}})(t,e)};return{subscribe:f,get:d,fetch:g,fetchSilent:F,fetchOnce:async(t=[],e=l)=>await E(!1,t,e),fetchOnceSilent:async(t=[],e=l)=>await E(!0,t,e),fetchRecursive:p,reset:()=>{i.set(a(e)),h.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0,lastFetchSilentError:null}),"function"==typeof n.onReset&&n.onReset()},resetError:()=>h.update(t=>({...t,lastFetchError:null})),getInternalDataStore:()=>i,touch:t=>{t&&i.set(t);let e=h.get();const r=new Date;h.set({...e,lastFetchStart:r,lastFetchEnd:r,successCounter:e.successCounter+1})},fetchWorker:t}},t.createFetchStreamStore=(t,e=null,r=null,n={})=>{n={...a,...n||{}};const o=(t,e)=>l(r)?r?.(t,e):t,u=s(o(e),n),i=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null}),{subscribe:h,get:f}=c([u,i],([t,e])=>({data:t,...e}));return{subscribe:h,get:f,fetchStream:(e=[],r=0)=>{let n,s,c=!1,a=()=>{"function"==typeof s?s():console.warn("`abort` is a noop (the fetchStreamWorker did not return a function)."),n&&clearTimeout(n),c=!0};const h=(e=[],r=0)=>{Array.isArray(e)||(e=[e]);const f=l(r)?r():r;i.update(t=>({...t,isFetching:!0,lastFetchStart:new Date,lastFetchEnd:null,lastFetchError:null}));try{s=t((t,s)=>{i.get().lastFetchError&&i.update(t=>({...t,lastFetchError:null})),"data"===t?u.set(o(s,u.get())):"error"===t?i.update(t=>({...t,lastFetchError:s})):"end"===t&&(i.update(t=>({...t,isFetching:!1,lastFetchEnd:new Date})),f>0&&!c&&(n&&clearTimeout(n),n=setTimeout(()=>{c||(a=h(e,r))},f)))},...e)}catch(t){i.update(e=>({...e,lastFetchError:t}))}return a};return h(e,r)},reset:()=>{u.set(o(e)),i.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null}),"function"==typeof n.onReset&&n.onReset()},resetError:()=>i.update(t=>({...t,lastFetchError:null})),getInternalDataStore:()=>u,fetchStreamWorker:t}}});
|