@net-vert/core 0.3.4 → 0.3.5

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.d.ts CHANGED
@@ -14,14 +14,14 @@ declare type CacheRequestor<P extends boolean, S extends boolean> = {
14
14
  isValid?: S extends true ? (params: IsValidParams) => boolean : (params: IsValidParams) => boolean | Promise<boolean>;
15
15
  };
16
16
 
17
- declare class ConcurrentPool {
17
+ declare class ConcurrentPool<T extends any = any> {
18
18
  parallelCount: number;
19
- tasks: TaskItemList;
19
+ tasks: TaskItemList<T>;
20
20
  runningCount: number;
21
21
  constructor(parallelCount?: number);
22
- add(id: string, task: Task): Promise<unknown>;
22
+ add(id: string, task: Task<T>): Promise<unknown>;
23
23
  remove(id: string): void;
24
- execute(currentTask: TaskItem): Promise<any>;
24
+ execute(currentTask: TaskItem<T>): Promise<void>;
25
25
  _run(): void;
26
26
  }
27
27
 
@@ -87,7 +87,7 @@ export declare const requestExtender: {
87
87
  createId?: (config: UnifiedConfig) => string;
88
88
  } & RetryOptions) => {
89
89
  requestor: Requestor;
90
- concurrentPool: ConcurrentPool;
90
+ concurrentPool: ConcurrentPool<any>;
91
91
  };
