@marianmeres/fetch-store 1.2.0 → 2.0.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/README.md CHANGED
@@ -4,6 +4,7 @@
4
4
  of any type of async work. Typically used for http requests.
5
5
 
6
6
  ## Install
7
+
7
8
  ```shell
8
9
  $ npm i @marianmeres/fetch-store
9
10
  ```
@@ -12,14 +13,13 @@ $ npm i @marianmeres/fetch-store
12
13
 
13
14
  ```javascript
14
15
  // foo-store.js
15
- const foo = createFetchStore(
16
- async () => {
17
- const r = await fetch('foo.json');
18
- if (!r.ok) throw new Error('Not OK!');
19
- return await r.json()
20
- }
21
- );
16
+ const foo = createFetchStore(async () => {
17
+ const r = await fetch('foo.json');
18
+ if (!r.ok) throw new Error('Not OK!');
19
+ return await r.json();
20
+ });
22
21
  ```
22
+
23
23
  ```sveltehtml
24
24
  <!--FooComponent.svelte-->
25
25
  <script>
@@ -66,17 +66,17 @@ store.subscribe((v) => {
66
66
  // instance api
67
67
 
68
68
  // do the async work
69
- store.fetch: (...args) => Promise<void>;
69
+ store.fetch(...args): Promise<T>;
70
70
 
71
71
  // do the async work, but do not update meta
72
- store.fetchSilent: (...args) => Promise<void>;
72
+ store.fetchSilent(...args): Promise<T>;
73
73
 
74
74
  // do the async work only once (if within threshold since last)
75
- store.fetchOnce: (args: any[], thresholdMs: number) => Promise<void>;
75
+ store.fetchOnce(args: any[], thresholdMs: number): Promise<void>;
76
76
 
77
77
  // to reset internal meta store
78
- store.reset: Function;
79
- store.resetError: Function;
78
+ store.reset();
79
+ store.resetError();
80
80
 
81
81
  // for manual hackings
82
82
  store.getInternalDataStore();
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";const e=e=>"function"==typeof e,t=(t,r="")=>{if(!e(t))throw new TypeError(`${r} Expecting function arg`.trim())},r=t=>e(t.subscribe),s=(r=void 0,s=null)=>{const n=t=>e(s?.persist)&&s.persist(t);let c=(()=>{const e=new Map,t=t=>(e.has(t)||e.set(t,new Set),e.get(t)),r=(e,r)=>{if("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");return t(e).add(r),()=>t(e).delete(r)};return{publish:(e,r)=>{t(e).forEach((e=>e(r)))},subscribe:r,subscribeOnce:(e,t)=>{const s=r(e,(e=>{t(e),s()}));return s},unsubscribeAll:t=>e.delete(t)}})(),a=r;n(a);const l=()=>a,o=e=>{a!==e&&(a=e,n(a),c.publish("change",a))};return{set:o,get:l,update:e=>{t(e,"[update]"),o(e(l()))},subscribe:e=>(t(e,"[subscribe]"),e(a),c.subscribe("change",e))}},n={fetchOnceDefaultThresholdMs:3e5},c=e=>"function"==typeof e;exports.createFetchStore=(a,l=null,o=null,i=null)=>{const{logger:u,onError:h,onSilentError:b,afterCreate:g,fetchOnceDefaultThresholdMs:f}={...n,...i||{}},p=(e,t)=>c(o)?o(e,t):e,d=s(p(l)),E=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0}),{subscribe:w,get:F}=((n,c,a=null)=>{const l=t=>e(a?.persist)&&a.persist(t),o=s(a?.initialValue),i=[];if(n.forEach((e=>{if(!r(e))throw new TypeError("Expecting array of StoreLike objects");e.subscribe((e=>i.push(e)))()})),!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 u=0,h=[];return{get:o.get,subscribe:e=>{t(e,"[derived.subscribe]"),u++||n.forEach(((e,t)=>{h.push(e.subscribe((e=>{i[t]=e,1===c.length?(o.set(c(i)),l(o.get())):c(i,(e=>{o.set(e),l(o.get())}))})))}));const r=o.subscribe(e);return()=>{--u||(h.forEach((e=>e())),h=[]),r()}}}})([d,E],(([e,t])=>({data:e,...t})));w((()=>null));const y=async(...e)=>{let t=d.get(),r=E.get();const s=new Date;let n=null;E.set({...r,isFetching:!0,lastFetchStart:s,lastFetchEnd:null,lastFetchError:n});try{t=p(await a(...e),t),r.successCounter++}catch(e){n=e}return d.set(t),E.set({...r,isFetching:!1,lastFetchStart:s,lastFetchEnd:new Date,lastFetchError:n}),n&&c(h)&&h(n),t},S={subscribe:w,get:F,fetch:y,fetchSilent:async(...e)=>{try{let t=p(await a(...e),d.get());return d.set(t),t}catch(e){((...e)=>{c(u)&&u.apply(null,e)})("silent fetch error",e),c(b)&&b(e)}},fetchOnce:async(e=[],t=f)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=E.get();return Array.isArray(e)||(e=[e]),r||s?t&&!s&&n&&Date.now()-new Date(n).valueOf()>t?await y(...e):void 0:await y(...e)},reset:()=>{d.set(p(l)),E.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0})},resetError:()=>E.update((e=>({...e,lastFetchError:null}))),getInternalDataStore:()=>d};return c(g)&&(console?.warn?.("`afterCreate` option is deprecated and will be removed"),g(S)),S};
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),s=(r,s=null)=>{const n=e=>t(s?.persist)&&s.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 s=r(t,(t=>{e(t),s()}));return s},unsubscribeAll:e=>t.delete(e)}})(),l=r;n(l);const a=()=>l,u=t=>{l!==t&&(l=t,n(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))}},n={fetchOnceDefaultThresholdMs:3e5};exports.createFetchStore=(c,l=null,a=null,u={})=>{const{fetchOnceDefaultThresholdMs:i}={...n,...u||{}},o=(t,e)=>"function"==typeof a?a?.(t,e):t,h=s(o(l),u),g=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,lastFetchSilentError:null,successCounter:0}),{subscribe:b,get:f}=((n,c,l=null)=>{const a=e=>t(l?.persist)&&l.persist(e),u=s(l?.initialValue),i=[];if(n.forEach((t=>{if(!r(t))throw new TypeError("Expecting array of StoreLike objects");t.subscribe((t=>i.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 o=0,h=[];return{get:u.get,subscribe:t=>{e(t,"[derived.subscribe]"),o++||n.forEach(((t,e)=>{h.push(t.subscribe((t=>{i[e]=t,1===c.length?(u.set(c(i)),a(u.get())):c(i,(t=>{u.set(t),a(u.get())}))})))}));const r=u.subscribe(t);return()=>{--o||(h.forEach((t=>t())),h=[]),r()}}}})([h,g],(([t,e])=>({data:t,...e})));b((()=>null));const E=async(...t)=>{let e=g.get();e.isFetching=!0,e.lastFetchStart=new Date,e.lastFetchEnd=null,e.lastFetchError=null,g.set({...e});try{h.set(o(await c(...t),h.get())),e.successCounter++}catch(t){e.lastFetchError=t}finally{e.isFetching=!1,e.lastFetchEnd=new Date}return g.set({...e}),h.get()},p={subscribe:b,get:f,fetch:E,fetchSilent:async(...t)=>{let e=g.get();e.lastFetchSilentError&&g.set({...e,lastFetchSilentError:null});try{h.set(o(await c(...t),h.get()))}catch(t){g.set({...e,lastFetchSilentError:t})}return h.get()},fetchOnce:async(t=[],e=i)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=g.get();return Array.isArray(t)||(t=[t]),r||s?e&&!s&&n&&Date.now()-new Date(n).valueOf()>e?await E(...t):h.get():await E(...t)},reset:()=>{h.set(o(l)),g.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,lastFetchSilentError:null,successCounter:0})},resetError:()=>g.update((t=>({...t,lastFetchError:null}))),getInternalDataStore:()=>h};return p};
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
- import { StoreLike, StoreReadable } from '@marianmeres/store';
1
+ import { CreateStoreOptions, StoreLike, StoreReadable } from '@marianmeres/store';
2
2
  export interface FetchStoreMeta {
3
3
  isFetching: boolean;
4
- lastFetchStart: Date;
5
- lastFetchEnd: Date;
6
- lastFetchError: Date;
4
+ lastFetchStart: Date | null;
5
+ lastFetchEnd: Date | null;
6
+ lastFetchError: Error | null;
7
+ lastFetchSilentError: Error | null;
7
8
  successCounter: number;
8
9
  }
9
10
  export interface FetchStoreValue<T> extends FetchStoreMeta {
@@ -13,17 +14,13 @@ export interface FetchStore<T, V> extends StoreReadable<T> {
13
14
  fetch: (...args: any[]) => Promise<V>;
14
15
  fetchSilent: (...args: any[]) => Promise<V>;
15
16
  fetchOnce: (args: any[], thresholdMs: number) => Promise<V>;
16
- reset: Function;
17
- resetError: Function;
17
+ reset: () => void;
18
+ resetError: () => void;
18
19
  getInternalDataStore: () => StoreLike<V>;
19
20
  }
20
- interface FetchStoreOptions {
21
- logger: (...args: any[]) => void;
21
+ interface FetchStoreOptions<T> extends CreateStoreOptions<T> {
22
22
  fetchOnceDefaultThresholdMs: number;
23
- onError: (e: any) => void;
24
- onSilentError: (e: any) => void;
25
- afterCreate: (fetchStoreInstance: any) => void;
26
23
  }
27
24
  type DataFactory<T> = (raw: any, old?: any) => T;
28
- export declare const createFetchStore: <T>(fetchWorker: (...args: any[]) => Promise<any>, initial?: T, dataFactory?: DataFactory<T>, options?: Partial<FetchStoreOptions>) => FetchStore<FetchStoreValue<T>, T>;
25
+ 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>;
29
26
  export {};
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- const e=e=>"function"==typeof e,t=(t,r="")=>{if(!e(t))throw new TypeError(`${r} Expecting function arg`.trim())},r=t=>e(t.subscribe),s=(r=void 0,s=null)=>{const n=t=>e(s?.persist)&&s.persist(t);let c=(()=>{const e=new Map,t=t=>(e.has(t)||e.set(t,new Set),e.get(t)),r=(e,r)=>{if("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");return t(e).add(r),()=>t(e).delete(r)};return{publish:(e,r)=>{t(e).forEach((e=>e(r)))},subscribe:r,subscribeOnce:(e,t)=>{const s=r(e,(e=>{t(e),s()}));return s},unsubscribeAll:t=>e.delete(t)}})(),a=r;n(a);const l=()=>a,o=e=>{a!==e&&(a=e,n(a),c.publish("change",a))};return{set:o,get:l,update:e=>{t(e,"[update]"),o(e(l()))},subscribe:e=>(t(e,"[subscribe]"),e(a),c.subscribe("change",e))}},n={fetchOnceDefaultThresholdMs:3e5},c=e=>"function"==typeof e,a=(a,l=null,o=null,i=null)=>{const{logger:u,onError:h,onSilentError:b,afterCreate:g,fetchOnceDefaultThresholdMs:f}={...n,...i||{}},p=(e,t)=>c(o)?o(e,t):e,d=s(p(l)),E=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0}),{subscribe:w,get:F}=((n,c,a=null)=>{const l=t=>e(a?.persist)&&a.persist(t),o=s(a?.initialValue),i=[];if(n.forEach((e=>{if(!r(e))throw new TypeError("Expecting array of StoreLike objects");e.subscribe((e=>i.push(e)))()})),!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 u=0,h=[];return{get:o.get,subscribe:e=>{t(e,"[derived.subscribe]"),u++||n.forEach(((e,t)=>{h.push(e.subscribe((e=>{i[t]=e,1===c.length?(o.set(c(i)),l(o.get())):c(i,(e=>{o.set(e),l(o.get())}))})))}));const r=o.subscribe(e);return()=>{--u||(h.forEach((e=>e())),h=[]),r()}}}})([d,E],(([e,t])=>({data:e,...t})));w((()=>null));const y=async(...e)=>{let t=d.get(),r=E.get();const s=new Date;let n=null;E.set({...r,isFetching:!0,lastFetchStart:s,lastFetchEnd:null,lastFetchError:n});try{t=p(await a(...e),t),r.successCounter++}catch(e){n=e}return d.set(t),E.set({...r,isFetching:!1,lastFetchStart:s,lastFetchEnd:new Date,lastFetchError:n}),n&&c(h)&&h(n),t},v={subscribe:w,get:F,fetch:y,fetchSilent:async(...e)=>{try{let t=p(await a(...e),d.get());return d.set(t),t}catch(e){((...e)=>{c(u)&&u.apply(null,e)})("silent fetch error",e),c(b)&&b(e)}},fetchOnce:async(e=[],t=f)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=E.get();return Array.isArray(e)||(e=[e]),r||s?t&&!s&&n&&Date.now()-new Date(n).valueOf()>t?await y(...e):void 0:await y(...e)},reset:()=>{d.set(p(l)),E.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0})},resetError:()=>E.update((e=>({...e,lastFetchError:null}))),getInternalDataStore:()=>d};return c(g)&&(console?.warn?.("`afterCreate` option is deprecated and will be removed"),g(v)),v};export{a as createFetchStore};
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),s=(r,s=null)=>{const n=e=>t(s?.persist)&&s.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 s=r(t,(t=>{e(t),s()}));return s},unsubscribeAll:e=>t.delete(e)}})(),l=r;n(l);const a=()=>l,u=t=>{l!==t&&(l=t,n(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))}},n={fetchOnceDefaultThresholdMs:3e5},c=(c,l=null,a=null,u={})=>{const{fetchOnceDefaultThresholdMs:i}={...n,...u||{}},o=(t,e)=>"function"==typeof a?a?.(t,e):t,h=s(o(l),u),g=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,lastFetchSilentError:null,successCounter:0}),{subscribe:b,get:f}=((n,c,l=null)=>{const a=e=>t(l?.persist)&&l.persist(e),u=s(l?.initialValue),i=[];if(n.forEach((t=>{if(!r(t))throw new TypeError("Expecting array of StoreLike objects");t.subscribe((t=>i.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 o=0,h=[];return{get:u.get,subscribe:t=>{e(t,"[derived.subscribe]"),o++||n.forEach(((t,e)=>{h.push(t.subscribe((t=>{i[e]=t,1===c.length?(u.set(c(i)),a(u.get())):c(i,(t=>{u.set(t),a(u.get())}))})))}));const r=u.subscribe(t);return()=>{--o||(h.forEach((t=>t())),h=[]),r()}}}})([h,g],(([t,e])=>({data:t,...e})));b((()=>null));const E=async(...t)=>{let e=g.get();e.isFetching=!0,e.lastFetchStart=new Date,e.lastFetchEnd=null,e.lastFetchError=null,g.set({...e});try{h.set(o(await c(...t),h.get())),e.successCounter++}catch(t){e.lastFetchError=t}finally{e.isFetching=!1,e.lastFetchEnd=new Date}return g.set({...e}),h.get()},p={subscribe:b,get:f,fetch:E,fetchSilent:async(...t)=>{let e=g.get();e.lastFetchSilentError&&g.set({...e,lastFetchSilentError:null});try{h.set(o(await c(...t),h.get()))}catch(t){g.set({...e,lastFetchSilentError:t})}return h.get()},fetchOnce:async(t=[],e=i)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=g.get();return Array.isArray(t)||(t=[t]),r||s?e&&!s&&n&&Date.now()-new Date(n).valueOf()>e?await E(...t):h.get():await E(...t)},reset:()=>{h.set(o(l)),g.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,lastFetchSilentError:null,successCounter:0})},resetError:()=>g.update((t=>({...t,lastFetchError:null}))),getInternalDataStore:()=>h};return p};export{c as createFetchStore};
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["fetch-store"]={})}(this,(function(e){"use strict";const t=e=>"function"==typeof e,r=(e,r="")=>{if(!t(e))throw new TypeError(`${r} Expecting function arg`.trim())},n=e=>t(e.subscribe),s=(e=void 0,n=null)=>{const s=e=>t(n?.persist)&&n.persist(e);let c=(()=>{const e=new Map,t=t=>(e.has(t)||e.set(t,new Set),e.get(t)),r=(e,r)=>{if("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");return t(e).add(r),()=>t(e).delete(r)};return{publish:(e,r)=>{t(e).forEach((e=>e(r)))},subscribe:r,subscribeOnce:(e,t)=>{const n=r(e,(e=>{t(e),n()}));return n},unsubscribeAll:t=>e.delete(t)}})(),o=e;s(o);const a=()=>o,l=e=>{o!==e&&(o=e,s(o),c.publish("change",o))};return{set:l,get:a,update:e=>{r(e,"[update]"),l(e(a()))},subscribe:e=>(r(e,"[subscribe]"),e(o),c.subscribe("change",e))}},c={fetchOnceDefaultThresholdMs:3e5},o=e=>"function"==typeof e;e.createFetchStore=(e,a=null,l=null,i=null)=>{const{logger:u,onError:h,onSilentError:f,afterCreate:b,fetchOnceDefaultThresholdMs:g}={...c,...i||{}},p=(e,t)=>o(l)?l(e,t):e,d=s(p(a)),E=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0}),{subscribe:w,get:y}=((e,c,o=null)=>{const a=e=>t(o?.persist)&&o.persist(e),l=s(o?.initialValue),i=[];if(e.forEach((e=>{if(!n(e))throw new TypeError("Expecting array of StoreLike objects");e.subscribe((e=>i.push(e)))()})),!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 u=0,h=[];return{get:l.get,subscribe:t=>{r(t,"[derived.subscribe]"),u++||e.forEach(((e,t)=>{h.push(e.subscribe((e=>{i[t]=e,1===c.length?(l.set(c(i)),a(l.get())):c(i,(e=>{l.set(e),a(l.get())}))})))}));const n=l.subscribe(t);return()=>{--u||(h.forEach((e=>e())),h=[]),n()}}}})([d,E],(([e,t])=>({data:e,...t})));w((()=>null));const F=async(...t)=>{let r=d.get(),n=E.get();const s=new Date;let c=null;E.set({...n,isFetching:!0,lastFetchStart:s,lastFetchEnd:null,lastFetchError:c});try{r=p(await e(...t),r),n.successCounter++}catch(e){c=e}return d.set(r),E.set({...n,isFetching:!1,lastFetchStart:s,lastFetchEnd:new Date,lastFetchError:c}),c&&o(h)&&h(c),r},S={subscribe:w,get:y,fetch:F,fetchSilent:async(...t)=>{try{let r=p(await e(...t),d.get());return d.set(r),r}catch(e){((...e)=>{o(u)&&u.apply(null,e)})("silent fetch error",e),o(f)&&f(e)}},fetchOnce:async(e=[],t=g)=>{const{successCounter:r,isFetching:n,lastFetchStart:s}=E.get();return Array.isArray(e)||(e=[e]),r||n?t&&!n&&s&&Date.now()-new Date(s).valueOf()>t?await F(...e):void 0:await F(...e)},reset:()=>{d.set(p(a)),E.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0})},resetError:()=>E.update((e=>({...e,lastFetchError:null}))),getInternalDataStore:()=>d};return o(b)&&(console?.warn?.("`afterCreate` option is deprecated and will be removed"),b(S)),S}}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["fetch-store"]={})}(this,(function(e){"use strict";const t=e=>"function"==typeof e,r=(e,r="")=>{if(!t(e))throw new TypeError(`${r} Expecting function arg`.trim())},s=e=>t(e.subscribe),n=(e,s=null)=>{const n=e=>t(s?.persist)&&s.persist(e);let c=(()=>{const e=new Map,t=t=>(e.has(t)||e.set(t,new Set),e.get(t)),r=(e,r)=>{if("function"!=typeof r)throw new TypeError("Expecting callback function as second argument");return t(e).add(r),()=>t(e).delete(r)};return{publish:(e,r)=>{t(e).forEach((e=>e(r)))},subscribe:r,subscribeOnce:(e,t)=>{const s=r(e,(e=>{t(e),s()}));return s},unsubscribeAll:t=>e.delete(t)}})(),l=e;n(l);const a=()=>l,i=e=>{l!==e&&(l=e,n(l),c.publish("change",l))};return{set:i,get:a,update:e=>{r(e,"[update]"),i(e(a()))},subscribe:e=>(r(e,"[subscribe]"),e(l),c.subscribe("change",e))}},c={fetchOnceDefaultThresholdMs:3e5};e.createFetchStore=(e,l=null,a=null,i={})=>{const{fetchOnceDefaultThresholdMs:u}={...c,...i||{}},o=(e,t)=>"function"==typeof a?a?.(e,t):e,h=n(o(l),i),f=n({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,lastFetchSilentError:null,successCounter:0}),{subscribe:g,get:b}=((e,c,l=null)=>{const a=e=>t(l?.persist)&&l.persist(e),i=n(l?.initialValue),u=[];if(e.forEach((e=>{if(!s(e))throw new TypeError("Expecting array of StoreLike objects");e.subscribe((e=>u.push(e)))()})),!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 o=0,h=[];return{get:i.get,subscribe:t=>{r(t,"[derived.subscribe]"),o++||e.forEach(((e,t)=>{h.push(e.subscribe((e=>{u[t]=e,1===c.length?(i.set(c(u)),a(i.get())):c(u,(e=>{i.set(e),a(i.get())}))})))}));const s=i.subscribe(t);return()=>{--o||(h.forEach((e=>e())),h=[]),s()}}}})([h,f],(([e,t])=>({data:e,...t})));g((()=>null));const p=async(...t)=>{let r=f.get();r.isFetching=!0,r.lastFetchStart=new Date,r.lastFetchEnd=null,r.lastFetchError=null,f.set({...r});try{h.set(o(await e(...t),h.get())),r.successCounter++}catch(e){r.lastFetchError=e}finally{r.isFetching=!1,r.lastFetchEnd=new Date}return f.set({...r}),h.get()},d={subscribe:g,get:b,fetch:p,fetchSilent:async(...t)=>{let r=f.get();r.lastFetchSilentError&&f.set({...r,lastFetchSilentError:null});try{h.set(o(await e(...t),h.get()))}catch(e){f.set({...r,lastFetchSilentError:e})}return h.get()},fetchOnce:async(e=[],t=u)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=f.get();return Array.isArray(e)||(e=[e]),r||s?t&&!s&&n&&Date.now()-new Date(n).valueOf()>t?await p(...e):h.get():await p(...e)},reset:()=>{h.set(o(l)),f.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,lastFetchSilentError:null,successCounter:0})},resetError:()=>f.update((e=>({...e,lastFetchError:null}))),getInternalDataStore:()=>h};return d}}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/fetch-store",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",