| 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 let define = (function(){ | 9 function define(name, deps, factory) { |
| 10 let moduleCache = new Map(); | 10 return new Promise(resolve => { |
| 11 | 11 mojo.define(name, deps, (...modules) => { |
| 12 return function(name, deps, factory) { | 12 let result = factory(...modules); |
| 13 let promise = moduleCache.get(name); | 13 resolve(result); |
| 14 if (promise === undefined) { | 14 return result; |
| 15 // This promise must be cached as mojo.define will only call the factory | 15 }); |
| 16 // function the first time the module is defined. | 16 }); |
| 17 promise = new Promise(resolve => { | 17 } |
| 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 })(); | |
| 29 | 18 |
| 30 // Returns a promise to an object that exposes common Mojo module interfaces. | 19 // Returns a promise to an object that exposes common Mojo module interfaces. |
| 31 // Additional modules to load can be specified in the |modules| parameter. The | 20 // Additional modules to load can be specified in the |modules| parameter. The |
| 32 // result will contain them, in the same order, in the |modules| field. | 21 // result will contain them, in the same order, in the |modules| field. |
| 33 function loadMojoModules(name, modules = []) { | 22 function loadMojoModules(name, modules = []) { |
| 34 return define('Mojo layout test module: ' + name, [ | 23 return define('Mojo layout test module: ' + name, [ |
| 35 'mojo/public/js/core', | 24 'mojo/public/js/core', |
| 36 'mojo/public/js/router', | 25 'mojo/public/js/router', |
| 37 'mojo/public/js/support', | 26 'mojo/public/js/support', |
| 38 'content/public/renderer/service_provider', | 27 'content/public/renderer/service_provider', |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 let buffer, handles; | 67 let buffer, handles; |
| 79 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0)); | 68 ({ result, buffer, handles } = mojo.core.readMessage(pipe, 0)); |
| 80 if (result !== mojo.core.RESULT_OK) { | 69 if (result !== mojo.core.RESULT_OK) { |
| 81 reject(result); | 70 reject(result); |
| 82 return; | 71 return; |
| 83 } | 72 } |
| 84 resolve({ buffer, handles }); | 73 resolve({ buffer, handles }); |
| 85 }); | 74 }); |
| 86 }); | 75 }); |
| 87 }; | 76 }; |
| OLD | NEW |