~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/hydrolysis/src/loader/file-loader.ts

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @license
 
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
 
9
 */
 
10
 
 
11
 
 
12
'use strict';
 
13
 
 
14
import {Resolver, Deferred} from './resolver';
 
15
 
 
16
/**
 
17
 * A FileLoader lets you resolve URLs with a set of potential resolvers.
 
18
 */
 
19
export class FileLoader {
 
20
  resolvers: Resolver[] = [];
 
21
 
 
22
  // map url -> Deferred
 
23
  requests: {[url:string]: Deferred<string>} = {};
 
24
 
 
25
  /**
 
26
   * Add an instance of a Resolver class to the list of url resolvers
 
27
   *
 
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.
 
31
   */
 
32
  addResolver(resolver:Resolver) {
 
33
    this.resolvers.push(resolver);
 
34
  };
 
35
 
 
36
  /**
 
37
   * Return a promise for an absolute url
 
38
   *
 
39
   * Url requests are deduplicated by the loader, returning the same Promise for
 
40
   * identical urls
 
41
   *
 
42
   * @param {string} url        The absolute url to request.
 
43
   * @return {Promise.<string>} A promise that resolves to the contents of the URL.
 
44
   */
 
45
  request(uri:string) {
 
46
    var promise: Promise<string>;
 
47
 
 
48
    if (!(uri in this.requests)) {
 
49
      var handled = false;
 
50
      var deferred = new Deferred<string>();
 
51
      this.requests[uri] = deferred;
 
52
 
 
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)) {
 
57
          handled = true;
 
58
          break;
 
59
        }
 
60
      }
 
61
 
 
62
      if (!handled) {
 
63
        deferred.reject(new Error('no resolver found for ' + uri));
 
64
      }
 
65
 
 
66
      promise = deferred.promise;
 
67
    } else {
 
68
      promise = this.requests[uri].promise;
 
69
    }
 
70
 
 
71
    return promise;
 
72
  }
 
73
};