1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | class JSResourceImpl {
|
6 | constructor(loader) {
|
7 | this._loader = loader;
|
8 | this._result = null;
|
9 | this._error = null;
|
10 | this._promise = null;
|
11 | }
|
12 | load() {
|
13 | let promise = this._promise;
|
14 | if (promise == null) {
|
15 | promise = this._loader().then((result) => {
|
16 | this._result = result;
|
17 | return result;
|
18 | }, (error) => {
|
19 | this._error = error;
|
20 | throw error;
|
21 | });
|
22 | this._promise = promise;
|
23 | }
|
24 | return promise;
|
25 | }
|
26 | get() {
|
27 | if (this._result != null) {
|
28 | return this._result;
|
29 | }
|
30 | return undefined;
|
31 | }
|
32 | getModuleIfRequired() {
|
33 | return this.get();
|
34 | }
|
35 | read() {
|
36 | if (this._result != null) {
|
37 | return this._result;
|
38 | }
|
39 | else if (this._error != null) {
|
40 | throw this._error;
|
41 | }
|
42 | else {
|
43 | throw this._promise;
|
44 | }
|
45 | }
|
46 | }
|
47 | const resourceMap = new Map();
|
48 | function JSResource(moduleId, loader) {
|
49 | let resource = resourceMap.get(moduleId);
|
50 | if (resource == null) {
|
51 | resource = new JSResourceImpl(loader);
|
52 | resourceMap.set(moduleId, resource);
|
53 | }
|
54 | return resource;
|
55 | }
|
56 |
|
57 | exports.JSResource = JSResource;
|