| OLD | NEW |
| 1 /* | 1 /* |
| 2 * mojo-helpers contains extensions to testharness.js useful for consuming | 2 * mojo-helpers contains extensions to testharness.js useful for consuming |
| 3 * and mocking Mojo services directly within test code. | 3 * and mocking Mojo services directly within test code. |
| 4 */ | 4 */ |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 // Fix up the global window.define, since all baked-in Mojo modules expect to | 7 // Fix up the global window.define, since all baked-in Mojo modules expect to |
| 8 // find it there. This define() also returns a promise to the module. | 8 // find it there. This define() also returns a promise to the module. |
| 9 function define(name, deps, factory) { | 9 let define = (function(){ |
| 10 return new Promise(resolve => { | 10 let moduleCache = new Map(); |
| 11 mojo.define(name, deps, (...modules) => { | 11 |
| 12 let result = factory(...modules); | 12 return function(name, deps, factory) { |
| 13 resolve(result); | 13 let promise = moduleCache.get(name); |
| 14 return result; | 14 if (promise === undefined) { |
| 15 }); | 15 // This promise must be cached as mojo.define will only call the factory |
| 16 }); | 16 // function the first time the module is defined. |
| 17 } | 17 promise = new Promise(resolve => { |
| 18 mojo.define(name, deps, (...modules) => { |
| 19 let result = factory(...modules); |
| 20 resolve(result); |
| 21 return result; |
| 22 }); |
| 23 }); |
| 24 moduleCache.set(name, promise); |
| 25 } |
| 26 return promise; |
| 27 } |
| 28 })(); |
| 18 | 29 |
| 19 // Returns a promise to an object that exposes common Mojo module interfaces. | 30 // Returns a promise to an object that exposes common Mojo module interfaces. |
| 20 // Additional modules to load can be specified in the |modules| parameter. The | 31 // Additional modules to load can be specified in the |modules| parameter. The |
| 21 // result will contain them, in the same order, in the |modules| field. | 32 // result will contain them, in the same order, in the |modules| field. |
| 22 function loadMojoModules(name, modules = []) { | 33 function loadMojoModules(name, modules = []) { |
| 23 return define('Mojo layout test module: ' + name, [ | 34 return define('Mojo layout test module: ' + name, [ |
| 24 'mojo/public/js/core', | 35 'mojo/public/js/core', |
| 25 'mojo/public/js/router', | 36 'mojo/public/js/router', |
| 26 'mojo/public/js/support', | 37 'mojo/public/js/support', |
| 27 'content/public/renderer/service_provider', | 38 'content/public/renderer/service_provider', |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 let buffer, handles; | 78 let buffer, handles; |
| 68 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0)); | 79 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0)); |
| 69 if (result !== mojo.core.RESULT_OK) { | 80 if (result !== mojo.core.RESULT_OK) { |
| 70 reject(result); | 81 reject(result); |
| 71 return; | 82 return; |
| 72 } | 83 } |
| 73 resolve({ buffer, handles }); | 84 resolve({ buffer, handles }); |
| 74 }); | 85 }); |
| 75 }); | 86 }); |
| 76 }; | 87 }; |
| OLD | NEW |