Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make it so that loadModule gets the loaded module as an argument, so …

…that modules that are actually async no longer have to have side effects or export to window.
  • Loading branch information...
commit ddfe0f1c011a9fd53d34dc812af0ff02728bbd76 1 parent 53f8f55
Malte Ubl cramforce authored
23 clients/module-client.js
@@ -38,20 +38,28 @@ function ModuleServer(urlPrefix, load, getUrl) {
38 38 };
39 39 }
40 40
41   - var ModuleServer = function(urlPrefix) {
  41 + var Server = function(urlPrefix) {
42 42 this.urlPrefix = urlPrefix;
43 43 this.requested = {};
44 44 this.requestedList = [];
45 45 this.loaded = {};
46 46 };
47 47
48   - ModuleServer.prototype.load = function(module, cb) {
  48 + Server.prototype.load = function(module, cb) {
49 49 var self = this;
50 50 module = 'module$' + module.replace(/\//g, '$');
51   - if (this.loaded[module]) {
52   - cb()
  51 + if (this.loaded[module] || ModuleServer.m[module]) {
  52 + if (cb) {
  53 + cb(ModuleServer.m[module]);
  54 + }
53 55 return;
54 56 }
  57 + var userCb = cb;
  58 + cb = function() {
  59 + if (userCb) {
  60 + userCb(ModuleServer.m[module]);
  61 + }
  62 + };
55 63 if (this.requested[module]) {
56 64 this.requested[module].push(cb);
57 65 return;
@@ -68,10 +76,13 @@ function ModuleServer(urlPrefix, load, getUrl) {
68 76 });
69 77 };
70 78
71   - var instance = new ModuleServer(urlPrefix);
  79 + var instance = new Server(urlPrefix);
72 80 return function(module, cb) {
73 81 instance.load(module, cb);
74 82 };
75   -};
  83 +}
  84 +
  85 +// Registry for loaded modules.
  86 +ModuleServer.m = {};
76 87
77 88 // var require = ModuleServer();
7 clients/test/demo.html
@@ -19,12 +19,13 @@
19 19 });
20 20 });
21 21
22   -loadModule('module/baz/foo', function() {
23   - log('loaded module/baz/foo');
  22 +loadModule('module/baz/foo', function(foo) {
  23 + log('loaded module/baz/foo ');
  24 + log('Call loaded function: ' + foo.foo());
24 25 });
25 26
26 27 function log(msg) {
27   - document.getElementById('log').textContent += msg + '\n';
  28 + document.getElementById('log').textContent += msg + '\n';
28 29 }
29 30
30 31 </script>
9 module-compiler/externs.js
@@ -25,6 +25,11 @@ var $LAB; // Part of the standard client.
25 25 * Loads a module and then calls the callback once the loaded code is
26 26 * available.
27 27 * @param {string} name Name of the module to load. e.g. 'my/app'
28   - * @param {function()} cb Callback to be called when the module loaded.
  28 + * @param {function(*)} cb Callback to be called when the module loaded.
  29 + * The callback retrieves the exports object of the loaded module as the 1st
  30 + * argument.
29 31 */
30   -window.loadModule = function(name, cb) {};
  32 +window.loadModule = function(name, cb) {};
  33 +
  34 +// Name of the default client
  35 +var ModuleServer;
4 module-server.js
@@ -71,6 +71,7 @@ function makeServer(graph, modules) {
71 71 }
72 72 var file = modules[name];
73 73 js += file.js + '\n';
  74 + js += 'ModuleServer.m.' + name + '=' + name + ';\n';
74 75 if (createSourceMap) {
75 76 var map = file.getMap();
76 77 map.sourceRoot = options.sourceMapSourceRootUrlPrefix;
@@ -81,7 +82,8 @@ function makeServer(graph, modules) {
81 82 },
82 83 map: map
83 84 });
84   - lineNumber = lineNumber + file.getNumberOfLines();
  85 + lineNumber = lineNumber + file.getNumberOfLines()
  86 + + 1 // The ModuleServer.m… line;
85 87 }
86 88 }
87 89 var sourceMap = createSourceMap ? {
2  test/fixtures/sample-module/graph.json
... ... @@ -1 +1 @@
1   -[{"inputs":["sub-app.js"],"transitive-requires":[],"name":"module$sub_app","provides":["module$sub_app"],"requires":[]},{"inputs":["module/bar.js"],"transitive-requires":[],"name":"module$module$bar","provides":["module$module$bar"],"requires":[]},{"inputs":["module/baz/foo.js"],"transitive-requires":[],"name":"module$module$baz$foo","provides":["module$module$baz$foo"],"requires":[]},{"inputs":["module/foo.js"],"transitive-requires":["module$module$baz$foo","module$module$bar"],"name":"module$module$foo","provides":["module$module$foo"],"requires":["module$module$bar","module$module$baz$foo"]},{"inputs":["module.js"],"transitive-requires":["module$module$foo","module$module$baz$foo","module$module$bar"],"name":"module$module","provides":["module$module"],"requires":["module$module$foo"]},{"inputs":["app.js"],"transitive-requires":["module$module","module$module$foo","module$module$baz$foo","module$sub_app","module$module$bar"],"name":"module$app","provides":["module$app"],"requires":["module$sub_app","module$module"]}]
  1 +[{"inputs":["sub-app.js"],"transitive-requires":[],"name":"module$sub_app","provides":["module$sub_app"],"requires":[]},{"inputs":["module/bar.js"],"transitive-requires":[],"name":"module$module$bar","provides":["module$module$bar"],"requires":[]},{"inputs":["module/baz/foo.js"],"transitive-requires":[],"name":"module$module$baz$foo","provides":["module$module$baz$foo"],"requires":[]},{"inputs":["module/foo.js"],"transitive-requires":["module$module$bar","module$module$baz$foo"],"name":"module$module$foo","provides":["module$module$foo"],"requires":["module$module$bar","module$module$baz$foo"]},{"inputs":["module.js"],"transitive-requires":["module$module$bar","module$module$baz$foo","module$module$foo"],"name":"module$module","provides":["module$module"],"requires":["module$module$foo"]},{"inputs":["app.js"],"transitive-requires":["module$module$bar","module$module$baz$foo","module$module$foo","module$module","module$sub_app"],"name":"module$app","provides":["module$app"],"requires":["module$sub_app","module$module"]}]
6 test/fixtures/sample-module/sub-app.js
... ... @@ -1,3 +1,9 @@
1 1 exports.subApp = function() {
2 2 return 'sub';
  3 +};
  4 +
  5 +exports.testLoad = function() {
  6 + loadModule('module/baz/foo', function(foo) {
  7 +
  8 + })
3 9 };
5 test/test-module-server.js
@@ -103,10 +103,10 @@ require('../module-server').from('./fixtures/build',
103 103 t.ok(s0.map.mappings);
104 104 t.is(s0.map.sourceRoot, prefix);
105 105
106   - t.is(sections[1].offset.line, 3);
  106 + t.is(sections[1].offset.line, 4);
107 107 t.is(sections[1].offset.column, 0);
108 108 t.is(sections[1].map.file, files[1]);
109   - t.is(sections[2].offset.line, 5);
  109 + t.is(sections[2].offset.line, 7);
110 110 t.is(sections[2].offset.column, 0);
111 111 t.is(sections[2].map.file, files[2]);
112 112 }, options);
@@ -121,6 +121,7 @@ function getJs(names) {
121 121 names.forEach(function(name) {
122 122 var filename = './fixtures/build/' + name + '.js';
123 123 js += require('fs').readFileSync(filename, 'utf8') + '\n';
  124 + js += 'ModuleServer.m.' + name + '=' + name + ';\n';
124 125 });
125 126 return js;
126 127 }

0 comments on commit ddfe0f1

Please sign in to comment.
Something went wrong with that request. Please try again.