@modern-js/runtime-utils
Version:
A Progressive React Framework for modern web development.
55 lines (54 loc) • 1.46 kB
JavaScript
import "node:module";
class Storage {
async keys() {
const _keys = [];
this.forEach?.((_, k)=>{
_keys.push(k);
});
return _keys;
}
async values() {
const _values = [];
this.forEach?.((v)=>{
_values.push(v);
});
return _values;
}
get(key) {
const uniqueKey = this.computedUniqueKey(key);
return this.container.get(uniqueKey);
}
async set(key, value) {
const uniqueKey = this.computedUniqueKey(key);
await this.container.set(uniqueKey, value);
return this;
}
has(key) {
const uniqueKey = this.computedUniqueKey(key);
return this.container.has(uniqueKey);
}
delete(key) {
const uniqueKey = this.computedUniqueKey(key);
return this.container.delete(uniqueKey);
}
async clear() {
const keys = await this.keys?.();
await Promise.all(keys?.map(async (key)=>this.container.delete(key)) || []);
}
forEach(fallbackFn) {
this.container.forEach?.((v, k)=>{
if (this.checkIsOwnkey(k)) fallbackFn(v, k, this);
});
}
computedUniqueKey(k) {
return `${this.namespace}:${k}`;
}
checkIsOwnkey(k) {
return k.startsWith(this.namespace);
}
constructor(namespace, container){
this.namespace = namespace;
this.container = container;
}
}
export { Storage };