mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
25 lines
789 B
TypeScript
25 lines
789 B
TypeScript
import type { ResourceIdentifier } from '../http/representation/ResourceIdentifier';
|
|
import type { ResourceSet } from './ResourceSet';
|
|
|
|
/**
|
|
* Caches resource existence in a `WeakMap` tied to the `ResourceIdentifier` object.
|
|
*/
|
|
export class CachedResourceSet implements ResourceSet {
|
|
private readonly source: ResourceSet;
|
|
private readonly cache: WeakMap<ResourceIdentifier, boolean>;
|
|
|
|
public constructor(source: ResourceSet) {
|
|
this.source = source;
|
|
this.cache = new WeakMap();
|
|
}
|
|
|
|
public async hasResource(identifier: ResourceIdentifier): Promise<boolean> {
|
|
if (this.cache.has(identifier)) {
|
|
return this.cache.get(identifier)!;
|
|
}
|
|
const result = await this.source.hasResource(identifier);
|
|
this.cache.set(identifier, result);
|
|
return result;
|
|
}
|
|
}
|