@pixel-pulse/next-cache-brain-core 0.1.0 → 0.2.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 CHANGED
@@ -9,14 +9,13 @@ declare function smartFetch(url: string, options?: SmartFetchOptions): Promise<a
9
9
 
10
10
  declare function clearCache(url?: string): void;
11
11
 
12
- declare global {
13
- interface Window {
14
- __NCB_DEVTOOLS__?: {
15
- getCache: () => any[];
16
- subscribe: (cb: () => void) => () => void;
17
- };
18
- }
12
+ interface CacheEntry<T> {
13
+ key: string;
14
+ data: T;
15
+ expiry: number;
16
+ ttl: number;
19
17
  }
20
- declare const emitUpdate: () => void;
21
18
 
22
- export { type SmartFetchOptions, type Strategy, clearCache, emitUpdate, smartFetch };
19
+ declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
20
+
21
+ export { type CacheEntry, type SmartFetchOptions, clearCache, setCache, smartFetch };
package/dist/index.d.ts CHANGED
@@ -9,14 +9,13 @@ declare function smartFetch(url: string, options?: SmartFetchOptions): Promise<a
9
9
 
10
10
  declare function clearCache(url?: string): void;
11
11
 
12
- declare global {
13
- interface Window {
14
- __NCB_DEVTOOLS__?: {
15
- getCache: () => any[];
16
- subscribe: (cb: () => void) => () => void;
17
- };
18
- }
12
+ interface CacheEntry<T> {
13
+ key: string;
14
+ data: T;
15
+ expiry: number;
16
+ ttl: number;
19
17
  }
20
- declare const emitUpdate: () => void;
21
18
 
22
- export { type SmartFetchOptions, type Strategy, clearCache, emitUpdate, smartFetch };
19
+ declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
20
+
21
+ export { type CacheEntry, type SmartFetchOptions, clearCache, setCache, smartFetch };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var f=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var E=(t,e)=>{for(var o in e)f(t,o,{get:e[o],enumerable:!0})},S=(t,e,o,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of D(e))!T.call(t,a)&&a!==o&&f(t,a,{get:()=>e[a],enumerable:!(n=C(e,a))||n.enumerable});return t};var O=t=>S(f({},"__esModule",{value:!0}),t);var M={};E(M,{clearCache:()=>_,emitUpdate:()=>i,smartFetch:()=>F});module.exports=O(M);var h=Symbol.for("ncb.cache"),y=Symbol.for("ncb.pending"),d=Symbol.for("ncb.inflight");globalThis[h]||(globalThis[h]=new Map);globalThis[y]||(globalThis[y]=new Map);globalThis[d]||(globalThis[d]=new Map);var r=globalThis[h],p=globalThis[y],m=globalThis[d];var g=new Set,i=()=>{g.forEach(t=>t())},w=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:t,subscribe(e){return g.add(e),()=>g.delete(e)}})};async function b(t,e){let o=Date.now(),n=r.get(t);if(n&&n.expiry>o)return n.data;if(p.has(t))return p.get(t);let a=fetch(t).then(s=>s.json()).then(s=>(r.set(t,{data:s,expiry:Date.now()+e,ttl:e}),p.delete(t),i(),s)).catch(s=>{throw p.delete(t),s});return p.set(t,a),i(),a}async function l(t,e){try{let n=await(await fetch(t)).json();return r.set(t,{data:n,expiry:Date.now()+e,ttl:e}),i(),n}catch(o){let n=r.get(t);if(n)return n.data;throw o}}async function u(t){return(await fetch(t)).json()}async function x(t,e){let o=Date.now(),n=r.get(t);if(n)return fetch(t).then(c=>c.json()).then(c=>{r.set(t,{data:c,expiry:Date.now()+e,ttl:e}),i()}).catch(c=>console.error("SWR Background Fetch Failed:",c)),n.data;let s=await(await fetch(t)).json();return r.set(t,{data:s,expiry:Date.now()+e,ttl:e}),i(),s}async function F(t,e){let o=e?.ttl??1e4,n=e?.strategy??"cache-first",a=e?.dedupe??!1;if(a&&m.has(t))return m.get(t);let c=(async()=>{try{switch(n){case"network-first":return await l(t,o);case"swr":return await x(t,o);case"no-cache":return await u(t);default:return await b(t,o)}}finally{a&&m.delete(t)}})();return a&&m.set(t,c),c}function _(t){t?r.delete(t):r.clear()}typeof window<"u"&&w(()=>Array.from(r.entries()).map(([t,e])=>({key:t,...e})));0&&(module.exports={clearCache,emitUpdate,smartFetch});
1
+ "use strict";var h=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var N=(t,e)=>{for(var r in e)h(t,r,{get:e[r],enumerable:!0})},v=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of F(e))!D.call(t,o)&&o!==r&&h(t,o,{get:()=>e[o],enumerable:!(n=O(e,o))||n.enumerable});return t};var I=t=>v(h({},"__esModule",{value:!0}),t);var K={};N(K,{clearCache:()=>T,setCache:()=>c,smartFetch:()=>E});module.exports=I(K);var p=Symbol.for("ncb.cache"),m=Symbol.for("ncb.pending"),g=Symbol.for("ncb.inflight");globalThis[p]||(globalThis[p]=new Map);globalThis[m]||(globalThis[m]=new Map);globalThis[g]||(globalThis[g]=new Map);var s=globalThis[p],i=globalThis[m],f=globalThis[g];var l="ncb_cache_snapshot",y=new Set,w=t=>{t&&typeof window<"u"&&localStorage.setItem(l,JSON.stringify(t)),y.forEach(e=>e())},b=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:()=>{let e=t();if(e.length===0){let r=localStorage.getItem(l);return r?JSON.parse(r):[]}return e},subscribe(e){return y.add(e),()=>y.delete(e)}})};var u="ncb_persistent_storage";function c(t,e,r){let n={key:t,data:e,expiry:Date.now()+r,ttl:r};if(s.set(t,n),typeof window<"u"){let o=localStorage.getItem(u),a=o?JSON.parse(o):{};a[t]=n,localStorage.setItem(u,JSON.stringify(a))}return w(),n}async function x(t,e){let r=Date.now(),n=s.get(t);if(n&&n.expiry>r)return n.data;if(i.has(t))return i.get(t);let o=fetch(t).then(a=>a.json()).then(a=>(c(t,a,e),i.delete(t),a)).catch(a=>{throw i.delete(t),a});return i.set(t,o),o}async function C(t,e){try{let n=await(await fetch(t)).json();return c(t,n,e),n}catch(r){let n=s.get(t);if(n)return n.data;throw r}}async function S(t){return(await fetch(t)).json()}async function _(t,e){let r=s.get(t);if(r)return fetch(t).then(a=>a.json()).then(a=>c(t,a,e)).catch(a=>console.error("SWR Background Fetch Failed:",a)),r.data;let o=await(await fetch(t)).json();return c(t,o,e),o}async function E(t,e){let r=e?.ttl??1e4,n=e?.strategy??"cache-first",o=e?.dedupe??!1;if(o&&f.has(t))return f.get(t);let d=(async()=>{try{switch(n){case"network-first":return await C(t,r);case"swr":return await _(t,r);case"no-cache":return await S(t);default:return await x(t,r)}}finally{o&&f.delete(t)}})();return o&&f.set(t,d),d}function T(t){t?s.delete(t):s.clear()}var j="ncb_persistent_storage";if(typeof window<"u"){try{let t=localStorage.getItem(j);if(t){let e=JSON.parse(t);Object.values(e).forEach(r=>{r.expiry>Date.now()&&s.set(r.key,r)})}}catch(t){console.error("NCB Core: Hydration failed",t)}b(()=>Array.from(s.values()))}0&&(module.exports={clearCache,setCache,smartFetch});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var f=Symbol.for("ncb.cache"),h=Symbol.for("ncb.pending"),y=Symbol.for("ncb.inflight");globalThis[f]||(globalThis[f]=new Map);globalThis[h]||(globalThis[h]=new Map);globalThis[y]||(globalThis[y]=new Map);var n=globalThis[f],p=globalThis[h],m=globalThis[y];var d=new Set,s=()=>{d.forEach(t=>t())},g=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:t,subscribe(e){return d.add(e),()=>d.delete(e)}})};async function w(t,e){let a=Date.now(),r=n.get(t);if(r&&r.expiry>a)return r.data;if(p.has(t))return p.get(t);let c=fetch(t).then(o=>o.json()).then(o=>(n.set(t,{data:o,expiry:Date.now()+e,ttl:e}),p.delete(t),s(),o)).catch(o=>{throw p.delete(t),o});return p.set(t,c),s(),c}async function b(t,e){try{let r=await(await fetch(t)).json();return n.set(t,{data:r,expiry:Date.now()+e,ttl:e}),s(),r}catch(a){let r=n.get(t);if(r)return r.data;throw a}}async function l(t){return(await fetch(t)).json()}async function u(t,e){let a=Date.now(),r=n.get(t);if(r)return fetch(t).then(i=>i.json()).then(i=>{n.set(t,{data:i,expiry:Date.now()+e,ttl:e}),s()}).catch(i=>console.error("SWR Background Fetch Failed:",i)),r.data;let o=await(await fetch(t)).json();return n.set(t,{data:o,expiry:Date.now()+e,ttl:e}),s(),o}async function x(t,e){let a=e?.ttl??1e4,r=e?.strategy??"cache-first",c=e?.dedupe??!1;if(c&&m.has(t))return m.get(t);let i=(async()=>{try{switch(r){case"network-first":return await b(t,a);case"swr":return await u(t,a);case"no-cache":return await l(t);default:return await w(t,a)}}finally{c&&m.delete(t)}})();return c&&m.set(t,i),i}function F(t){t?n.delete(t):n.clear()}typeof window<"u"&&g(()=>Array.from(n.entries()).map(([t,e])=>({key:t,...e})));export{F as clearCache,s as emitUpdate,x as smartFetch};
1
+ var h=Symbol.for("ncb.cache"),p=Symbol.for("ncb.pending"),m=Symbol.for("ncb.inflight");globalThis[h]||(globalThis[h]=new Map);globalThis[p]||(globalThis[p]=new Map);globalThis[m]||(globalThis[m]=new Map);var a=globalThis[h],i=globalThis[p],f=globalThis[m];var d="ncb_cache_snapshot",g=new Set,l=t=>{t&&typeof window<"u"&&localStorage.setItem(d,JSON.stringify(t)),g.forEach(e=>e())},w=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:()=>{let e=t();if(e.length===0){let r=localStorage.getItem(d);return r?JSON.parse(r):[]}return e},subscribe(e){return g.add(e),()=>g.delete(e)}})};var b="ncb_persistent_storage";function c(t,e,r){let n={key:t,data:e,expiry:Date.now()+r,ttl:r};if(a.set(t,n),typeof window<"u"){let s=localStorage.getItem(b),o=s?JSON.parse(s):{};o[t]=n,localStorage.setItem(b,JSON.stringify(o))}return l(),n}async function u(t,e){let r=Date.now(),n=a.get(t);if(n&&n.expiry>r)return n.data;if(i.has(t))return i.get(t);let s=fetch(t).then(o=>o.json()).then(o=>(c(t,o,e),i.delete(t),o)).catch(o=>{throw i.delete(t),o});return i.set(t,s),s}async function x(t,e){try{let n=await(await fetch(t)).json();return c(t,n,e),n}catch(r){let n=a.get(t);if(n)return n.data;throw r}}async function C(t){return(await fetch(t)).json()}async function S(t,e){let r=a.get(t);if(r)return fetch(t).then(o=>o.json()).then(o=>c(t,o,e)).catch(o=>console.error("SWR Background Fetch Failed:",o)),r.data;let s=await(await fetch(t)).json();return c(t,s,e),s}async function _(t,e){let r=e?.ttl??1e4,n=e?.strategy??"cache-first",s=e?.dedupe??!1;if(s&&f.has(t))return f.get(t);let y=(async()=>{try{switch(n){case"network-first":return await x(t,r);case"swr":return await S(t,r);case"no-cache":return await C(t);default:return await u(t,r)}}finally{s&&f.delete(t)}})();return s&&f.set(t,y),y}function E(t){t?a.delete(t):a.clear()}var T="ncb_persistent_storage";if(typeof window<"u"){try{let t=localStorage.getItem(T);if(t){let e=JSON.parse(t);Object.values(e).forEach(r=>{r.expiry>Date.now()&&a.set(r.key,r)})}}catch(t){console.error("NCB Core: Hydration failed",t)}w(()=>Array.from(a.values()))}export{E as clearCache,c as setCache,_ as smartFetch};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixel-pulse/next-cache-brain-core",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",