@duongtrungnguyen/next-helper
Version:
Helper library for Next.js 15
123 lines • 3.58 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cache_exports = {};
__export(cache_exports, {
queryCache: () => queryCache
});
module.exports = __toCommonJS(cache_exports);
class QueryCache {
constructor() {
this.cache = /* @__PURE__ */ new Map();
}
getKeyString(queryKey) {
return JSON.stringify(queryKey);
}
get(queryKey) {
const keyString = this.getKeyString(queryKey);
const entry = this.cache.get(keyString);
if (!entry) {
return void 0;
}
if (this.isStale(queryKey)) {
this.cache.delete(keyString);
return void 0;
}
return entry.data;
}
set(queryKey, data, staleTime = 5 * 60 * 1e3, cacheTime = 30 * 60 * 1e3) {
const keyString = this.getKeyString(queryKey);
const entry = this.cache.get(keyString);
if (entry) {
entry.data = data;
entry.timestamp = Date.now();
entry.staleTime = staleTime;
entry.cacheTime = cacheTime;
entry.subscribers.forEach((callback) => callback());
} else {
this.cache.set(keyString, {
data,
timestamp: Date.now(),
staleTime,
cacheTime,
subscribers: /* @__PURE__ */ new Set()
});
}
}
isStale(queryKey) {
const keyString = this.getKeyString(queryKey);
const entry = this.cache.get(keyString);
if (!entry) {
return true;
}
const now = Date.now();
return now - entry.timestamp > entry.staleTime;
}
subscribe(queryKey, callback) {
const keyString = this.getKeyString(queryKey);
let entry = this.cache.get(keyString);
if (!entry) {
entry = {
data: void 0,
timestamp: 0,
staleTime: 0,
cacheTime: 0,
subscribers: /* @__PURE__ */ new Set()
};
this.cache.set(keyString, entry);
}
entry.subscribers.add(callback);
return () => {
const currentEntry = this.cache.get(keyString);
if (currentEntry) {
currentEntry.subscribers.delete(callback);
}
};
}
invalidate(queryKey) {
const keyString = this.getKeyString(queryKey);
const entry = this.cache.get(keyString);
if (entry) {
entry.timestamp = 0;
entry.subscribers.forEach((callback) => callback());
}
}
remove(queryKey) {
const keyString = this.getKeyString(queryKey);
this.cache.delete(keyString);
}
clear() {
this.cache.clear();
}
getMatchingKeys(queryKey) {
const prefix = this.getKeyString(queryKey);
const matchingKeys = [];
this.cache.forEach((_, key) => {
if (key.startsWith(prefix)) {
matchingKeys.push(JSON.parse(key));
}
});
return matchingKeys;
}
}
const queryCache = new QueryCache();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
queryCache
});
//# sourceMappingURL=cache.js.map