|
| 1 | +import type { ApplicationContext } from "../../app/context.ts"; |
| 2 | +import { fetchCatalogIconBlobUrl } from "../plugins/icon-loader.ts"; |
| 3 | + |
| 4 | +type IconRequest = { |
| 5 | + controller: AbortController; |
| 6 | + timeout: ReturnType<typeof setTimeout>; |
| 7 | +}; |
| 8 | + |
| 9 | +export class ModelSetupIconLoader { |
| 10 | + private urls: Record<string, string> = {}; |
| 11 | + private readonly misses = new Set<string>(); |
| 12 | + private readonly requests = new Map<string, IconRequest>(); |
| 13 | + |
| 14 | + constructor( |
| 15 | + private readonly getContext: () => ApplicationContext, |
| 16 | + private readonly isEligible: (iconUrl: string) => boolean, |
| 17 | + private readonly onChange: (urls: Record<string, string>) => void, |
| 18 | + ) {} |
| 19 | + |
| 20 | + reconcile(eligible: ReadonlySet<string>): void { |
| 21 | + const nextUrls = { ...this.urls }; |
| 22 | + let changed = false; |
| 23 | + for (const [iconUrl, blobUrl] of Object.entries(nextUrls)) { |
| 24 | + if (!eligible.has(iconUrl)) { |
| 25 | + URL.revokeObjectURL(blobUrl); |
| 26 | + delete nextUrls[iconUrl]; |
| 27 | + changed = true; |
| 28 | + } |
| 29 | + } |
| 30 | + if (changed) { |
| 31 | + this.publish(nextUrls); |
| 32 | + } |
| 33 | + for (const [iconUrl, request] of this.requests) { |
| 34 | + if (!eligible.has(iconUrl)) { |
| 35 | + clearTimeout(request.timeout); |
| 36 | + request.controller.abort(); |
| 37 | + this.requests.delete(iconUrl); |
| 38 | + } |
| 39 | + } |
| 40 | + for (const iconUrl of this.misses) { |
| 41 | + if (!eligible.has(iconUrl)) { |
| 42 | + this.misses.delete(iconUrl); |
| 43 | + } |
| 44 | + } |
| 45 | + for (const iconUrl of eligible) { |
| 46 | + if (!this.urls[iconUrl] && !this.misses.has(iconUrl) && !this.requests.has(iconUrl)) { |
| 47 | + this.fetch(iconUrl); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + invalidate(iconUrl: string): void { |
| 53 | + const request = this.requests.get(iconUrl); |
| 54 | + if (request) { |
| 55 | + clearTimeout(request.timeout); |
| 56 | + request.controller.abort(); |
| 57 | + this.requests.delete(iconUrl); |
| 58 | + } |
| 59 | + const blobUrl = this.urls[iconUrl]; |
| 60 | + if (blobUrl) { |
| 61 | + URL.revokeObjectURL(blobUrl); |
| 62 | + } |
| 63 | + const nextUrls = { ...this.urls }; |
| 64 | + delete nextUrls[iconUrl]; |
| 65 | + this.publish(nextUrls); |
| 66 | + this.misses.add(iconUrl); |
| 67 | + } |
| 68 | + |
| 69 | + reset(): void { |
| 70 | + for (const request of this.requests.values()) { |
| 71 | + clearTimeout(request.timeout); |
| 72 | + request.controller.abort(); |
| 73 | + } |
| 74 | + for (const blobUrl of Object.values(this.urls)) { |
| 75 | + URL.revokeObjectURL(blobUrl); |
| 76 | + } |
| 77 | + this.requests.clear(); |
| 78 | + this.misses.clear(); |
| 79 | + this.publish({}); |
| 80 | + } |
| 81 | + |
| 82 | + private fetch(iconUrl: string): void { |
| 83 | + const controller = new AbortController(); |
| 84 | + const timeout = setTimeout( |
| 85 | + () => controller.abort(new DOMException("catalog icon fetch timed out", "TimeoutError")), |
| 86 | + 10_000, |
| 87 | + ); |
| 88 | + const request = { controller, timeout }; |
| 89 | + this.requests.set(iconUrl, request); |
| 90 | + const context = this.getContext(); |
| 91 | + void fetchCatalogIconBlobUrl({ |
| 92 | + iconUrl, |
| 93 | + basePath: context.basePath, |
| 94 | + gatewayUrl: context.gateway.connection.gatewayUrl, |
| 95 | + auth: { |
| 96 | + hello: context.gateway.snapshot.hello, |
| 97 | + settings: { token: context.gateway.connection.token }, |
| 98 | + password: context.gateway.connection.password, |
| 99 | + }, |
| 100 | + signal: controller.signal, |
| 101 | + }) |
| 102 | + .then((blobUrl) => { |
| 103 | + if ( |
| 104 | + this.requests.get(iconUrl) !== request || |
| 105 | + this.getContext().gateway.snapshot.phase !== "connected" || |
| 106 | + !this.isEligible(iconUrl) |
| 107 | + ) { |
| 108 | + if (blobUrl) { |
| 109 | + URL.revokeObjectURL(blobUrl); |
| 110 | + } |
| 111 | + return; |
| 112 | + } |
| 113 | + if (blobUrl) { |
| 114 | + this.publish({ ...this.urls, [iconUrl]: blobUrl }); |
| 115 | + } else { |
| 116 | + this.misses.add(iconUrl); |
| 117 | + } |
| 118 | + }) |
| 119 | + .catch(() => { |
| 120 | + if (this.requests.get(iconUrl) === request) { |
| 121 | + this.misses.add(iconUrl); |
| 122 | + } |
| 123 | + }) |
| 124 | + .finally(() => { |
| 125 | + clearTimeout(timeout); |
| 126 | + if (this.requests.get(iconUrl) === request) { |
| 127 | + this.requests.delete(iconUrl); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + private publish(urls: Record<string, string>): void { |
| 133 | + this.urls = urls; |
| 134 | + this.onChange(urls); |
| 135 | + } |
| 136 | +} |
0 commit comments