Index: third_party/WebKit/LayoutTests/resources/mojo-helpers.js |
diff --git a/third_party/WebKit/LayoutTests/resources/mojo-helpers.js b/third_party/WebKit/LayoutTests/resources/mojo-helpers.js |
index 10350f7fc6da14d53e0c579956f578d7d414356b..aca02bc9b78c7375375d4a1a43c11a5e4d427eae 100644 |
--- a/third_party/WebKit/LayoutTests/resources/mojo-helpers.js |
+++ b/third_party/WebKit/LayoutTests/resources/mojo-helpers.js |
@@ -6,15 +6,26 @@ |
// Fix up the global window.define, since all baked-in Mojo modules expect to |
// find it there. This define() also returns a promise to the module. |
-function define(name, deps, factory) { |
- return new Promise(resolve => { |
- mojo.define(name, deps, (...modules) => { |
- let result = factory(...modules); |
- resolve(result); |
- return result; |
- }); |
- }); |
-} |
+let define = (function(){ |
+ let moduleCache = new Map(); |
+ |
+ return function(name, deps, factory) { |
+ let promise = moduleCache.get(name); |
+ if (promise === undefined) { |
+ // This promise must be cached as mojo.define will only call the factory |
+ // function the first time the module is defined. |
+ promise = new Promise(resolve => { |
+ mojo.define(name, deps, (...modules) => { |
+ let result = factory(...modules); |
+ resolve(result); |
+ return result; |
+ }); |
+ }); |
+ moduleCache.set(name, promise); |
+ } |
+ return promise; |
+ } |
+})(); |
// Returns a promise to an object that exposes common Mojo module interfaces. |
// Additional modules to load can be specified in the |modules| parameter. The |