2025-03-21 21:34:26 +08:00

200 lines
5.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
__publicField
} from "./chunk-V6TY7KAL.js";
// ../../node_modules/.pnpm/@kevisual+cache@0.0.1_rollup@4.36.0_tslib@2.8.1_typescript@5.8.2/node_modules/@kevisual/cache/dist/cache.js
function promisifyRequest(request) {
return new Promise((resolve, reject) => {
request.oncomplete = request.onsuccess = () => resolve(request.result);
request.onabort = request.onerror = () => reject(request.error);
});
}
function createStore(dbName, storeName) {
const request = indexedDB.open(dbName);
request.onupgradeneeded = () => request.result.createObjectStore(storeName);
const dbp = promisifyRequest(request);
return (txMode, callback) => dbp.then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName)));
}
var defaultGetStoreFunc;
function defaultGetStore() {
if (!defaultGetStoreFunc) {
defaultGetStoreFunc = createStore("keyval-store", "keyval");
}
return defaultGetStoreFunc;
}
function get(key, customStore = defaultGetStore()) {
return customStore("readonly", (store) => promisifyRequest(store.get(key)));
}
function set(key, value, customStore = defaultGetStore()) {
return customStore("readwrite", (store) => {
store.put(value, key);
return promisifyRequest(store.transaction);
});
}
function setMany(entries2, customStore = defaultGetStore()) {
return customStore("readwrite", (store) => {
entries2.forEach((entry) => store.put(entry[1], entry[0]));
return promisifyRequest(store.transaction);
});
}
function getMany(keys2, customStore = defaultGetStore()) {
return customStore("readonly", (store) => Promise.all(keys2.map((key) => promisifyRequest(store.get(key)))));
}
function update(key, updater, customStore = defaultGetStore()) {
return customStore("readwrite", (store) => (
// Need to create the promise manually.
// If I try to chain promises, the transaction closes in browsers
// that use a promise polyfill (IE10/11).
new Promise((resolve, reject) => {
store.get(key).onsuccess = function() {
try {
store.put(updater(this.result), key);
resolve(promisifyRequest(store.transaction));
} catch (err) {
reject(err);
}
};
})
));
}
function del(key, customStore = defaultGetStore()) {
return customStore("readwrite", (store) => {
store.delete(key);
return promisifyRequest(store.transaction);
});
}
function delMany(keys2, customStore = defaultGetStore()) {
return customStore("readwrite", (store) => {
keys2.forEach((key) => store.delete(key));
return promisifyRequest(store.transaction);
});
}
function clear(customStore = defaultGetStore()) {
return customStore("readwrite", (store) => {
store.clear();
return promisifyRequest(store.transaction);
});
}
function eachCursor(store, callback) {
store.openCursor().onsuccess = function() {
if (!this.result)
return;
callback(this.result);
this.result.continue();
};
return promisifyRequest(store.transaction);
}
function keys(customStore = defaultGetStore()) {
return customStore("readonly", (store) => {
if (store.getAllKeys) {
return promisifyRequest(store.getAllKeys());
}
const items = [];
return eachCursor(store, (cursor) => items.push(cursor.key)).then(() => items);
});
}
function values(customStore = defaultGetStore()) {
return customStore("readonly", (store) => {
if (store.getAll) {
return promisifyRequest(store.getAll());
}
const items = [];
return eachCursor(store, (cursor) => items.push(cursor.value)).then(() => items);
});
}
function entries(customStore = defaultGetStore()) {
return customStore("readonly", (store) => {
if (store.getAll && store.getAllKeys) {
return Promise.all([
promisifyRequest(store.getAllKeys()),
promisifyRequest(store.getAll())
]).then(([keys2, values2]) => keys2.map((key, i) => [key, values2[i]]));
}
const items = [];
return customStore("readonly", (store2) => eachCursor(store2, (cursor) => items.push([cursor.key, cursor.value])).then(() => items));
});
}
var idb = Object.freeze({
__proto__: null,
clear,
createStore,
del,
delMany,
entries,
get,
getMany,
keys,
promisifyRequest,
set,
setMany,
update,
values
});
var CacheWorkspace = class {
constructor() {
__publicField(this, "storage");
__publicField(this, "data", null);
this.storage = idb;
}
async get(name) {
const data = await get(name);
this.data = data;
return data;
}
async set(name, data) {
this.data = data;
await set(name, data);
}
async del(name) {
this.data = null;
await del(name);
}
/**
* Clear all values in the store.
*/
async clear() {
await clear();
}
};
var MyCache = class extends CacheWorkspace {
constructor(name) {
super();
__publicField(this, "name");
__publicField(this, "updatedAt");
this.name = name || "my-cache";
}
async getData() {
const cache = await super.get(this.name);
this.updatedAt = cache.updatedAt;
if (cache.expireTime && cache.expireTime < Date.now()) {
await super.del(this.name);
return {};
}
return cache.data;
}
/**
* 设置缓存数据默认过期时间为10天
* @param data
* @param opts
*/
async setData(data, opts) {
const now = Date.now();
const expireTime = now + ((opts == null ? void 0 : opts.expireTime) || 1e3 * 60 * 60 * 24 * 10);
const newData = {
data,
updatedAt: Date.now(),
expireTime
};
await super.set(this.name, newData);
this.updatedAt = newData.updatedAt;
}
async del() {
await super.del(this.name);
}
};
export {
CacheWorkspace,
MyCache
};
//# sourceMappingURL=@kevisual_cache.js.map