| 259 | } |
| 260 | |
| 261 | requestContentData(): Promise<TextUtils.ContentData.ContentDataOrError> { |
| 262 | if (!this.#contentPromise) { |
| 263 | const fileSizeToCache = 65535; // We won't bother cacheing files under 64K |
| 264 | if (this.hash && !this.#isLiveEdit && this.contentLength > fileSizeToCache) { |
| 265 | // For large files that aren't live edits and have a hash, we keep a content-addressed cache |
| 266 | // so we don't need to load multiple copies or disassemble wasm modules multiple times. |
| 267 | if (!scriptCacheInstance) { |
| 268 | // Initialize script cache singleton. Add a finalizer for removing keys from the map. |
| 269 | scriptCacheInstance = { |
| 270 | cache: new Map(), |
| 271 | registry: new FinalizationRegistry(hashCode => scriptCacheInstance?.cache.delete(hashCode)), |
| 272 | }; |
| 273 | } |
| 274 | // This key should be sufficient to identify scripts that are known to have the same content. |
| 275 | const fullHash = [ |
| 276 | this.#language, |
| 277 | this.contentLength, |
| 278 | this.lineOffset, |
| 279 | this.columnOffset, |
| 280 | this.endLine, |
| 281 | this.endColumn, |
| 282 | this.#codeOffset, |
| 283 | this.hash, |
| 284 | ].join(':'); |
| 285 | const cachedContentPromise = scriptCacheInstance.cache.get(fullHash)?.deref(); |
| 286 | if (cachedContentPromise) { |
| 287 | this.#contentPromise = cachedContentPromise; |
| 288 | } else { |
| 289 | this.#contentPromise = this.#requestContent(); |
| 290 | scriptCacheInstance.cache.set(fullHash, new WeakRef(this.#contentPromise)); |
| 291 | scriptCacheInstance.registry.register(this.#contentPromise, fullHash); |
| 292 | } |
| 293 | } else { |
| 294 | this.#contentPromise = this.#requestContent(); |
| 295 | } |
| 296 | } |
| 297 | return this.#contentPromise; |
| 298 | } |
| 299 | |
| 300 | async #requestContent(): Promise<TextUtils.ContentData.ContentDataOrError> { |
| 301 | if (!this.scriptId) { |