@marianmeres/fetch-store 1.1.2 → 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 +12 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +9 -12
- package/dist/index.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
69
|
+
store.fetch(...args): Promise<T>;
|
|
70
70
|
|
|
71
71
|
// do the async work, but do not update meta
|
|
72
|
-
store.fetchSilent
|
|
72
|
+
store.fetchSilent(...args): Promise<T>;
|
|
73
73
|
|
|
74
74
|
// do the async work only once (if within threshold since last)
|
|
75
|
-
store.fetchOnce
|
|
75
|
+
store.fetchOnce(args: any[], thresholdMs: number): Promise<void>;
|
|
76
76
|
|
|
77
77
|
// to reset internal meta store
|
|
78
|
-
store.reset
|
|
79
|
-
store.resetError
|
|
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
|
|
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:
|
|
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:
|
|
17
|
-
resetError:
|
|
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
|
|
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
|
|
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())},
|
|
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": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -56,6 +56,6 @@
|
|
|
56
56
|
"typescript": "^4.9.5"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"@marianmeres/store": "^1.
|
|
59
|
+
"@marianmeres/store": "^1.3.0"
|
|
60
60
|
}
|
|
61
61
|
}
|