MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / Cached

Class Cached

src/client/sandbox.mjs:438–457  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

436}
437
438class Cached {
439 constructor(mapping) {
440 this.mapping = mapping
441 this.work = Object.create(null)
442 this.done = Object.create(null)
443 }
444
445 compute(value) {
446 return this.work[value] || (this.work[value] = this.mapping(value).then(result => this.done[value] = result))
447 }
448
449 store(value, result) {
450 this.work[value] = Promise.resolve(result)
451 return this.done[value] = result
452 }
453
454 get(value) {
455 return this.done[value]
456 }
457}
458
459// Cache for loaded code and resolved unpkg redirects
460const resolved = new Cached(name => fetch("https://unpkg.com/" + name.replace(/\/$/, "")).then(resp => {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected