Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
Tim van der Lippe | 3387bd4 | 2020-10-20 11:46:31 | [diff] [blame^] | 31 | import * as RootModule from '../root/root.js'; |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 32 | |
Tim van der Lippe | 3387bd4 | 2020-10-20 11:46:31 | [diff] [blame^] | 33 | // Legacy runtime namespace definitions |
| 34 | // @ts-ignore |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 35 | self.Runtime = self.Runtime || {}; |
Tim van der Lippe | 3387bd4 | 2020-10-20 11:46:31 | [diff] [blame^] | 36 | // @ts-ignore |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 37 | Runtime = Runtime || {}; |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 38 | // The following two variables are initialized in `build_release_applications` |
Tim van der Lippe | 3387bd4 | 2020-10-20 11:46:31 | [diff] [blame^] | 39 | // @ts-ignore |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 40 | Root.allDescriptors = Root.allDescriptors || []; |
Tim van der Lippe | 3387bd4 | 2020-10-20 11:46:31 | [diff] [blame^] | 41 | // @ts-ignore |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 42 | Root.applicationDescriptor = Root.applicationDescriptor || undefined; |
| 43 | |
| 44 | /** @type {Function} */ |
| 45 | let appStartedPromiseCallback; |
Patrick Brosset | e65aaac | 2020-06-22 08:04:40 | [diff] [blame] | 46 | Runtime.appStarted = new Promise(fulfil => { |
| 47 | appStartedPromiseCallback = fulfil; |
| 48 | }); |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @param {string} appName |
| 52 | * @return {!Promise.<void>} |
| 53 | */ |
| 54 | export async function startApplication(appName) { |
| 55 | console.timeStamp('Root.Runtime.startApplication'); |
| 56 | |
| 57 | /** @type {!Object<string, RootModule.Runtime.ModuleDescriptor>} */ |
| 58 | const allDescriptorsByName = {}; |
| 59 | for (let i = 0; i < Root.allDescriptors.length; ++i) { |
| 60 | const d = Root.allDescriptors[i]; |
| 61 | allDescriptorsByName[d['name']] = d; |
| 62 | } |
| 63 | |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 64 | const configuration = Root.applicationDescriptor.modules; |
Tim van der Lippe | f866e2b | 2020-09-14 14:49:13 | [diff] [blame] | 65 | const moduleDescriptors = []; |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 66 | const coreModuleNames = []; |
| 67 | for (let i = 0; i < configuration.length; ++i) { |
| 68 | const descriptor = configuration[i]; |
| 69 | const name = descriptor['name']; |
Tim van der Lippe | f866e2b | 2020-09-14 14:49:13 | [diff] [blame] | 70 | moduleDescriptors.push(allDescriptorsByName[name]); |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 71 | if (descriptor['type'] === 'autostart') { |
| 72 | coreModuleNames.push(name); |
| 73 | } |
| 74 | } |
| 75 | |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 76 | for (let i = 0; i < moduleDescriptors.length; ++i) { |
| 77 | moduleDescriptors[i].name = configuration[i]['name']; |
| 78 | moduleDescriptors[i].condition = configuration[i]['condition']; |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 79 | } |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 80 | const runtimeInstance = RootModule.Runtime.Runtime.instance({forceNew: true, moduleDescriptors}); |
Tim van der Lippe | 3387bd4 | 2020-10-20 11:46:31 | [diff] [blame^] | 81 | // @ts-ignore Exposed for legacy layout tests |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 82 | self.runtime = runtimeInstance; |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 83 | if (coreModuleNames) { |
Tim van der Lippe | 7f2002c | 2020-09-28 14:54:01 | [diff] [blame] | 84 | await runtimeInstance.loadAutoStartModules(coreModuleNames); |
Tim van der Lippe | 6d51bf0 | 2020-03-18 12:15:14 | [diff] [blame] | 85 | } |
| 86 | appStartedPromiseCallback(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param {string} appName |
| 91 | * @return {!Promise.<void>} |
| 92 | */ |
| 93 | export async function startWorker(appName) { |
| 94 | return startApplication(appName).then(sendWorkerReady); |
| 95 | |
| 96 | function sendWorkerReady() { |
| 97 | self.postMessage('workerReady'); |
| 98 | } |
| 99 | } |