@marianmeres/fetch-store 2.8.0 → 2.9.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/fetch-stream-store.d.ts +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ export interface FetchStreamStoreMeta {
|
|
|
7
7
|
lastFetchError: Error | null;
|
|
8
8
|
}
|
|
9
9
|
interface FetchStreamStoreOptions<T> extends CreateStoreOptions<T> {
|
|
10
|
+
onReset: () => void;
|
|
10
11
|
}
|
|
11
12
|
type FetchStreamEventName = 'data' | 'error' | 'end';
|
|
12
13
|
type FetchStreamEventEmitFn = (eventName: FetchStreamEventName, eventData: any) => () => void;
|
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?.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("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");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},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(l?.persist)&&l.persist(e),u=n(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=[];return{get:u.get,subscribe: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()}}}},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:g,get:F}=s([i,h],(([t,e])=>({data:t,...e})));g((()=>null));const 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()},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,
|
|
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?.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("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");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},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(l?.persist)&&l.persist(e),u=n(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=[];return{get:u.get,subscribe: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()}}}},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:g,get:F}=s([i,h],(([t,e])=>({data:t,...e})));g((()=>null));const 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()},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:g,get:F,fetch:E,fetchSilent:f,fetchOnce:async(t=[],e=l)=>{const{successCounter:r,isFetching:n,lastFetchStart:s}=h.get();return Array.isArray(t)||(t=[t]),r||n?e&&!n&&s&&Date.now()-new Date(s).valueOf()>e?await E(...t):i.get():await E(...t)},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,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:g}=s([o,i],(([t,e])=>({data:t,...e})));h((()=>null));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}};
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export interface FetchStore<T, V> extends StoreReadable<T> {
|
|
|
23
23
|
}
|
|
24
24
|
interface FetchStoreOptions<T> extends CreateStoreOptions<T> {
|
|
25
25
|
fetchOnceDefaultThresholdMs: number;
|
|
26
|
+
onReset: () => void;
|
|
26
27
|
}
|
|
27
28
|
export type DataFactory<T> = (raw: any, old?: any) => T;
|
|
28
29
|
export declare const createFetchStore: <T>(fetchWorker: (...args: any[]) => Promise<any>, initial?: T | null, dataFactory?: DataFactory<T> | null, options?: Partial<FetchStoreOptions<T>>) => FetchStore<FetchStoreValue<T>, T>;
|
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?.persist)&&n.persist(e);let
|
|
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?.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("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");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},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(l?.persist)&&l.persist(e),u=n(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=[];return{get:u.get,subscribe: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()}}}},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})));h((()=>null));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})));g((()=>null));const 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()},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)=>{const{successCounter:r,isFetching:n,lastFetchStart:s}=h.get();return Array.isArray(t)||(t=[t]),r||n?e&&!n&&s&&Date.now()-new Date(s).valueOf()>e?await F(...t):i.get():await F(...t)},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,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,(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(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("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");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},unsubscribeAll:e=>t.delete(e)}})(),l=t;s(l);const a=()=>l,
|
|
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(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("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");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},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(l?.persist)&&l.persist(t),o=s(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=[];return{get:o.get,subscribe: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()}}}},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:g}=c([i,h],(([t,e])=>({data:t,...e})));f((()=>null));const 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()},d=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=(t=[],e=500)=>{let r,n=!1;return((t=[],e=500)=>{Array.isArray(t)||(t=[t]);const s=u(e)?e():e;return d(...t).then((()=>{r&&clearTimeout(r),s>0&&!n&&(r=setTimeout((()=>!n&&E(t,e)),s))})),()=>{r&&clearTimeout(r),n=!0}})(t,e)};return{subscribe:f,get:g,fetch:F,fetchSilent:d,fetchOnce:async(t=[],e=l)=>{const{successCounter:r,isFetching:n,lastFetchStart:s}=h.get();return Array.isArray(t)||(t=[t]),r||n?e&&!n&&s&&Date.now()-new Date(s).valueOf()>e?await F(...t):i.get():await F(...t)},fetchRecursive:E,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,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})));h((()=>null));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}}}));
|