3
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
* Code distributed by Google as part of the polymer project is also
8
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
14
import {Resolver, Deferred} from './resolver';
17
* A FileLoader lets you resolve URLs with a set of potential resolvers.
19
export class FileLoader {
20
resolvers: Resolver[] = [];
22
// map url -> Deferred
23
requests: {[url:string]: Deferred<string>} = {};
26
* Add an instance of a Resolver class to the list of url resolvers
28
* Ordering of resolvers is most to least recently added
29
* The first resolver to "accept" the url wins.
30
* @param {Resolver} resolver The resolver to add.
32
addResolver(resolver:Resolver) {
33
this.resolvers.push(resolver);
37
* Return a promise for an absolute url
39
* Url requests are deduplicated by the loader, returning the same Promise for
42
* @param {string} url The absolute url to request.
43
* @return {Promise.<string>} A promise that resolves to the contents of the URL.
46
var promise: Promise<string>;
48
if (!(uri in this.requests)) {
50
var deferred = new Deferred<string>();
51
this.requests[uri] = deferred;
53
// loop backwards through resolvers until one "accepts" the request
54
for (var i = this.resolvers.length - 1; i >= 0; i--) {
55
const r = this.resolvers[i];
56
if (r.accept(uri, deferred)) {
63
deferred.reject(new Error('no resolver found for ' + uri));
66
promise = deferred.promise;
68
promise = this.requests[uri].promise;