When a single resource is preloaded twice in ngtsc, the second request would be recognized as in-flight in which case `undefined` would be returned, which signals to the compilation that is can resume synchronously. The compilation would then proceed immediately and call `load`, only to find out that the request is still in-flight which is not allowed. This commit caches the Promise of the in-flight fetch requests, such that subsequent preload requests can return the corresponding Promise instance. PR Close #27357
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import {ResourceLoader} from './annotations';
|
|
|
|
/**
|
|
* `ResourceLoader` which delegates to a `CompilerHost` resource loading method.
|
|
*/
|
|
export class HostResourceLoader implements ResourceLoader {
|
|
private cache = new Map<string, string>();
|
|
private fetching = new Map<string, Promise<void>>();
|
|
|
|
constructor(private host: (url: string) => string | Promise<string>) {}
|
|
|
|
preload(url: string): Promise<void>|undefined {
|
|
if (this.cache.has(url)) {
|
|
return undefined;
|
|
} else if (this.fetching.has(url)) {
|
|
return this.fetching.get(url);
|
|
}
|
|
|
|
const result = this.host(url);
|
|
if (typeof result === 'string') {
|
|
this.cache.set(url, result);
|
|
return undefined;
|
|
} else {
|
|
const fetchCompletion = result.then(str => {
|
|
this.fetching.delete(url);
|
|
this.cache.set(url, str);
|
|
});
|
|
this.fetching.set(url, fetchCompletion);
|
|
return fetchCompletion;
|
|
}
|
|
}
|
|
|
|
load(url: string): string {
|
|
if (this.cache.has(url)) {
|
|
return this.cache.get(url) !;
|
|
}
|
|
|
|
const result = this.host(url);
|
|
if (typeof result !== 'string') {
|
|
throw new Error(`HostResourceLoader: host(${url}) returned a Promise`);
|
|
}
|
|
this.cache.set(url, result);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `ResourceLoader` which directly uses the filesystem to resolve resources synchronously.
|
|
*/
|
|
export class FileResourceLoader implements ResourceLoader {
|
|
load(url: string): string { return fs.readFileSync(url, 'utf8'); }
|
|
}
|