@pixel-pulse/next-cache-brain-core 0.1.0 → 0.3.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/index.d.mts +26 -3
- package/dist/index.d.ts +26 -3
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
interface CacheEntry<T> {
|
|
2
|
+
key: string;
|
|
3
|
+
data: T;
|
|
4
|
+
expiry: number;
|
|
5
|
+
ttl: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type Listener = (data: any[]) => void;
|
|
9
|
+
declare const cacheEngine: {
|
|
10
|
+
subscribe(fn: Listener): () => boolean;
|
|
11
|
+
getValues(): CacheEntry<any>[];
|
|
12
|
+
notify(data?: any[]): void;
|
|
13
|
+
};
|
|
14
|
+
|
|
1
15
|
type Strategy = "cache-first" | "network-first" | "swr" | "no-cache";
|
|
2
16
|
type SmartFetchOptions = {
|
|
3
17
|
strategy?: Strategy;
|
|
@@ -5,10 +19,20 @@ type SmartFetchOptions = {
|
|
|
5
19
|
dedupe?: boolean;
|
|
6
20
|
};
|
|
7
21
|
|
|
8
|
-
|
|
22
|
+
/**
|
|
23
|
+
* smartFetch is the main entry point for the library.
|
|
24
|
+
* It handles deduplication, strategy selection, and DevTools notification.
|
|
25
|
+
*/
|
|
26
|
+
declare function smartFetch<T = any>(url: string, options?: SmartFetchOptions): Promise<T>;
|
|
27
|
+
|
|
28
|
+
declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
|
|
9
29
|
|
|
10
30
|
declare function clearCache(url?: string): void;
|
|
11
31
|
|
|
32
|
+
/**
|
|
33
|
+
* GLOBAL TYPE DEFINITION
|
|
34
|
+
* Informs TypeScript about our DevTools bridge on the window object.
|
|
35
|
+
*/
|
|
12
36
|
declare global {
|
|
13
37
|
interface Window {
|
|
14
38
|
__NCB_DEVTOOLS__?: {
|
|
@@ -17,6 +41,5 @@ declare global {
|
|
|
17
41
|
};
|
|
18
42
|
}
|
|
19
43
|
}
|
|
20
|
-
declare const emitUpdate: () => void;
|
|
21
44
|
|
|
22
|
-
export { type
|
|
45
|
+
export { type CacheEntry, cacheEngine, clearCache, setCache, smartFetch };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
interface CacheEntry<T> {
|
|
2
|
+
key: string;
|
|
3
|
+
data: T;
|
|
4
|
+
expiry: number;
|
|
5
|
+
ttl: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type Listener = (data: any[]) => void;
|
|
9
|
+
declare const cacheEngine: {
|
|
10
|
+
subscribe(fn: Listener): () => boolean;
|
|
11
|
+
getValues(): CacheEntry<any>[];
|
|
12
|
+
notify(data?: any[]): void;
|
|
13
|
+
};
|
|
14
|
+
|
|
1
15
|
type Strategy = "cache-first" | "network-first" | "swr" | "no-cache";
|
|
2
16
|
type SmartFetchOptions = {
|
|
3
17
|
strategy?: Strategy;
|
|
@@ -5,10 +19,20 @@ type SmartFetchOptions = {
|
|
|
5
19
|
dedupe?: boolean;
|
|
6
20
|
};
|
|
7
21
|
|
|
8
|
-
|
|
22
|
+
/**
|
|
23
|
+
* smartFetch is the main entry point for the library.
|
|
24
|
+
* It handles deduplication, strategy selection, and DevTools notification.
|
|
25
|
+
*/
|
|
26
|
+
declare function smartFetch<T = any>(url: string, options?: SmartFetchOptions): Promise<T>;
|
|
27
|
+
|
|
28
|
+
declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
|
|
9
29
|
|
|
10
30
|
declare function clearCache(url?: string): void;
|
|
11
31
|
|
|
32
|
+
/**
|
|
33
|
+
* GLOBAL TYPE DEFINITION
|
|
34
|
+
* Informs TypeScript about our DevTools bridge on the window object.
|
|
35
|
+
*/
|
|
12
36
|
declare global {
|
|
13
37
|
interface Window {
|
|
14
38
|
__NCB_DEVTOOLS__?: {
|
|
@@ -17,6 +41,5 @@ declare global {
|
|
|
17
41
|
};
|
|
18
42
|
}
|
|
19
43
|
}
|
|
20
|
-
declare const emitUpdate: () => void;
|
|
21
44
|
|
|
22
|
-
export { type
|
|
45
|
+
export { type CacheEntry, cacheEngine, clearCache, setCache, smartFetch };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var d=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var D=(e,t)=>{for(var n in t)d(e,n,{get:t[n],enumerable:!0})},I=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of N(t))!F.call(e,r)&&r!==n&&d(e,r,{get:()=>t[r],enumerable:!(o=O(t,r))||o.enumerable});return e};var B=e=>I(d({},"__esModule",{value:!0}),e);var P={};D(P,{cacheEngine:()=>c,clearCache:()=>T,setCache:()=>i,smartFetch:()=>x});module.exports=B(P);var g=new Set,c={subscribe(e){return g.add(e),e(this.getValues()),()=>g.delete(e)},getValues(){return Array.from(s.values())},notify(e){let t=e||this.getValues();g.forEach(n=>n(t))}};var y=Symbol.for("ncb.cache"),l=Symbol.for("ncb.pending"),w=Symbol.for("ncb.inflight");globalThis[y]||(globalThis[y]=new Map);globalThis[l]||(globalThis[l]=new Map);globalThis[w]||(globalThis[w]=new Map);var s=globalThis[y],m=globalThis[l],p=globalThis[w];var A="ncb_cache_snapshot",L="ncb-devtools-update";var h=()=>{if(typeof window>"u")return;let e=c.getValues();try{localStorage.setItem(A,JSON.stringify(e))}catch(t){console.warn("NCB: Persist failed",t)}c.notify(e),window.dispatchEvent(new CustomEvent(L,{detail:e}))};var E="ncb_persistent_storage";function i(e,t,n){let o={key:e,data:t,expiry:Date.now()+n,ttl:n};if(s.set(e,o),typeof window<"u")try{let r=localStorage.getItem(E),a=r?JSON.parse(r):{};a[e]=o,localStorage.setItem(E,JSON.stringify(a))}catch(r){console.warn("NCB: Persistence failed",r)}return h(),o}async function b(e,t){let n=Date.now(),o=s.get(e);if(o&&o.expiry>n)return o.data;if(m.has(e))return m.get(e);let r=fetch(e).then(a=>a.json()).then(a=>(i(e,a,t),m.delete(e),a)).catch(a=>{throw m.delete(e),a});return m.set(e,r),r}async function C(e,t){try{let o=await(await fetch(e)).json();return i(e,o,t),o}catch(n){let o=s.get(e);if(o)return o.data;throw n}}async function S(e){return(await fetch(e)).json()}async function _(e,t){let n=s.get(e);if(n)return fetch(e).then(a=>a.json()).then(a=>i(e,a,t)).catch(a=>console.error("SWR Background Fetch Failed:",a)),n.data;let r=await(await fetch(e)).json();return i(e,r,t),r}async function x(e,t){let n=t?.ttl??1e4,o=t?.strategy??"cache-first",r=t?.dedupe??!0;if(r&&p.has(e))return p.get(e);let u=(async()=>{try{let f;switch(o){case"network-first":f=await C(e,n);break;case"swr":f=await _(e,n);break;case"no-cache":f=await S(e);break;default:f=await b(e,n);break}return h(),f}catch(f){throw console.error(`[NextCacheBrain] Fetch failed for ${e}:`,f),f}finally{r&&p.delete(e)}})();return r&&p.set(e,u),u}var v="ncb_persistent_storage";function T(e){if(e){if(s.delete(e),typeof window<"u"){let t=Array.from(s.values());localStorage.setItem(v,JSON.stringify(t))}}else s.clear(),typeof window<"u"&&localStorage.removeItem(v);h()}if(typeof window<"u"){try{let e=localStorage.getItem("ncb_persistent_storage");if(e){let t=JSON.parse(e);Object.values(t).forEach(n=>{n.expiry>Date.now()&&s.set(n.key,n)}),c.notify()}}catch(e){console.error("NCB Core: Hydration failed",e)}window.__NCB_DEVTOOLS__={getCache:()=>c.getValues(),subscribe:e=>c.subscribe(e)}}0&&(module.exports={cacheEngine,clearCache,setCache,smartFetch});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var d=new Set,c={subscribe(e){return d.add(e),e(this.getValues()),()=>d.delete(e)},getValues(){return Array.from(a.values())},notify(e){let t=e||this.getValues();d.forEach(n=>n(t))}};var g=Symbol.for("ncb.cache"),y=Symbol.for("ncb.pending"),l=Symbol.for("ncb.inflight");globalThis[g]||(globalThis[g]=new Map);globalThis[y]||(globalThis[y]=new Map);globalThis[l]||(globalThis[l]=new Map);var a=globalThis[g],m=globalThis[y],p=globalThis[l];var x="ncb_cache_snapshot",v="ncb-devtools-update";var h=()=>{if(typeof window>"u")return;let e=c.getValues();try{localStorage.setItem(x,JSON.stringify(e))}catch(t){console.warn("NCB: Persist failed",t)}c.notify(e),window.dispatchEvent(new CustomEvent(v,{detail:e}))};var u="ncb_persistent_storage";function f(e,t,n){let r={key:e,data:t,expiry:Date.now()+n,ttl:n};if(a.set(e,r),typeof window<"u")try{let s=localStorage.getItem(u),o=s?JSON.parse(s):{};o[e]=r,localStorage.setItem(u,JSON.stringify(o))}catch(s){console.warn("NCB: Persistence failed",s)}return h(),r}async function E(e,t){let n=Date.now(),r=a.get(e);if(r&&r.expiry>n)return r.data;if(m.has(e))return m.get(e);let s=fetch(e).then(o=>o.json()).then(o=>(f(e,o,t),m.delete(e),o)).catch(o=>{throw m.delete(e),o});return m.set(e,s),s}async function b(e,t){try{let r=await(await fetch(e)).json();return f(e,r,t),r}catch(n){let r=a.get(e);if(r)return r.data;throw n}}async function C(e){return(await fetch(e)).json()}async function S(e,t){let n=a.get(e);if(n)return fetch(e).then(o=>o.json()).then(o=>f(e,o,t)).catch(o=>console.error("SWR Background Fetch Failed:",o)),n.data;let s=await(await fetch(e)).json();return f(e,s,t),s}async function T(e,t){let n=t?.ttl??1e4,r=t?.strategy??"cache-first",s=t?.dedupe??!0;if(s&&p.has(e))return p.get(e);let w=(async()=>{try{let i;switch(r){case"network-first":i=await b(e,n);break;case"swr":i=await S(e,n);break;case"no-cache":i=await C(e);break;default:i=await E(e,n);break}return h(),i}catch(i){throw console.error(`[NextCacheBrain] Fetch failed for ${e}:`,i),i}finally{s&&p.delete(e)}})();return s&&p.set(e,w),w}var _="ncb_persistent_storage";function O(e){if(e){if(a.delete(e),typeof window<"u"){let t=Array.from(a.values());localStorage.setItem(_,JSON.stringify(t))}}else a.clear(),typeof window<"u"&&localStorage.removeItem(_);h()}if(typeof window<"u"){try{let e=localStorage.getItem("ncb_persistent_storage");if(e){let t=JSON.parse(e);Object.values(t).forEach(n=>{n.expiry>Date.now()&&a.set(n.key,n)}),c.notify()}}catch(e){console.error("NCB Core: Hydration failed",e)}window.__NCB_DEVTOOLS__={getCache:()=>c.getValues(),subscribe:e=>c.subscribe(e)}}export{c as cacheEngine,O as clearCache,f as setCache,T as smartFetch};
|