@pixel-pulse/cache-brain-vue 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/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # @pixel-pulse/cache-brain-vue
2
+
3
+ ## 1.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 40a0ec9: # 🚀 Pixel Pulse Cache Brain Update
8
+
9
+ ### Core & Adapters
10
+
11
+ - **Interface Alignment:** Synchronized `CacheOptions` and `CacheEntry` documentation with the TypeScript source.
12
+ - **Vue Performance:** Updated `SmartCacheReturn` to use `ShallowRef` for the `data` property, significantly reducing reactivity overhead for large payloads.
13
+ - **Type Safety:** Improved generic type handling for `useSmartCache` across both React and Vue adapters.
14
+
15
+ ### DevTools
16
+
17
+ - **Visibility Control:** Added the `enabled` prop to `CacheBrainDevTools`. This allows for cleaner environment-based rendering (e.g., `:enabled="isDev"`).
18
+ - **Documentation:** Rewrote the README with high-end Markdown tables for better developer onboarding.
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,151 @@
1
+ <div align="center">
2
+ <img src="https://cache-brain.netlify.app/og-image-nuxt.jpeg" width="100%" alt="Cache Brain - Smart Caching Engine"/>
3
+
4
+ <br/>
5
+
6
+ [![npm version](https://img.shields.io/npm/v/@pixel-pulse/cache-brain-vue.svg)](https://www.npmjs.com/package/@pixel-pulse/cache-brain-vue)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
8
+
9
+ <h3>Next-Generation Caching Engine for Vue.js & Nuxt.js</h3>
10
+ <p align="center"><b>The Vue adapter for Cache Brain.</b> High-performance data fetching composables with deep reactivity for Vue 3 and Nuxt 3.</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
+ ---
22
+
23
+ ## ✨ Features
24
+
25
+ - **Atomic Pulse Engine:** Unified API supporting `swr`, `cache-first`, `network-first` and `no-cache` strategies with a single function call.
26
+ - **Visual Diagnostics:** Real-time DevTools to track **TTL**, **Registry Hits**, and **Deduplication** events as they happen.
27
+ - **Event Bridge:** Reactive synchronization ensuring that when data updates in one component, the entire UI stays in sync.
28
+ - **App Router Native:** Deeply optimized for Next.js Server Components, Actions, and high-performance client hydration.
29
+
30
+ ---
31
+
32
+ ## ⚠️ Beta Version Notice
33
+
34
+ This project is currently in **v1.0.0-beta**. We are actively refining the **Registry Pulse** logic and synchronization performance.
35
+
36
+ - Please report any bugs via GitHub Issues.
37
+ - Expect breaking changes until version **1.2.0**.
38
+
39
+ ---
40
+
41
+ ## 📦 Installation
42
+
43
+ ```bash
44
+ npm add @pixel-pulse/cache-brain-vue
45
+ ```
46
+
47
+ ## 🚀 Setup
48
+
49
+ #### Vue 3 (Vite/Plugin)
50
+ Initialize the client and install it as a plugin in your `main.ts`.
51
+
52
+ ```TypeScript
53
+ import { createApp } from 'vue';
54
+ import { CacheBrainClient } from '@pixel-pulse/cache-brain';
55
+ import { createCacheBrain } from '@pixel-pulse/cache-brain-vue';
56
+
57
+ const app = createApp(App);
58
+ const client = new CacheBrainClient({ defaultTTL: 300000 });
59
+
60
+ app.use(createCacheBrain(client));
61
+ app.mount('#app');
62
+ ```
63
+
64
+ ## 🎣 Usage
65
+
66
+ **`useSmartCache`**
67
+
68
+ The primary composable for fetching data. Returns fully reactive `Refs`.
69
+
70
+ ```TypeScript
71
+ <script setup lang="ts">
72
+ import { useSmartCache } from '@pixel-pulse/cache-brain-vue';
73
+
74
+ const { data, isLoading, refresh } = useSmartCache(
75
+ ['profile', 'settings'],
76
+ async () => {
77
+ const res = await fetch('/api/settings');
78
+ return res.json();
79
+ }
80
+ );
81
+ </script>
82
+
83
+ <template>
84
+ <div v-if="isLoading">Loading...</div>
85
+ <div v-else-if="data">
86
+ <h1>{{ data.userName }}</h1>
87
+ <button @click="refresh()">Sync Settings</button>
88
+ </div>
89
+ </template>
90
+ ```
91
+
92
+ ### ⚙️ Hook Options (SmartCacheOptions)
93
+
94
+ The `useSmartCache` composable returns a reactive object. Because it uses Vue's Reactivity API, you should access these properties using `.value` in scripts or directly in templates.
95
+
96
+ | Property | Type | Description |
97
+ | :----------- | :-------------------- | :------------------------------------------------------------------------------------------------------------ |
98
+ | `data` | `ShallowRef<T \| null>` | The retrieved data. Uses `shallowRef` to prevent deep reactivity overhead on large datasets. |
99
+ | `isLoading` | `Ref<boolean>` | `true` only during the initial fetch. Stays `false` during subsequent background refreshes. |
100
+ | `isFetching` | `Ref<boolean>` | `true` whenever a network request is active, including background revalidations. |
101
+ | `error` | `Ref<Error \| null>` | Captured error object if the fetcher function fails. |
102
+ | `refresh` | `Function` | A manual trigger to force a fresh fetch. Accepts optional overrides for `ttl` or `strategy`. |
103
+
104
+ ---
105
+
106
+ ## 🔗 Related Packages
107
+
108
+ - **Visual Debugger:** [@pixel-pulse/cache-brain-vue-devtools](https://www.npmjs.com/package/@pixel-pulse/cache-brain-vue-devtools)
109
+
110
+ ---
111
+
112
+ ### 🛡 License
113
+
114
+ <details>
115
+ <summary>Distributed under the <b>MIT License</b>. Click to view full license.</summary>
116
+ <br />
117
+
118
+ ```text
119
+ MIT License
120
+
121
+ Copyright (c) Lin Htet Aung | 2026 Pixel Pulse Tech
122
+
123
+ Permission is hereby granted, free of charge, to any person obtaining a copy
124
+ of this software and associated documentation files (the "Software"), to deal
125
+ in the Software without restriction, including without limitation the rights
126
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
127
+ copies of the Software, and to permit persons to whom the Software is
128
+ furnished to do so, subject to the following conditions:
129
+
130
+ The above copyright notice and this permission notice shall be included in all
131
+ copies or substantial portions of the Software.
132
+
133
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
134
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
135
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
136
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
137
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
138
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
139
+ SOFTWARE.
140
+
141
+ ```
142
+ </details>
143
+
144
+ ---
145
+
146
+ <p align="center">
147
+ <br />
148
+ <b>Engineered by Pixel Pulse Tech</b><br />
149
+ <sub>Redefining state management for 2026.</sub>
150
+ </p>
151
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var S=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var G=(e,a)=>{for(var r in a)S(e,r,{get:a[r],enumerable:!0})},M=(e,a,r,s)=>{if(a&&typeof a=="object"||typeof a=="function")for(let n of j(a))!z.call(e,n)&&n!==r&&S(e,n,{get:()=>a[n],enumerable:!(s=D(a,n))||s.enumerable});return e};var q=e=>M(S({},"__esModule",{value:!0}),e);var L={};G(L,{CacheBrainClient:()=>A.CacheBrainClient,cacheEngine:()=>d.cacheEngine,createCacheBrain:()=>E,normalizeKey:()=>d.normalizeKey,useCacheData:()=>R,useCacheInternal:()=>y,useSmartCache:()=>w});module.exports=q(L);var d=require("@pixel-pulse/cache-brain");var p=require("vue"),T=require("@pixel-pulse/cache-brain"),B=Symbol.for("cache-brain"),E=e=>{let a=(0,p.reactive)({entries:T.cacheEngine.getValues()});return T.cacheEngine.subscribe(r=>{a.entries=[...r]}),{install(r){r.provide(B,{client:e,state:(0,p.readonly)(a)})}}},y=()=>{let e=(0,p.inject)(B);if(!e)throw new Error("CB: useSmartCache must be wrapped in a CacheBrain Provider.");return e};var l=require("vue"),h=require("@pixel-pulse/cache-brain");function R(e){y();let a=(0,l.computed)(()=>Array.isArray(e)?e:[String(e)]),r=(0,l.computed)(()=>(0,h.normalizeKey)(a.value)),s=(0,l.shallowRef)(h.cacheEngine.get(a.value)||null),n=h.cacheEngine.subscribe(f=>{let u=f.find(C=>(0,h.normalizeKey)(C.key)===r.value);u?u.data!==s.value&&(s.value=u.data):s.value=null});return(0,l.onUnmounted)(()=>{n()}),(0,l.watch)(r,()=>{s.value=h.cacheEngine.get(a.value)||null}),s}var t=require("vue"),i=require("@pixel-pulse/cache-brain");function w(e,a,r){let{client:s}=y(),n=(0,t.shallowRef)(null),f=(0,t.ref)(!0),u=(0,t.ref)(!1),C=(0,t.ref)(null),g=(0,t.computed)(()=>{let o=typeof e=="function"?e():(0,t.unref)(e);return Array.isArray(o)?o.map(c=>(0,t.unref)(c)):[String((0,t.unref)(o))]}),b=(0,t.computed)(()=>(0,i.normalizeKey)(g.value)),K=()=>typeof r=="function"?r():r||{},v=async o=>{let c={...K(),...o},m=i.cacheEngine.get(g.value);if(c.strategy==="cache-first"&&m!==void 0){n.value=m,f.value=!1;return}n.value||(f.value=!0),u.value=!0,C.value=null;try{let x=await(0,i.smartCache)(g.value,a,c);n.value=x,(0,t.triggerRef)(n)}catch(x){C.value=x}finally{f.value=!1,u.value=!1}},O=i.cacheEngine.subscribe(o=>{let c=o.find(m=>(0,i.normalizeKey)(m.key)===b.value);if(c){let m=Date.now()>c.expiry;c.data!==n.value&&(n.value=c.data,f.value=!1),m&&!u.value&&v()}}),I=i.cacheEngine.onRevalidate(o=>{let c=!o,m=o?(0,i.normalizeKey)(o):null;(c||m===b.value)&&v({strategy:"network-first"})});return(0,t.watch)(b,(o,c)=>{o!==c&&v()},{immediate:!0}),(0,t.onUnmounted)(()=>{O(),I()}),{data:n,isLoading:f,isFetching:u,error:C,refresh:v}}var A=require("@pixel-pulse/cache-brain");0&&(module.exports={CacheBrainClient,cacheEngine,createCacheBrain,normalizeKey,useCacheData,useCacheInternal,useSmartCache});
@@ -0,0 +1,34 @@
1
+ import { CacheBrainClient } from '@pixel-pulse/cache-brain';
2
+ export { CacheBrainClient, cacheEngine, normalizeKey } from '@pixel-pulse/cache-brain';
3
+ import { App, DeepReadonly, Ref, ShallowRef } from 'vue';
4
+ import { SmartCacheOptions } from '@pixel-pulse/cache-brain/types';
5
+
6
+ interface CacheBrainContext {
7
+ client: CacheBrainClient;
8
+ state: DeepReadonly<{
9
+ entries: any[];
10
+ }>;
11
+ }
12
+ declare const createCacheBrain: (client: CacheBrainClient) => {
13
+ install(app: App): void;
14
+ };
15
+ declare const useCacheInternal: () => CacheBrainContext;
16
+
17
+ /**
18
+ * Explicitly defining the return type as Ref<T | null>
19
+ * prevents the TS2742 "cannot be named" error during the DTS build.
20
+ */
21
+ declare function useCacheData<T = any>(key: string | any[]): Ref<T | null>;
22
+
23
+ type MaybeGetter<T> = T | (() => T);
24
+ interface SmartCacheReturn<T> {
25
+ data: ShallowRef<T | null>;
26
+ isLoading: Ref<boolean>;
27
+ isFetching: Ref<boolean>;
28
+ error: Ref<Error | null>;
29
+ refresh: (manualOptions?: any) => Promise<void>;
30
+ }
31
+
32
+ declare function useSmartCache<T = any>(key: string | any[] | (() => any[]), fetcher: () => Promise<T>, options?: MaybeGetter<SmartCacheOptions>): SmartCacheReturn<T>;
33
+
34
+ export { createCacheBrain, useCacheData, useCacheInternal, useSmartCache };
@@ -0,0 +1,34 @@
1
+ import { CacheBrainClient } from '@pixel-pulse/cache-brain';
2
+ export { CacheBrainClient, cacheEngine, normalizeKey } from '@pixel-pulse/cache-brain';
3
+ import { App, DeepReadonly, Ref, ShallowRef } from 'vue';
4
+ import { SmartCacheOptions } from '@pixel-pulse/cache-brain/types';
5
+
6
+ interface CacheBrainContext {
7
+ client: CacheBrainClient;
8
+ state: DeepReadonly<{
9
+ entries: any[];
10
+ }>;
11
+ }
12
+ declare const createCacheBrain: (client: CacheBrainClient) => {
13
+ install(app: App): void;
14
+ };
15
+ declare const useCacheInternal: () => CacheBrainContext;
16
+
17
+ /**
18
+ * Explicitly defining the return type as Ref<T | null>
19
+ * prevents the TS2742 "cannot be named" error during the DTS build.
20
+ */
21
+ declare function useCacheData<T = any>(key: string | any[]): Ref<T | null>;
22
+
23
+ type MaybeGetter<T> = T | (() => T);
24
+ interface SmartCacheReturn<T> {
25
+ data: ShallowRef<T | null>;
26
+ isLoading: Ref<boolean>;
27
+ isFetching: Ref<boolean>;
28
+ error: Ref<Error | null>;
29
+ refresh: (manualOptions?: any) => Promise<void>;
30
+ }
31
+
32
+ declare function useSmartCache<T = any>(key: string | any[] | (() => any[]), fetcher: () => Promise<T>, options?: MaybeGetter<SmartCacheOptions>): SmartCacheReturn<T>;
33
+
34
+ export { createCacheBrain, useCacheData, useCacheInternal, useSmartCache };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import{cacheEngine as ie,normalizeKey as se}from"@pixel-pulse/cache-brain";import{inject as K,reactive as O,readonly as I}from"vue";import{cacheEngine as x}from"@pixel-pulse/cache-brain";var S=Symbol.for("cache-brain"),D=a=>{let o=O({entries:x.getValues()});return x.subscribe(r=>{o.entries=[...r]}),{install(r){r.provide(S,{client:a,state:I(o)})}}},m=()=>{let a=K(S);if(!a)throw new Error("CB: useSmartCache must be wrapped in a CacheBrain Provider.");return a};import{shallowRef as j,computed as T,watch as z,onUnmounted as G}from"vue";import{cacheEngine as C,normalizeKey as B}from"@pixel-pulse/cache-brain";function M(a){m();let o=T(()=>Array.isArray(a)?a:[String(a)]),r=T(()=>B(o.value)),l=j(C.get(o.value)||null),n=C.subscribe(s=>{let c=s.find(u=>B(u.key)===r.value);c?c.data!==l.value&&(l.value=c.data):l.value=null});return G(()=>{n()}),z(r,()=>{l.value=C.get(o.value)||null}),l}import{ref as v,shallowRef as q,computed as E,watch as L,onUnmounted as P,unref as d,triggerRef as U}from"vue";import{cacheEngine as g,normalizeKey as b,smartCache as _}from"@pixel-pulse/cache-brain";function F(a,o,r){let{client:l}=m(),n=q(null),s=v(!0),c=v(!1),u=v(null),h=E(()=>{let e=typeof a=="function"?a():d(a);return Array.isArray(e)?e.map(t=>d(t)):[String(d(e))]}),p=E(()=>b(h.value)),R=()=>typeof r=="function"?r():r||{},f=async e=>{let t={...R(),...e},i=g.get(h.value);if(t.strategy==="cache-first"&&i!==void 0){n.value=i,s.value=!1;return}n.value||(s.value=!0),c.value=!0,u.value=null;try{let y=await _(h.value,o,t);n.value=y,U(n)}catch(y){u.value=y}finally{s.value=!1,c.value=!1}},w=g.subscribe(e=>{let t=e.find(i=>b(i.key)===p.value);if(t){let i=Date.now()>t.expiry;t.data!==n.value&&(n.value=t.data,s.value=!1),i&&!c.value&&f()}}),A=g.onRevalidate(e=>{let t=!e,i=e?b(e):null;(t||i===p.value)&&f({strategy:"network-first"})});return L(p,(e,t)=>{e!==t&&f()},{immediate:!0}),P(()=>{w(),A()}),{data:n,isLoading:s,isFetching:c,error:u,refresh:f}}import{CacheBrainClient as he}from"@pixel-pulse/cache-brain";export{he as CacheBrainClient,ie as cacheEngine,D as createCacheBrain,se as normalizeKey,M as useCacheData,m as useCacheInternal,F as useSmartCache};
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@pixel-pulse/cache-brain-vue",
3
+ "version": "1.0.0",
4
+ "description": "Vue 3 & Nuxt.js adapter for Cache Brain engine.",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "peerDependencies": {
17
+ "vue": ">=3.0.0"
18
+ },
19
+ "devDependencies": {
20
+ "vue": "^3.4.0",
21
+ "tsup": "^8.0.0",
22
+ "typescript": "^5.0.0",
23
+ "@pixel-pulse/cache-brain": "1.0.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsup",
27
+ "dev": "tsup --watch"
28
+ }
29
+ }
@@ -0,0 +1,47 @@
1
+ import { shallowRef, computed, watch, onUnmounted, Ref } from "vue"; // 1. Added Ref import
2
+ import { cacheEngine, normalizeKey } from "@pixel-pulse/cache-brain";
3
+ import { useCacheInternal } from "../plugin";
4
+ import { CacheEntry } from "@pixel-pulse/cache-brain/types";
5
+
6
+ /**
7
+ * Explicitly defining the return type as Ref<T | null>
8
+ * prevents the TS2742 "cannot be named" error during the DTS build.
9
+ */
10
+ export function useCacheData<T = any>(key: string | any[]): Ref<T | null> {
11
+ useCacheInternal();
12
+
13
+ const queryKey = computed(() => {
14
+ return Array.isArray(key) ? key : [String(key)];
15
+ });
16
+
17
+ const cacheKey = computed(() => normalizeKey(queryKey.value));
18
+
19
+ const data = shallowRef<T | null>(
20
+ (cacheEngine.get(queryKey.value) as T) || null,
21
+ );
22
+
23
+ // 2. Explicitly type 'allEntries' and 'e' to satisfy strict builds
24
+ const unsubscribe = cacheEngine.subscribe((allEntries: any[]) => {
25
+ const match = (allEntries as CacheEntry<T>[]).find(
26
+ (e: CacheEntry<T>) => normalizeKey(e.key) === cacheKey.value,
27
+ );
28
+
29
+ if (match) {
30
+ if (match.data !== data.value) {
31
+ data.value = match.data as T;
32
+ }
33
+ } else {
34
+ data.value = null;
35
+ }
36
+ });
37
+
38
+ onUnmounted(() => {
39
+ unsubscribe();
40
+ });
41
+
42
+ watch(cacheKey, () => {
43
+ data.value = (cacheEngine.get(queryKey.value) as T) || null;
44
+ });
45
+
46
+ return data;
47
+ }
@@ -0,0 +1,131 @@
1
+ import {
2
+ ref,
3
+ shallowRef,
4
+ computed,
5
+ watch,
6
+ onUnmounted,
7
+ unref,
8
+ triggerRef,
9
+ } from "vue";
10
+ import {
11
+ cacheEngine,
12
+ normalizeKey,
13
+ smartCache,
14
+ } from "@pixel-pulse/cache-brain";
15
+ import type { SmartCacheOptions } from "@pixel-pulse/cache-brain/types";
16
+ import { MaybeGetter, SmartCacheReturn } from "@/types";
17
+ import { useCacheInternal } from "@/plugin";
18
+
19
+ export function useSmartCache<T = any>(
20
+ key: string | any[] | (() => any[]),
21
+ fetcher: () => Promise<T>,
22
+ options?: MaybeGetter<SmartCacheOptions>,
23
+ ): SmartCacheReturn<T> {
24
+ // 0. THE GUARD: This throws the error if plugin is not installed
25
+ const { client } = useCacheInternal();
26
+
27
+ // 1. Reactive State
28
+ const data = shallowRef<T | null>(null);
29
+ const isLoading = ref(true);
30
+ const isFetching = ref(false);
31
+ const error = ref<Error | null>(null);
32
+
33
+ // 2. Compute the queryKey and cacheKey reactively
34
+ const queryKey = computed(() => {
35
+ const k = typeof key === "function" ? key() : unref(key);
36
+ return Array.isArray(k) ? k.map((item) => unref(item)) : [String(unref(k))];
37
+ });
38
+
39
+ const cacheKey = computed(() => normalizeKey(queryKey.value));
40
+
41
+ const resolveOptions = (): SmartCacheOptions => {
42
+ return typeof options === "function" ? options() : options || {};
43
+ };
44
+
45
+ // 3. The Core Refresh Logic
46
+ const refresh = async (manualOptions?: SmartCacheOptions) => {
47
+ const mergedOptions: SmartCacheOptions = {
48
+ ...resolveOptions(),
49
+ ...manualOptions,
50
+ };
51
+
52
+ const existing = cacheEngine.get(queryKey.value);
53
+
54
+ if (mergedOptions.strategy === "cache-first" && existing !== undefined) {
55
+ data.value = existing as T;
56
+ isLoading.value = false;
57
+ return;
58
+ }
59
+
60
+ if (!data.value) isLoading.value = true;
61
+
62
+ isFetching.value = true;
63
+ error.value = null;
64
+
65
+ try {
66
+ const result = await smartCache(queryKey.value, fetcher, mergedOptions);
67
+ data.value = result as T;
68
+ triggerRef(data);
69
+ } catch (err) {
70
+ error.value = err as Error;
71
+ } finally {
72
+ isLoading.value = false;
73
+ isFetching.value = false;
74
+ }
75
+ };
76
+
77
+ // 4. Global Subscriptions
78
+ const unsubscribeSync = cacheEngine.subscribe((allEntries) => {
79
+ const entry = allEntries.find(
80
+ (e) => normalizeKey(e.key) === cacheKey.value,
81
+ );
82
+
83
+ if (entry) {
84
+ const isExpired = Date.now() > entry.expiry;
85
+
86
+ if (entry.data !== data.value) {
87
+ data.value = entry.data;
88
+ isLoading.value = false;
89
+ }
90
+
91
+ if (isExpired && !isFetching.value) {
92
+ refresh();
93
+ }
94
+ }
95
+ });
96
+
97
+ const unsubscribeRevalidate = cacheEngine.onRevalidate((invalidatedKey) => {
98
+ const isGlobal = !invalidatedKey;
99
+ const incomingKeyString = invalidatedKey
100
+ ? normalizeKey(invalidatedKey)
101
+ : null;
102
+
103
+ if (isGlobal || incomingKeyString === cacheKey.value) {
104
+ refresh({ strategy: "network-first" });
105
+ }
106
+ });
107
+
108
+ // 5. Watch for Key Changes
109
+ watch(
110
+ cacheKey,
111
+ (newVal, oldVal) => {
112
+ if (newVal !== oldVal) {
113
+ refresh();
114
+ }
115
+ },
116
+ { immediate: true },
117
+ );
118
+
119
+ onUnmounted(() => {
120
+ unsubscribeSync();
121
+ unsubscribeRevalidate();
122
+ });
123
+
124
+ return {
125
+ data,
126
+ isLoading,
127
+ isFetching,
128
+ error,
129
+ refresh,
130
+ };
131
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export { cacheEngine, normalizeKey } from "@pixel-pulse/cache-brain";
2
+
3
+ // 1. The Vue Plugin (for app.use(createCacheBrain()))
4
+ export { createCacheBrain, useCacheInternal } from "./plugin";
5
+
6
+ // 2. Reactive Composables (The "Hooks")
7
+ export { useCacheData } from "./composables/useCacheData";
8
+
9
+ export { useSmartCache } from "./composables/useSmartCache";
10
+
11
+ // 3. The Functional Client & Types
12
+ export { CacheBrainClient } from "@pixel-pulse/cache-brain";
package/src/plugin.ts ADDED
@@ -0,0 +1,47 @@
1
+ import {
2
+ inject,
3
+ InjectionKey,
4
+ App,
5
+ reactive,
6
+ readonly,
7
+ DeepReadonly,
8
+ } from "vue";
9
+ // Import from your core package
10
+ import { cacheEngine, CacheBrainClient } from "@pixel-pulse/cache-brain";
11
+
12
+ interface CacheBrainContext {
13
+ client: CacheBrainClient;
14
+ state: DeepReadonly<{ entries: any[] }>;
15
+ }
16
+
17
+ const CACHE_BRAIN_SYMBOL: InjectionKey<CacheBrainContext> =
18
+ Symbol.for("cache-brain");
19
+
20
+ export const createCacheBrain = (client: CacheBrainClient) => {
21
+ const state = reactive({
22
+ entries: cacheEngine.getValues(),
23
+ });
24
+
25
+ cacheEngine.subscribe((newData) => {
26
+ state.entries = [...newData];
27
+ });
28
+
29
+ return {
30
+ install(app: App) {
31
+ app.provide(CACHE_BRAIN_SYMBOL, {
32
+ client,
33
+ state: readonly(state),
34
+ });
35
+ },
36
+ };
37
+ };
38
+
39
+ export const useCacheInternal = () => {
40
+ const context = inject(CACHE_BRAIN_SYMBOL);
41
+ if (!context) {
42
+ throw new Error(
43
+ "CB: useSmartCache must be wrapped in a CacheBrain Provider.",
44
+ );
45
+ }
46
+ return context;
47
+ };
package/src/types.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { Ref, ShallowRef } from "vue";
2
+
3
+ export type MaybeGetter<T> = T | (() => T);
4
+
5
+ export interface SmartCacheReturn<T> {
6
+ data: ShallowRef<T | null>;
7
+ isLoading: Ref<boolean>;
8
+ isFetching: Ref<boolean>;
9
+ error: Ref<Error | null>;
10
+ refresh: (manualOptions?: any) => Promise<void>;
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "isolatedModules": true,
10
+ "esModuleInterop": true,
11
+ "paths": {
12
+ "@/*": ["./src/*"]
13
+ }
14
+ },
15
+ "include": ["src"]
16
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["esm", "cjs"],
6
+ dts: true,
7
+ minify: true,
8
+ clean: true,
9
+ external: ["vue", "@pixel-pulse/cache-brain"],
10
+ });