ng-http-caching
Version:
Cache for HTTP requests in Angular application.
99 lines (95 loc) • 3.32 kB
JavaScript
import * as i0 from '@angular/core';
import { inject, Injectable } from '@angular/core';
import { NG_HTTP_CACHING_NG_SIMPLE_STATE_CONFIG, NgHttpCachingNgSimpleStateSentinel } from 'ng-http-caching';
import { NgSimpleStateBaseSignalStore } from 'ng-simple-state';
/**
* Storage adapter for ng-http-caching backed by ng-simple-state.
*
* This allows a single source of truth for HTTP cache entries.
*/
class NgHttpCachingNgSimpleStateAdapter extends NgSimpleStateBaseSignalStore {
storeConfig() {
const userConfig = inject(NG_HTTP_CACHING_NG_SIMPLE_STATE_CONFIG, { optional: true });
return {
storeName: userConfig?.storeName ?? 'NgHttpCaching',
...userConfig,
};
}
initialState() {
return { entries: {} };
}
get size() {
return Object.keys(this.getCurrentState().entries).length;
}
clear() {
this.restartState();
}
delete(key) {
if (!key) {
return false;
}
const current = this.getCurrentState();
if (!Object.prototype.hasOwnProperty.call(current.entries, key)) {
return false;
}
const nextEntries = { ...current.entries };
delete nextEntries[key];
const next = {
...current,
entries: nextEntries
};
this.replaceState(next, `ngHttpCaching:delete`);
return true;
}
forEach(callbackfn) {
const entries = this.getCurrentState().entries;
Object.keys(entries).forEach((key) => {
callbackfn(entries[key], key);
});
}
get(key) {
if (!key) {
return undefined;
}
return this.getCurrentState().entries[key];
}
has(key) {
if (!key) {
return false;
}
return Object.prototype.hasOwnProperty.call(this.getCurrentState().entries, key);
}
set(key, value) {
if (!key) {
return;
}
const current = this.getCurrentState();
const next = {
...current,
entries: {
...current.entries,
[key]: value
}
};
this.replaceState(next, `ngHttpCaching:set`);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: NgHttpCachingNgSimpleStateAdapter, deps: null, target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: NgHttpCachingNgSimpleStateAdapter, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: NgHttpCachingNgSimpleStateAdapter, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}] });
/**
* Factory helper to enable the ng-simple-state adapter for ng-http-caching.
*/
function withNgHttpCachingNgSimpleState(config) {
return new NgHttpCachingNgSimpleStateSentinel(NgHttpCachingNgSimpleStateAdapter, config);
}
/**
* Generated bundle index. Do not edit.
*/
export { NgHttpCachingNgSimpleStateAdapter, withNgHttpCachingNgSimpleState };
//# sourceMappingURL=ng-http-caching-ng-simple-state.mjs.map