-
Notifications
You must be signed in to change notification settings - Fork 215
/
module-reference.js
93 lines (83 loc) · 2.5 KB
/
module-reference.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var Montage = require("./core").Montage;
/**
* @class ModuleReference
* @extends Montage
*/
exports.ModuleReference = Montage.specialize( /** @lends ModuleReference */ {
constructor: {
value: function ModuleReference() {
return this.super();
}
},
initWithIdAndRequire: {
value: function (id, require) {
if (!id || ! require) {
throw new Error("Module ID and require required");
}
this.id = id;
this.require = require;
return this;
}
},
/**
* The absolute id of the module within the `require` package
* @type {string}
*/
id: {
value: null
},
/**
* The require of a package.
* @type {function}
*/
require: {
value: null
},
_exports: {
value: null
},
/**
* A promise for the exports of the module. The exports are loaded lazily
* when the property is accessed.
* @type {Promise.<Object>} The exports of the module.
*/
exports: {
get: function () {
if (this._exports) {
return this._exports;
}
return this._exports = this.require.async(this.id);
}
},
/**
* Resolves this module reference so that it can be required from
* otherRequire.
* @method
* @param {function} otherRequire Require from another package that has
* the package of this module as a dependency.
* @returns {string} The module id to pass to otherRequire that results
* in this module.
* @throws {Error} If there is no mapping from this require inside
* otherRequire.
* @example
* var ref = new ModuleReference().initWithIdAndRequire("core/uuid", montageRequire);
* ref.resolve(applicationRequire); // => "montage/core/uuid"
* @example
* var ref = new ModuleReference().initWithIdAndRequire("ui/main.reel", applicationRequire);
* ref.resolve(montageRequire); // => Error
* // because there is no module id such that montageRequire(id) can
* // return the module from inside your application
*/
resolve: {
value: function (otherRequire) {
return otherRequire.identify(this.id, this.require);
}
},
// Used for cross-frame detection, similar to Array.isArray, but just
// a property as there's no need for complex logic.
isModuleReference: {
writable: false,
configurable: false,
value: true
}
});