UNPKG

2.79 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24class FailUrlLoader {
25 canLoad(_url) {
26 return true;
27 }
28 load(url) {
29 throw new Error(`${url} not known in InMemoryOverlayLoader`);
30 }
31}
32/**
33 * Resolves requests first from an in-memory map of file contents, and if a
34 * file isn't found there, defers to another url loader.
35 *
36 * Useful for the editor use case. An editor will have a number of files in open
37 * buffers at any time. For these files, the editor's in-memory buffer is
38 * canonical, so that their contents are read even when they have unsaved
39 * changes. For all other files, we can load the files using another loader,
40 * e.g. from disk.
41 *
42 * TODO(rictic): make this a mixin that mixes another loader.
43 */
44class InMemoryOverlayUrlLoader {
45 constructor(fallbackLoader) {
46 this.urlContentsMap = new Map();
47 this._fallbackLoader = fallbackLoader || new FailUrlLoader();
48 if (this._fallbackLoader.readDirectory) {
49 this.readDirectory =
50 this._fallbackLoader.readDirectory.bind(this._fallbackLoader);
51 }
52 }
53 canLoad(url) {
54 return this.urlContentsMap.has(url) || this._fallbackLoader.canLoad(url);
55 }
56 load(url) {
57 return __awaiter(this, void 0, void 0, function* () {
58 const contents = this.urlContentsMap.get(url);
59 if (typeof contents === 'string') {
60 return contents;
61 }
62 return this._fallbackLoader.load(url);
63 });
64 }
65}
66exports.InMemoryOverlayUrlLoader = InMemoryOverlayUrlLoader;
67//# sourceMappingURL=overlay-loader.js.map
\No newline at end of file