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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 'use strict'; 5 'use strict';
6 6
7 if (mojoBindings) { 7 if (mojoBindings) {
8 throw new Error('mojoBindings has been initialized.'); 8 throw new Error('mojoBindings has been initialized.');
9 } 9 }
10 10
11 var mojoBindings = {}; 11 var mojoBindings = {};
12 mojoBindings.internal = {}; 12 mojoBindings.internal = {};
13 mojoBindings.internal.global = this; 13 mojoBindings.internal.global = this;
14 mojoBindings.config = {
15 // Whether to automatically load mojom dependencies.
16 // For example, if foo.mojom imports bar.mojom, |auto_load_mojom_deps| sets
Ken Rockot(use gerrit already) 2017/03/26 18:27:11 not: sets -> set
yzshen1 2017/03/26 19:51:11 Done.
17 // to true means that loading foo.mojom.js will insert a <script> tag to
18 // load bar.mojom.js, if it hasn't been loaded.
19 //
20 // The URL of bar.mojom.js is determined by the relative path of bar.mojom
21 // (relative to the position of foo.mojom at build time) and the URL of
22 // foo.mojom.js. For exmple, if at build time the two mojom files are
23 // located at:
24 // a/b/c/foo.mojom
25 // a/b/d/bar.mojom
26 // and the URL of foo.mojom.js is:
27 // http://example.org/scripts/b/c/foo.mojom.js
28 // then the URL of bar.mojom.js will be:
29 // http://example.org/scripts/b/d/bar.mojom.js
30 //
31 // If you would like bar.mojom.js to live at a different location, you need
32 // to turn off |auto_load_mojom_deps| before loading foo.mojom.js, and
33 // manually load bar.mojom.js yourself. Similarly, you need to turn off the
34 // option if you merge bar.mojom.js and foo.mojom.js into a single file.
35 //
36 // Performance tip: Avoid loading the same mojom.js file multiple times.
37 // Assume that |auto_load_mojom_deps| is set to true:
38 // <!-- No duplicate loading; recommended. -->
39 // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script>
40 //
41 // <!-- No duplicate loading, although unnecessary. -->
42 // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script>
43 // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script>
44 //
45 // <!-- Load bar.mojom.js twice; should be avoided. -->
46 // <script src="http://example.org/scripts/b/c/foo.mojom.js"></script>
47 // <script src="http://example.org/scripts/b/d/bar.mojom.js"></script>
48 auto_load_mojom_deps: true
Ken Rockot(use gerrit already) 2017/03/26 18:27:11 not: shouldn't this be autoLoadMojomDeps?
yzshen1 2017/03/26 19:51:11 Yes! :) I am too used to C++...
49 };
14 50
15 (function() { 51 (function() {
16 var internal = mojoBindings.internal; 52 var internal = mojoBindings.internal;
17 53
54 var LoadState = {
55 PENDING_LOAD: 1,
56 LOADED: 2
57 };
58
59 var mojomRegistry = new Map();
60
18 function exposeNamespace(namespace) { 61 function exposeNamespace(namespace) {
19 var current = internal.global; 62 var current = internal.global;
20 var parts = namespace.split('.'); 63 var parts = namespace.split('.');
21 64
22 for (var part; parts.length && (part = parts.shift());) { 65 for (var part; parts.length && (part = parts.shift());) {
23 if (!current[part]) { 66 if (!current[part]) {
24 current[part] = {}; 67 current[part] = {};
25 } 68 }
26 current = current[part]; 69 current = current[part];
27 } 70 }
28 71
29 return current; 72 return current;
30 } 73 }
31 74
75 function isMojomPendingLoad(id) {
76 return mojomRegistry.get(id) === LoadState.PENDING_LOAD;
77 }
78
79 function isMojomLoaded(id) {
80 return mojomRegistry.get(id) === LoadState.LOADED;
81 }
82
83 function markMojomPendingLoad(id) {
84 if (isMojomLoaded(id)) {
85 throw new Error('The following mojom file has been loaded: ' + id);
86 }
87
88 mojomRegistry.set(id, LoadState.PENDING_LOAD);
89 }
90
91 function markMojomLoaded(id) {
92 mojomRegistry.set(id, LoadState.LOADED);
93 }
94
95 function loadMojomIfNecessary(id, url) {
96 if (mojomRegistry.has(id)) {
97 return;
98 }
99
100 markMojomPendingLoad(id);
101 internal.global.document.write('<script type="text/javascript" src="' +
102 url + '"></script>');
103 }
104
32 internal.exposeNamespace = exposeNamespace; 105 internal.exposeNamespace = exposeNamespace;
106 internal.isMojomPendingLoad = isMojomPendingLoad;
107 internal.isMojomLoaded = isMojomLoaded;
108 internal.markMojomPendingLoad = markMojomPendingLoad;
109 internal.markMojomLoaded = markMojomLoaded;
110 internal.loadMojomIfNecessary = loadMojomIfNecessary;
33 })(); 111 })();
OLDNEW
« 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