@marianmeres/fetch-store 1.0.2 → 1.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/README.md +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.js +1 -1
- package/dist/index.umd.js +1 -1
- package/package.json +59 -58
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ store.fetch: (...args) => Promise<void>;
|
|
|
71
71
|
// do the async work, but do not update meta
|
|
72
72
|
store.fetchSilent: (...args) => Promise<void>;
|
|
73
73
|
|
|
74
|
-
//
|
|
74
|
+
// do the async work only once (if within threshold since last)
|
|
75
75
|
store.fetchOnce: (args: any[], thresholdMs: number) => Promise<void>;
|
|
76
76
|
|
|
77
77
|
// to reset internal meta store
|
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=
|
|
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,u=e=>{a!==e&&(a=e,n(a),c.publish("change",a))};return{set:u,get:l,update:e=>{t(e,"[update]"),u(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,u=null,i=null)=>{const{logger:o,onError:h,onSilentError:g,afterCreate:b,fetchOnceDefaultThresholdMs:f}={...n,...i||{}},p=(e,t)=>c(u)?u(e,t):e,E=s(p(l)),d=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),u=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 o=0,h=[];return{get:u.get,subscribe:e=>{t(e,"[derived.subscribe]"),o++||n.forEach(((e,t)=>{h.push(e.subscribe((e=>{i[t]=e,1===c.length?(u.set(c(i)),l(u.get())):c(i,(e=>{u.set(e),l(u.get())}))})))}));const r=u.subscribe(e);return()=>{--o||(h.forEach((e=>e())),h=[]),r()}}}})([E,d],(([e,t])=>({data:e,...t})));w((()=>null));const y=async(...e)=>{let t=E.get(),r=d.get();const s=new Date;let n=null;d.set({...r,isFetching:!0,lastFetchStart:s,lastFetchEnd:null,lastFetchError:n});try{t=p(await a(...e),t),r.successCounter++}catch(e){n=e}return E.set(t),d.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),E.get());return E.set(t),t}catch(e){((...e)=>{c(o)&&o.apply(null,e)})("silent fetch error",e),c(g)&&g(e)}},fetchOnce:async(e=[],t=f)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=d.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:()=>{E.set(p(l)),d.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0})},resetError:()=>d.update((e=>({...e,lastFetchError:null}))),getInternalDataStore:()=>E};return c(b)&&b(S),S};
|
package/dist/index.d.ts
CHANGED
|
@@ -10,9 +10,9 @@ export interface FetchStoreValue<T> extends FetchStoreMeta {
|
|
|
10
10
|
data: T;
|
|
11
11
|
}
|
|
12
12
|
export interface FetchStore<T, V> extends StoreReadable<T> {
|
|
13
|
-
fetch: (...args: any[]) => Promise<
|
|
14
|
-
fetchSilent: (...args: any[]) => Promise<
|
|
15
|
-
fetchOnce: (args: any[], thresholdMs: number) => Promise<
|
|
13
|
+
fetch: (...args: any[]) => Promise<V>;
|
|
14
|
+
fetchSilent: (...args: any[]) => Promise<V>;
|
|
15
|
+
fetchOnce: (args: any[], thresholdMs: number) => Promise<V>;
|
|
16
16
|
reset: Function;
|
|
17
17
|
resetError: Function;
|
|
18
18
|
getInternalDataStore: () => StoreLike<V>;
|
|
@@ -20,9 +20,10 @@ export interface FetchStore<T, V> extends StoreReadable<T> {
|
|
|
20
20
|
interface FetchStoreOptions {
|
|
21
21
|
logger: (...args: any[]) => void;
|
|
22
22
|
afterCreate: (fetchStoreInstance: any) => void;
|
|
23
|
-
|
|
23
|
+
fetchOnceDefaultThresholdMs: number;
|
|
24
24
|
onError: (e: any) => void;
|
|
25
25
|
onSilentError: (e: any) => void;
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
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>;
|
|
28
29
|
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=
|
|
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,u=e=>{a!==e&&(a=e,n(a),c.publish("change",a))};return{set:u,get:l,update:e=>{t(e,"[update]"),u(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,u=null,i=null)=>{const{logger:o,onError:h,onSilentError:g,afterCreate:b,fetchOnceDefaultThresholdMs:f}={...n,...i||{}},p=(e,t)=>c(u)?u(e,t):e,E=s(p(l)),d=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),u=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 o=0,h=[];return{get:u.get,subscribe:e=>{t(e,"[derived.subscribe]"),o++||n.forEach(((e,t)=>{h.push(e.subscribe((e=>{i[t]=e,1===c.length?(u.set(c(i)),l(u.get())):c(i,(e=>{u.set(e),l(u.get())}))})))}));const r=u.subscribe(e);return()=>{--o||(h.forEach((e=>e())),h=[]),r()}}}})([E,d],(([e,t])=>({data:e,...t})));w((()=>null));const y=async(...e)=>{let t=E.get(),r=d.get();const s=new Date;let n=null;d.set({...r,isFetching:!0,lastFetchStart:s,lastFetchEnd:null,lastFetchError:n});try{t=p(await a(...e),t),r.successCounter++}catch(e){n=e}return E.set(t),d.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),E.get());return E.set(t),t}catch(e){((...e)=>{c(o)&&o.apply(null,e)})("silent fetch error",e),c(g)&&g(e)}},fetchOnce:async(e=[],t=f)=>{const{successCounter:r,isFetching:s,lastFetchStart:n}=d.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:()=>{E.set(p(l)),d.set({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0})},resetError:()=>d.update((e=>({...e,lastFetchError:null}))),getInternalDataStore:()=>E};return c(b)&&b(S),S};export{a 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).ticker={})}(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=
|
|
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).ticker={})}(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)}})(),l=e;s(l);const a=()=>l,i=e=>{l!==e&&(l=e,s(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},l=e=>"function"==typeof e;e.createFetchStore=(e,a=null,i=null,o=null)=>{const{logger:u,onError:h,onSilentError:f,afterCreate:b,fetchOnceDefaultThresholdMs:g}={...c,...o||{}},p=(e,t)=>l(i)?i(e,t):e,d=s(p(a)),E=s({isFetching:!1,lastFetchStart:null,lastFetchEnd:null,lastFetchError:null,successCounter:0}),{subscribe:y,get:w}=((e,c,l=null)=>{const a=e=>t(l?.persist)&&l.persist(e),i=s(l?.initialValue),o=[];if(e.forEach((e=>{if(!n(e))throw new TypeError("Expecting array of StoreLike objects");e.subscribe((e=>o.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:i.get,subscribe:t=>{r(t,"[derived.subscribe]"),u++||e.forEach(((e,t)=>{h.push(e.subscribe((e=>{o[t]=e,1===c.length?(i.set(c(o)),a(i.get())):c(o,(e=>{i.set(e),a(i.get())}))})))}));const n=i.subscribe(t);return()=>{--u||(h.forEach((e=>e())),h=[]),n()}}}})([d,E],(([e,t])=>({data:e,...t})));y((()=>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&&l(h)&&h(c),r},S={subscribe:y,get:w,fetch:F,fetchSilent:async(...t)=>{try{let r=p(await e(...t),d.get());return d.set(r),r}catch(e){((...e)=>{l(u)&&u.apply(null,e)})("silent fetch error",e),l(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 l(b)&&b(S),S}}));
|
package/package.json
CHANGED
|
@@ -1,60 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
2
|
+
"name": "@marianmeres/fetch-store",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"browser": "./dist/index.umd.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
"require": "./dist/index.cjs",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"scripts": {
|
|
18
|
+
"clean": "rimraf dist/*",
|
|
19
|
+
"prettier": "prettier --write \"{src,tests}/**/*.{js,ts,json}\"",
|
|
20
|
+
"release": "release -v minor",
|
|
21
|
+
"release:patch": "release -v patch",
|
|
22
|
+
"test": "node --loader ts-node/esm tests/index.ts",
|
|
23
|
+
"test:watch": "nodemon --delay 0.5 -q -w \"src/**/*.*\" -w \"tests/**/*.*\" -x \"node --loader ts-node/esm tests/index.ts\" --",
|
|
24
|
+
"build": "npm run clean && rollup -c",
|
|
25
|
+
"dev": "npm run clean && rollup -c -w"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/marianmeres/fetch-store.git"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"interval"
|
|
33
|
+
],
|
|
34
|
+
"author": "Marian Meres <marian@meres.sk>",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/marianmeres/ticker/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/marianmeres/ticker#readme",
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@marianmeres/clog": "^1.0.0",
|
|
42
|
+
"@marianmeres/release": "^1.0.0",
|
|
43
|
+
"@marianmeres/test-runner": "^2.0.15",
|
|
44
|
+
"@rollup/plugin-commonjs": "^24.0.1",
|
|
45
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
46
|
+
"@rollup/plugin-terser": "^0.4.0",
|
|
47
|
+
"@rollup/plugin-typescript": "^11.0.0",
|
|
48
|
+
"@types/node": "^18.15.1",
|
|
49
|
+
"mkdirp": "^2.1.5",
|
|
50
|
+
"nodemon": "^3.0.2",
|
|
51
|
+
"prettier": "^2.8.4",
|
|
52
|
+
"rimraf": "^4.4.0",
|
|
53
|
+
"rollup": "^3.19.1",
|
|
54
|
+
"ts-node": "^10.9.1",
|
|
55
|
+
"tslib": "^2.5.0",
|
|
56
|
+
"typescript": "^4.9.5"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@marianmeres/store": "^1.2.0"
|
|
60
|
+
}
|
|
60
61
|
}
|