-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathresolver.cache.js
More file actions
77 lines (69 loc) · 2.16 KB
/
resolver.cache.js
File metadata and controls
77 lines (69 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import abslog from 'abslog';
import assert from 'assert';
/**
* @typedef {object} PodletClientCacheResolverOptions
* @property {import('abslog').AbstractLoggerOptions} [logger]
*/
export default class PodletClientCacheResolver {
#registry;
#log;
/**
* @constructor
* @param {import('ttl-mem-cache').default} registry
* @param {PodletClientCacheResolverOptions} options
*/
constructor(registry, options = {}) {
assert(
registry,
'you must pass a "registry" object to the PodletClientCacheResolver constructor',
);
this.#registry = registry;
this.#log = abslog(options.logger);
}
/**
* Loads the podlet's manifest from cache if not stale
*
* @param {import('./http-outgoing.js').default} outgoing
* @returns {Promise<import('./http-outgoing.js').default>}
*/
load(outgoing) {
return new Promise((resolve) => {
if (outgoing.status !== 'stale') {
const cached = this.#registry.get(outgoing.name);
if (cached) {
outgoing.manifest = { ...cached };
outgoing.status = 'cached';
this.#log.debug(
`loaded manifest from cache - resource: ${outgoing.name}`,
);
}
}
resolve(outgoing);
});
}
/**
* Saves the podlet's manifest to the cache
*
* @param {import('./http-outgoing.js').default} outgoing
* @returns {Promise<import('./http-outgoing.js').default>}
*/
save(outgoing) {
return new Promise((resolve) => {
if (outgoing.status === 'fresh') {
this.#registry.set(
outgoing.name,
outgoing.manifest,
outgoing.maxAge,
);
this.#log.debug(
`saved manifest to cache - resource: ${outgoing.name}`,
);
}
outgoing.recursions++;
resolve(outgoing);
});
}
get [Symbol.toStringTag]() {
return 'PodletClientCacheResolver';
}
}