@pixel-pulse/cache-brain-react 1.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/LICENSE +8 -0
- package/README.md +156 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright © 2026 Lin Htet Aung (Liam)
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://cache-brain.netlify.app/og-image-next.jpeg" width="100%" alt="Cache Brain - Smart Caching Engine"/>
|
|
3
|
+
|
|
4
|
+
<br/>
|
|
5
|
+
|
|
6
|
+
[](https://www.npmjs.com/package/@pixel-pulse/cache-brain-react)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
|
|
9
|
+
<h3>Next-Generation Caching Engine for React.js & Next.js</h3>
|
|
10
|
+
<p align="center"><b>The React adapter for Cache Brain.</b> Provides reactive hooks and context providers for seamless state synchronization in React and Next.js applications.</p>
|
|
11
|
+
|
|
12
|
+
<p>
|
|
13
|
+
<a href="https://cache-brain.netlify.app/"><b>Cache Brain</b></a> •
|
|
14
|
+
<a href="https://cache-brain.netlify.app/doc"><b>Documentation</b></a> •
|
|
15
|
+
<a href="https://cache-brain.netlify.app/doc/playground/core"><b>Server Lab</b></a> •
|
|
16
|
+
<a href="https://cache-brain.netlify.app/doc/playground/hook"><b>Client Lab</b></a> •
|
|
17
|
+
<a href="https://github.com/LinnHtetAungSE"><b>GitHub</b></a>
|
|
18
|
+
</p>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
## ✨ Features
|
|
22
|
+
|
|
23
|
+
- **Atomic Pulse Engine:** Unified API supporting `swr`, `cache-first`, `network-first` and `no-cache` strategies with a single function call.
|
|
24
|
+
- **Visual Diagnostics:** Real-time DevTools to track **TTL**, **Registry Hits**, and **Deduplication** events as they happen.
|
|
25
|
+
- **Event Bridge:** Reactive synchronization ensuring that when data updates in one component, the entire UI stays in sync.
|
|
26
|
+
- **App Router Native:** Deeply optimized for Next.js Server Components, Actions, and high-performance client hydration.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## ⚠️ Beta Version Notice
|
|
31
|
+
|
|
32
|
+
This project is currently in **v1.0.0-beta**. We are actively refining the **Registry Pulse** logic and synchronization performance.
|
|
33
|
+
|
|
34
|
+
- Please report any bugs via GitHub Issues.
|
|
35
|
+
- Expect breaking changes until version **1.2.0**.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 📦 Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm add @pixel-pulse/cache-brain-react
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 🚀 Setup
|
|
48
|
+
|
|
49
|
+
#### Wrap your App
|
|
50
|
+
In **Next.js (App Router)**, wrap your `layout.tsx`. In Vite, wrap your `App.tsx`.
|
|
51
|
+
|
|
52
|
+
```TypeScript
|
|
53
|
+
"use client";
|
|
54
|
+
|
|
55
|
+
import { useMemo } from "react";
|
|
56
|
+
import { CacheBrainClient } from "@pixel-pulse/cache-brain";
|
|
57
|
+
import { CacheBrainProvider } from "@pixel-pulse/cache-brain-react";
|
|
58
|
+
|
|
59
|
+
// Initialize the client
|
|
60
|
+
const client = new CacheBrainClient({ defaultTTL: 300000 });
|
|
61
|
+
|
|
62
|
+
export default function RootLayout({ children }) {
|
|
63
|
+
return (
|
|
64
|
+
<CacheBrainProvider client={client}>
|
|
65
|
+
{children}
|
|
66
|
+
</CacheBrainProvider>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 🎣 Usage
|
|
72
|
+
|
|
73
|
+
**`useSmartCache`**
|
|
74
|
+
|
|
75
|
+
The primary hook for data fetching. It automatically tracks loading states and synchronizes with the global cache.
|
|
76
|
+
|
|
77
|
+
```TypeScript
|
|
78
|
+
import { useSmartCache } from "@pixel-pulse/cache-brain-react";
|
|
79
|
+
|
|
80
|
+
function UserProfile({ id }) {
|
|
81
|
+
const { data, isLoading, error, revalidate } = useSmartCache(
|
|
82
|
+
['user', id],
|
|
83
|
+
() => fetch(`/api/user/${id}`).then(res => res.json())
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (isLoading) return <div>Loading...</div>;
|
|
87
|
+
if (error) return <div>Error loading data.</div>;
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div>
|
|
91
|
+
<h1>{data.name}</h1>
|
|
92
|
+
<button onClick={() => revalidate()}>Refresh Data</button>
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### ⚙️ Hook Options (SmartCacheOptions)
|
|
99
|
+
|
|
100
|
+
The `useSmartCache` hook accepts a configuration object as its third argument to fine-tune the behavior of individual requests.
|
|
101
|
+
|
|
102
|
+
| Property | Type | Description | Default |
|
|
103
|
+
| :--------- | :--------- | :--------------------------------------------------------------------------------------------------- | :---------- |
|
|
104
|
+
| `strategy` | `Strategy` | Defines how the engine handles data (e.g., `swr`, `network-first`). | `default` |
|
|
105
|
+
| `ttl` | `number` | Time-to-live in milliseconds. Overrides the global client setting for this fetch. | `300000` |
|
|
106
|
+
| `dedupe` | `boolean` | When `true`, multiple identical calls within the same timeframe are collapsed into a single request. | `true` |
|
|
107
|
+
| `persist` | `boolean` | If enabled, the engine will attempt to save this specific result to persistent storage. | `false` |
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## 🔗 Related Packages
|
|
112
|
+
|
|
113
|
+
- **Visual Debugger:** [@pixel-pulse/cache-brain-react-devtools](https://www.npmjs.com/package/@pixel-pulse/cache-brain-react-devtools)
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### 🛡 License
|
|
118
|
+
|
|
119
|
+
<details>
|
|
120
|
+
<summary>Distributed under the <b>MIT License</b>. Click to view full license.</summary>
|
|
121
|
+
<br />
|
|
122
|
+
|
|
123
|
+
```text
|
|
124
|
+
MIT License
|
|
125
|
+
|
|
126
|
+
Copyright (c) Lin Htet Aung | 2026 Pixel Pulse Tech
|
|
127
|
+
|
|
128
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
129
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
130
|
+
in the Software without restriction, including without limitation the rights
|
|
131
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
132
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
133
|
+
furnished to do so, subject to the following conditions:
|
|
134
|
+
|
|
135
|
+
The above copyright notice and this permission notice shall be included in all
|
|
136
|
+
copies or substantial portions of the Software.
|
|
137
|
+
|
|
138
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
139
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
140
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
141
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
142
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
143
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
144
|
+
SOFTWARE.
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
</details>
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
<p align="center">
|
|
152
|
+
<br />
|
|
153
|
+
<b>Engineered by Pixel Pulse Tech</b><br />
|
|
154
|
+
<sub>Redefining state management for 2026.</sub>
|
|
155
|
+
</p>
|
|
156
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SmartCacheOptions } from '@pixel-pulse/cache-brain/types';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { CacheBrainClient } from '@pixel-pulse/cache-brain';
|
|
5
|
+
export * from '@pixel-pulse/cache-brain';
|
|
6
|
+
|
|
7
|
+
declare function useSmartCache<T = any>(key: string | any[], fetcher: () => Promise<T>, options?: SmartCacheOptions): {
|
|
8
|
+
data: T | null;
|
|
9
|
+
isLoading: boolean;
|
|
10
|
+
isFetching: boolean;
|
|
11
|
+
error: Error | null;
|
|
12
|
+
refresh: (manualOptions?: SmartCacheOptions) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare function useCacheData<T = any>(key: string | any[]): T | null;
|
|
16
|
+
|
|
17
|
+
interface CacheContextValue {
|
|
18
|
+
client: CacheBrainClient;
|
|
19
|
+
entries: any[];
|
|
20
|
+
}
|
|
21
|
+
declare const CacheBrainProvider: ({ children, client, }: {
|
|
22
|
+
children: React.ReactNode;
|
|
23
|
+
client: CacheBrainClient;
|
|
24
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
25
|
+
declare const useCacheClient: () => CacheContextValue;
|
|
26
|
+
|
|
27
|
+
export { CacheBrainProvider, useCacheClient, useCacheData, useSmartCache };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SmartCacheOptions } from '@pixel-pulse/cache-brain/types';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { CacheBrainClient } from '@pixel-pulse/cache-brain';
|
|
5
|
+
export * from '@pixel-pulse/cache-brain';
|
|
6
|
+
|
|
7
|
+
declare function useSmartCache<T = any>(key: string | any[], fetcher: () => Promise<T>, options?: SmartCacheOptions): {
|
|
8
|
+
data: T | null;
|
|
9
|
+
isLoading: boolean;
|
|
10
|
+
isFetching: boolean;
|
|
11
|
+
error: Error | null;
|
|
12
|
+
refresh: (manualOptions?: SmartCacheOptions) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare function useCacheData<T = any>(key: string | any[]): T | null;
|
|
16
|
+
|
|
17
|
+
interface CacheContextValue {
|
|
18
|
+
client: CacheBrainClient;
|
|
19
|
+
entries: any[];
|
|
20
|
+
}
|
|
21
|
+
declare const CacheBrainProvider: ({ children, client, }: {
|
|
22
|
+
children: React.ReactNode;
|
|
23
|
+
client: CacheBrainClient;
|
|
24
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
25
|
+
declare const useCacheClient: () => CacheContextValue;
|
|
26
|
+
|
|
27
|
+
export { CacheBrainProvider, useCacheClient, useCacheData, useSmartCache };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var T=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var G=(e,t)=>{for(var r in t)T(e,r,{get:t[r],enumerable:!0})},P=(e,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of J(t))!L.call(e,a)&&a!==r&&T(e,a,{get:()=>t[a],enumerable:!(s=I(t,a))||s.enumerable});return e},C=(e,t,r)=>(P(e,t,"default"),r&&P(r,t,"default"));var H=e=>P(T({},"__esModule",{value:!0}),e);var u={};G(u,{CacheBrainProvider:()=>V,useCacheClient:()=>q,useCacheData:()=>M,useSmartCache:()=>D});module.exports=H(u);var n=require("react"),o=require("@pixel-pulse/cache-brain");function D(e,t,r){let[s,a]=(0,n.useState)(null),[m,l]=(0,n.useState)(!0),[d,b]=(0,n.useState)(!1),[F,w]=(0,n.useState)(null),S=(0,n.useRef)(null),K=(0,n.useRef)(t),A=(0,n.useRef)(r);(0,n.useEffect)(()=>{K.current=t,A.current=r},[t,r]);let R=(0,n.useMemo)(()=>Array.isArray(e)?e:[String(e)],[JSON.stringify(e)]),x=(0,n.useMemo)(()=>(0,o.normalizeKey)(R),[R]),E=(0,n.useCallback)(async p=>{S.current||l(!0),b(!0),w(null);try{let y={...A.current,...p},v=await(0,o.smartCache)(R,K.current,y);a(v),S.current=v}catch(y){w(y)}finally{l(!1),b(!1)}},[x]);return(0,n.useEffect)(()=>{let p=!0;E();let y=o.cacheEngine.onRevalidate(g=>{if(!p)return;let f=!g,B=g?(0,o.normalizeKey)(g):null;(f||B===x)&&E({strategy:"network-first"})}),v=o.cacheEngine.subscribe(g=>{if(!p)return;let f=g.find(B=>(0,o.normalizeKey)(B.key)===x);f&&f.data!==S.current&&(a(f.data),S.current=f.data)});return()=>{p=!1,y(),v()}},[x,E]),{data:s,isLoading:m,isFetching:d,error:F,refresh:E}}var i=require("react"),h=require("@pixel-pulse/cache-brain");function M(e){let t=(0,i.useMemo)(()=>Array.isArray(e)?e:[String(e)],[JSON.stringify(e)]),r=(0,i.useMemo)(()=>(0,h.normalizeKey)(t),[t]),[s,a]=(0,i.useState)(()=>h.cacheEngine.get(t)||null);return(0,i.useEffect)(()=>{let m=h.cacheEngine.subscribe(l=>{let d=l.find(b=>(0,h.normalizeKey)(b.key)===r);d?d.data!==s&&a(d.data):a(null)});return()=>{m()}},[r,s]),s}var c=require("react"),O=require("@pixel-pulse/cache-brain"),z=require("react/jsx-runtime"),N=(0,c.createContext)(null),V=({children:e,client:t})=>{let[r,s]=(0,c.useState)(()=>O.cacheEngine.getValues());(0,c.useEffect)(()=>{let m=O.cacheEngine.subscribe(l=>s([...l]));return()=>{m()}},[]);let a=(0,c.useMemo)(()=>({client:t,entries:r}),[t,r]);return(0,z.jsx)(N.Provider,{value:a,children:e})},q=()=>{let e=(0,c.useContext)(N);if(!e)throw new Error("CB: useSmartCache must be wrapped in a <CacheBrainProvider />. Please provide a CacheBrainClient instance to the provider.");return e};C(u,require("@pixel-pulse/cache-brain"),module.exports);0&&(module.exports={CacheBrainProvider,useCacheClient,useCacheData,useSmartCache,...require("@pixel-pulse/cache-brain")});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useState as y,useEffect as B,useCallback as N,useRef as S,useMemo as P}from"react";import{cacheEngine as T,normalizeKey as x,smartCache as V}from"@pixel-pulse/cache-brain";function q(e,t,r){let[n,a]=y(null),[o,s]=y(!0),[i,C]=y(!1),[M,E]=y(null),h=S(null),v=S(t),R=S(r);B(()=>{v.current=t,R.current=r},[t,r]);let g=P(()=>Array.isArray(e)?e:[String(e)],[JSON.stringify(e)]),m=P(()=>x(g),[g]),d=N(async u=>{h.current||s(!0),C(!0),E(null);try{let l={...R.current,...u},p=await V(g,v.current,l);a(p),h.current=p}catch(l){E(l)}finally{s(!1),C(!1)}},[m]);return B(()=>{let u=!0;d();let l=T.onRevalidate(f=>{if(!u)return;let c=!f,b=f?x(f):null;(c||b===m)&&d({strategy:"network-first"})}),p=T.subscribe(f=>{if(!u)return;let c=f.find(b=>x(b.key)===m);c&&c.data!==h.current&&(a(c.data),h.current=c.data)});return()=>{u=!1,l(),p()}},[m,d]),{data:n,isLoading:o,isFetching:i,error:M,refresh:d}}import{useState as z,useEffect as F,useMemo as O}from"react";import{cacheEngine as w,normalizeKey as K}from"@pixel-pulse/cache-brain";function I(e){let t=O(()=>Array.isArray(e)?e:[String(e)],[JSON.stringify(e)]),r=O(()=>K(t),[t]),[n,a]=z(()=>w.get(t)||null);return F(()=>{let o=w.subscribe(s=>{let i=s.find(C=>K(C.key)===r);i?i.data!==n&&a(i.data):a(null)});return()=>{o()}},[r,n]),n}import{createContext as J,useContext as L,useEffect as G,useState as H,useMemo as Q}from"react";import{cacheEngine as A}from"@pixel-pulse/cache-brain";import{jsx as X}from"react/jsx-runtime";var D=J(null),U=({children:e,client:t})=>{let[r,n]=H(()=>A.getValues());G(()=>{let o=A.subscribe(s=>n([...s]));return()=>{o()}},[]);let a=Q(()=>({client:t,entries:r}),[t,r]);return X(D.Provider,{value:a,children:e})},W=()=>{let e=L(D);if(!e)throw new Error("CB: useSmartCache must be wrapped in a <CacheBrainProvider />. Please provide a CacheBrainClient instance to the provider.");return e};export*from"@pixel-pulse/cache-brain";export{U as CacheBrainProvider,W as useCacheClient,I as useCacheData,q as useSmartCache};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pixel-pulse/cache-brain-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React & Next.js adapter for Cache Brain engine.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@pixel-pulse/cache-brain": "1.0.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": ">=18.0.0",
|
|
23
|
+
"react-dom": ">=18.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.19.39",
|
|
27
|
+
"@types/react": "^18.3.1",
|
|
28
|
+
"@types/react-dom": "^18.3.1",
|
|
29
|
+
"react": "18.3.1",
|
|
30
|
+
"react-dom": "18.3.1",
|
|
31
|
+
"tsup": "^8.0.0",
|
|
32
|
+
"typescript": "^6.0.2"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup",
|
|
36
|
+
"dev": "tsup --watch"
|
|
37
|
+
}
|
|
38
|
+
}
|