92
92
  syncRequestor: (config?: {
93
93
  persist?: false;
@@ -111,15 +111,15 @@ declare type RetryOptions = {
111
111
  retryCondition?: (error: any) => boolean;
112
112
  };
113
113
 
114
- declare type Task = () => Promise<any>;
114
+ declare type Task<T> = () => Promise<T>;
115
115
 
116
- declare type TaskItem = {
117
- task: Task;
118
- resolve: (value: any) => any;
119
- reject: (value: any) => any;
116
+ declare type TaskItem<T> = {
117
+ task: Task<T>;
118
+ resolve: (value: T) => void;
119
+ reject: (reason: any) => void;
120
120
  };
121
121
 
122
- declare type TaskItemList = TaskQueue<TaskItem>;
122
+ declare type TaskItemList<T> = TaskQueue<TaskItem<T>>;
123
123
 
124
124
  export declare type UnifiedConfig<D = any> = RequestConfig<D> & {
125
125
  url: string;
@@ -1 +1 @@
1
- (function(h,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("localforage"),require("id-queue")):typeof define=="function"&&define.amd?define(["exports","localforage","id-queue"],w):(h=typeof globalThis<"u"?globalThis:h||self,w(h["net-vert/core"]={},h.localforage,h.idQueue))})(this,function(h,w,D){"use strict";const R={get:(t,e)=>({url:t,method:"get",...e,params:e==null?void 0:e.params}),post:(t,e,r)=>({url:t,method:"post",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),delete:(t,e)=>({url:t,method:"delete",...e}),put:(t,e,r)=>({url:t,method:"put",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),request:t=>t};function E(t){const e={};return Object.keys(R).forEach(r=>{e[r]=(...n)=>{const o=R[r](...n);return t(o)}}),{...e,request:t}}const I="default",O=()=>`${Date.now()}_${Math.random().toString().slice(2,8)}`,j=new Map,z=(t,e=I)=>{j.set(e,E(t))},M=(t=I)=>{const e=j.get(t);if(!e)throw new Error(`Requestor实例 ${t} 未注册`);return e};class H{has(e){return!!this.get(e)}get(e){const r=localStorage.getItem(e);if(r)try{return JSON.parse(r)}catch(n){console.error("Error parsing cached data",n);return}}set(e,r){try{const n=JSON.stringify(r);localStorage.setItem(e,n)}catch(n){console.error("Error saving data to localStorage",n)}return r}remove(e){localStorage.removeItem(e)}clear(){localStorage.clear()}}const U=new H,_=({persist:t,name:e,sync:r})=>{if(t&&r)return U;if(t){const n=w.createInstance({name:e});return{has:o=>n.keys().then(s=>s.includes(o)),get:o=>n.getItem(o),set:(o,s)=>n.setItem(o,s),remove:o=>n.removeItem(o),clear:()=>n.clear()}}else{const n=new Map;return{has:o=>n.has(o),get:o=>n.get(o),set:(o,s)=>n.set(o,s),remove:o=>n.delete(o),clear:()=>n.clear()}}},A=()=>{const t=new Map;return{getPromise:s=>t.get(s),setPromise:(s,i)=>{t.set(s,i)},delPromise:s=>{t.delete(s)},clearCache:()=>{t.clear()}}},J=(t,e)=>({value:t,expiresAt:Date.now()+e}),T=t=>t.expiresAt>Date.now(),N={key:t=>t.url,persist:!1,duration:1/0,sync:!1},x=t=>{const e={...N,...t},{name:r,persist:n,sync:o}=e,s=_({persist:n,name:r,sync:o}),{getPromise:i,setPromise:S,delPromise:p}=A();function a(c,d){const{isValid:l}=e,m=s.get(c);let q=!1;if(m&&T(m)){try{q=(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!q&&s.remove(c)}return{shouldUseCache:q,cachedData:m}}async function k(c,d){const{isValid:l}=e,m=await s.get(c);let q=!1;if(m&&T(m)){try{q=await(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!q&&s.remove(c)}return{shouldUseCache:q,cachedData:m}}const g={get(c,d){function l(u,f,...y){const C=Reflect.apply(c[d],c,y).then(async P=>{const v=typeof e.duration=="number"?e.duration:e.duration({key:u,config:f,response:P});return s.set(u,J(P,v)),P}).finally(()=>{p(u)});return S(u,C),C}return o?(...u)=>{const f=R[d](...u),y=e.key(f),C=i(y);if(C)return C;const{shouldUseCache:P,cachedData:v}=a(y,f);return P?v.value:l(y,f,...u)}:async(...u)=>{const f=R[d](...u),y=e.key(f),C=i(y);if(C)return C;const{shouldUseCache:P,cachedData:v}=await k(y,f);return P?v.value:l(y,f,...u)}}};return{requestor:new Proxy(M(),g),store:s}},b=t=>{const{method:e,url:r,params:n,data:o}=t;return[e,r,JSON.stringify(n),JSON.stringify(o)].join("|")},Q=t=>{const{requestor:e}=x({key:r=>t?t(r):b(r),persist:!1});return{requestor:e}},F={retries:3,delay:0,retryCondition:()=>!0},$=t=>{const{retries:e,delay:r,retryCondition:n}={...F,...t},o={get(s,i){return(...p)=>{let a=0;const k=()=>Reflect.apply(s[i],s,p).catch(g=>{if(a<e&&n(g)){a++;const c=typeof r=="function"?r(a):r;return new Promise(d=>{setTimeout(()=>d(k()),c)})}return Promise.reject(g)});return k()}}};return{requestor:new Proxy(M(),o)}};class L{constructor(e=4){this.parallelCount=e,this.tasks=new D.TaskQueue,this.runningCount=0}add(e,r){return new Promise((n,o)=>{this.tasks.enqueue(e,{task:r,resolve:n,reject:o}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:r,resolve:n,reject:o}=e;return r().then(n).catch(o).finally(()=>{this.runningCount--,this._run()})}_run(){for(;this.runningCount<this.parallelCount&&this.tasks.size>0;){const e=this.tasks.dequeue();this.runningCount++,this.execute(e)}}}const W={parallelCount:4,retries:0,createId:()=>O()},Y=t=>{const e={...W,...t},{parallelCount:r,createId:n,...o}=e,s=new L(r),{requestor:i=null}=o.retries>0?$(o):{},S={get(p,a){return(...g)=>{const c=R[a](...g),d=n(c),l=()=>i?Reflect.apply(i[a],i,g):Reflect.apply(p[a],p,g);return s.add(d,l)}}};return{requestor:new Proxy(M(),S),concurrentPool:s}},B={persist:!1,sync:!0},G={cacheRequestor:x,idempotencyRequestor:Q,retryRequestor:$,concurrentPoolRequestor:Y,syncRequestor:t=>{const e={...B,...t},{...r}=e,{requestor:n}=x(r),o={get(s,i){return(...p)=>{try{const a=Reflect.apply(s[i],s,p);if(a instanceof Promise)throw a;return a}catch(a){throw a}}}};return{requestor:new Proxy(n,o)}}};h.inject=z,h.requestExtender=G,h.useRequestor=M,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
1
+ (function(h,w){typeof exports=="object"&&typeof module<"u"?w(exports,require("localforage"),require("id-queue")):typeof define=="function"&&define.amd?define(["exports","localforage","id-queue"],w):(h=typeof globalThis<"u"?globalThis:h||self,w(h.netVertCore={},h.localforage,h.idQueue))})(this,function(h,w,D){"use strict";const R={get:(t,e)=>({url:t,method:"get",...e,params:e==null?void 0:e.params}),post:(t,e,r)=>({url:t,method:"post",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),delete:(t,e)=>({url:t,method:"delete",...e}),put:(t,e,r)=>({url:t,method:"put",data:e,headers:{"Content-Type":"application/json",...r==null?void 0:r.headers},...r}),request:t=>t};function E(t){const e={};return Object.keys(R).forEach(r=>{e[r]=(...n)=>{const o=R[r](...n);return t(o)}}),{...e,request:t}}const I="default",O=()=>`${Date.now()}_${Math.random().toString().slice(2,8)}`,j=new Map,z=(t,e=I)=>{j.set(e,E(t))},M=(t=I)=>{const e=j.get(t);if(!e)throw new Error(`Requestor实例 ${t} 未注册`);return e};class H{has(e){return!!this.get(e)}get(e){const r=localStorage.getItem(e);if(r)try{return JSON.parse(r)}catch(n){console.error("Error parsing cached data",n);return}}set(e,r){try{const n=JSON.stringify(r);localStorage.setItem(e,n)}catch(n){console.error("Error saving data to localStorage",n)}return r}remove(e){localStorage.removeItem(e)}clear(){localStorage.clear()}}const U=new H,_=({persist:t,name:e,sync:r})=>{if(t&&r)return U;if(t){const n=w.createInstance({name:e});return{has:o=>n.keys().then(s=>s.includes(o)),get:o=>n.getItem(o),set:(o,s)=>n.setItem(o,s),remove:o=>n.removeItem(o),clear:()=>n.clear()}}else{const n=new Map;return{has:o=>n.has(o),get:o=>n.get(o),set:(o,s)=>n.set(o,s),remove:o=>n.delete(o),clear:()=>n.clear()}}},A=()=>{const t=new Map;return{getPromise:s=>t.get(s),setPromise:(s,i)=>{t.set(s,i)},delPromise:s=>{t.delete(s)},clearCache:()=>{t.clear()}}},J=(t,e)=>({value:t,expiresAt:Date.now()+e}),T=t=>t.expiresAt>Date.now(),N={key:t=>t.url,persist:!1,duration:1/0,sync:!1},x=t=>{const e={...N,...t},{name:r,persist:n,sync:o}=e,s=_({persist:n,name:r,sync:o}),{getPromise:i,setPromise:S,delPromise:p}=A();function a(c,d){const{isValid:l}=e,m=s.get(c);let C=!1;if(m&&T(m)){try{C=(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!C&&s.remove(c)}return{shouldUseCache:C,cachedData:m}}async function k(c,d){const{isValid:l}=e,m=await s.get(c);let C=!1;if(m&&T(m)){try{C=await(l==null?void 0:l({key:c,config:d,cachedData:m}))??!0}catch(u){console.error(`校验异常 ${c}`,u)}!C&&s.remove(c)}return{shouldUseCache:C,cachedData:m}}const g={get(c,d){function l(u,f,...y){const q=Reflect.apply(c[d],c,y).then(async P=>{const v=typeof e.duration=="number"?e.duration:e.duration({key:u,config:f,response:P});return s.set(u,J(P,v)),P}).finally(()=>{p(u)});return S(u,q),q}return o?(...u)=>{const f=R[d](...u),y=e.key(f),q=i(y);if(q)return q;const{shouldUseCache:P,cachedData:v}=a(y,f);return P?v.value:l(y,f,...u)}:async(...u)=>{const f=R[d](...u),y=e.key(f),q=i(y);if(q)return q;const{shouldUseCache:P,cachedData:v}=await k(y,f);return P?v.value:l(y,f,...u)}}};return{requestor:new Proxy(M(),g),store:s}},b=t=>{const{method:e,url:r,params:n,data:o}=t;return[e,r,JSON.stringify(n),JSON.stringify(o)].join("|")},Q=t=>{const{requestor:e}=x({key:r=>t?t(r):b(r),persist:!1});return{requestor:e}},F={retries:3,delay:0,retryCondition:()=>!0},$=t=>{const{retries:e,delay:r,retryCondition:n}={...F,...t},o={get(s,i){return(...p)=>{let a=0;const k=()=>Reflect.apply(s[i],s,p).catch(g=>{if(a<e&&n(g)){a++;const c=typeof r=="function"?r(a):r;return new Promise(d=>{setTimeout(()=>d(k()),c)})}return Promise.reject(g)});return k()}}};return{requestor:new Proxy(M(),o)}};class L{constructor(e=4){this.parallelCount=e,this.tasks=new D.TaskQueue,this.runningCount=0}add(e,r){return new Promise((n,o)=>{this.tasks.enqueue(e,{task:r,resolve:n,reject:o}),this._run()})}remove(e){this.tasks.remove(e)}execute(e){const{task:r,resolve:n,reject:o}=e;return r().then(n).catch(o).finally(()=>{this.runningCount--,this._run()})}_run(){for(;this.runningCount<this.parallelCount&&this.tasks.size>0;){const e=this.tasks.dequeue();this.runningCount++,this.execute(e)}}}const V={parallelCount:4,retries:0,createId:()=>O()},W=t=>{const e={...V,...t},{parallelCount:r,createId:n,...o}=e,s=new L(r),{requestor:i=null}=o.retries>0?$(o):{},S={get(p,a){return(...g)=>{const c=R[a](...g),d=n(c),l=()=>i?Reflect.apply(i[a],i,g):Reflect.apply(p[a],p,g);return s.add(d,l)}}};return{requestor:new Proxy(M(),S),concurrentPool:s}},Y={persist:!1,sync:!0},B={cacheRequestor:x,idempotencyRequestor:Q,retryRequestor:$,concurrentPoolRequestor:W,syncRequestor:t=>{const e={...Y,...t},{...r}=e,{requestor:n}=x(r),o={get(s,i){return(...p)=>{try{const a=Reflect.apply(s[i],s,p);if(a instanceof Promise)throw a;return a}catch(a){throw a}}}};return{requestor:new Proxy(n,o)}}};h.inject=z,h.requestExtender=B,h.useRequestor=M,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@net-vert/core",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Dependency Inversion Network Library with Type-Safe Injection.",
5
5
  "main": "dist/index",
6
6
  "type": "module",
7
+ "types": "dist/index.d.ts",
7
8
  "scripts": {
8
9
  "test": "vitest",
9
10
  "build": "run-p type-check build-only",
@@ -32,7 +33,7 @@
32
33
  "access": "public"
33
34
  },
34
35
  "dependencies": {
35
- "id-queue": "^1.0.9",
36
+ "id-queue": "^1.0.10",
36
37
  "localforage": "^1.10.0"
37
38
  }
38
39
  }