Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1182)

Unified Diff: mojo/public/js/new_bindings/base.js

Issue 2779493002: Mojo JS bindings: support auto-loading mojom.js deps. (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/tools/bindings/generators/js_templates/module.amd.tmpl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/js/new_bindings/base.js
diff --git a/mojo/public/js/new_bindings/base.js b/mojo/public/js/new_bindings/base.js
index ef7aa32f73646c47bec8b51694b458ad50d929a7..db72d489db6ec016817ec1557a11812f33836b31 100644
--- a/mojo/public/js/new_bindings/base.js
+++ b/mojo/public/js/new_bindings/base.js
@@ -11,10 +11,53 @@ if (mojo && mojo.internal) {
var mojo = mojo || {};
mojo.internal = {};
mojo.internal.global = this;
+mojo.config = {
+ // Whether to automatically load mojom dependencies.
+ // For example, if foo.mojom imports bar.mojom, |autoLoadMojomDeps| set to
+ // true means that loading foo.mojom.js will insert a <script> tag to load
+ // bar.mojom.js, if it hasn't been loaded.
+ //
+ // The URL of bar.mojom.js is determined by the relative path of bar.mojom
+ // (relative to the position of foo.mojom at build time) and the URL of
+ // foo.mojom.js. For exmple, if at build time the two mojom files are
+ // located at:
+ // a/b/c/foo.mojom
+ // a/b/d/bar.mojom
+ // and the URL of foo.mojom.js is:
+ // http://example.org/scripts/b/c/foo.mojom.js
+ // then the URL of bar.mojom.js will be:
+ // http://example.org/scripts/b/d/bar.mojom.js
+ //
+ // If you would like bar.mojom.js to live at a different location, you need
+ // to turn off |autoLoadMojomDeps| before loading foo.mojom.js, and manually
+ // load bar.mojom.js yourself. Similarly, you need to turn off the option if
+ // you merge bar.mojom.js and foo.mojom.js into a single file.
+ //
+ // Performance tip: Avoid loading the same mojom.js file multiple times.
+ // Assume that |autoLoadMojomDeps| is set to true:
+ // <!-- No duplicate loading; recommended. -->
+ // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script>
+ //
+ // <!-- No duplicate loading, although unnecessary. -->
+ // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script>
+ // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script>
+ //
+ // <!-- Load bar.mojom.js twice; should be avoided. -->
+ // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script>
+ // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script>
+ autoLoadMojomDeps: true
+};
(function() {
var internal = mojo.internal;
+ var LoadState = {
+ PENDING_LOAD: 1,
+ LOADED: 2
+ };
+
+ var mojomRegistry = new Map();
+
function exposeNamespace(namespace) {
var current = internal.global;
var parts = namespace.split('.');
@@ -29,5 +72,40 @@ mojo.internal.global = this;
return current;
}
+ function isMojomPendingLoad(id) {
+ return mojomRegistry.get(id) === LoadState.PENDING_LOAD;
+ }
+
+ function isMojomLoaded(id) {
+ return mojomRegistry.get(id) === LoadState.LOADED;
+ }
+
+ function markMojomPendingLoad(id) {
+ if (isMojomLoaded(id)) {
+ throw new Error('The following mojom file has been loaded: ' + id);
+ }
+
+ mojomRegistry.set(id, LoadState.PENDING_LOAD);
+ }
+
+ function markMojomLoaded(id) {
+ mojomRegistry.set(id, LoadState.LOADED);
+ }
+
+ function loadMojomIfNecessary(id, url) {
+ if (mojomRegistry.has(id)) {
+ return;
+ }
+
+ markMojomPendingLoad(id);
+ internal.global.document.write('<script type="text/javascript" src="' +
+ url + '"></script>');
+ }
+
internal.exposeNamespace = exposeNamespace;
+ internal.isMojomPendingLoad = isMojomPendingLoad;
+ internal.isMojomLoaded = isMojomLoaded;
+ internal.markMojomPendingLoad = markMojomPendingLoad;
+ internal.markMojomLoaded = markMojomLoaded;
+ internal.loadMojomIfNecessary = loadMojomIfNecessary;
})();
« no previous file with comments | « no previous file | mojo/public/tools/bindings/generators/js_templates/module.amd.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698