blob: 50c40cef6309a89b0eec22a32deaa8807db5b2c1 [file] [log] [blame]
Tim van der Lippe6d51bf02020-03-18 12:15:141/*
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 Lippe3387bd42020-10-20 11:46:3131import * as RootModule from '../root/root.js';
Tim van der Lippe6d51bf02020-03-18 12:15:1432
Tim van der Lippe3387bd42020-10-20 11:46:3133// Legacy runtime namespace definitions
34// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1435self.Runtime = self.Runtime || {};
Tim van der Lippe3387bd42020-10-20 11:46:3136// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1437Runtime = Runtime || {};
Tim van der Lippe6d51bf02020-03-18 12:15:1438// The following two variables are initialized in `build_release_applications`
Tim van der Lippe3387bd42020-10-20 11:46:3139// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1440Root.allDescriptors = Root.allDescriptors || [];
Tim van der Lippe3387bd42020-10-20 11:46:3141// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1442Root.applicationDescriptor = Root.applicationDescriptor || undefined;
43
44/** @type {Function} */
45let appStartedPromiseCallback;
Patrick Brossete65aaac2020-06-22 08:04:4046Runtime.appStarted = new Promise(fulfil => {
47 appStartedPromiseCallback = fulfil;
48});
Tim van der Lippe6d51bf02020-03-18 12:15:1449
50/**
51 * @param {string} appName
52 * @return {!Promise.<void>}
53 */
54export 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 Lippe6d51bf02020-03-18 12:15:1464 const configuration = Root.applicationDescriptor.modules;
Tim van der Lippef866e2b2020-09-14 14:49:1365 const moduleDescriptors = [];
Tim van der Lippe6d51bf02020-03-18 12:15:1466 const coreModuleNames = [];
67 for (let i = 0; i < configuration.length; ++i) {
68 const descriptor = configuration[i];
69 const name = descriptor['name'];
Tim van der Lippef866e2b2020-09-14 14:49:1370 moduleDescriptors.push(allDescriptorsByName[name]);
Tim van der Lippe6d51bf02020-03-18 12:15:1471 if (descriptor['type'] === 'autostart') {
72 coreModuleNames.push(name);
73 }
74 }
75
Tim van der Lippe6d51bf02020-03-18 12:15:1476 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 Lippe6d51bf02020-03-18 12:15:1479 }
Tim van der Lippe7f2002c2020-09-28 14:54:0180 const runtimeInstance = RootModule.Runtime.Runtime.instance({forceNew: true, moduleDescriptors});
Tim van der Lippe3387bd42020-10-20 11:46:3181 // @ts-ignore Exposed for legacy layout tests
Tim van der Lippe7f2002c2020-09-28 14:54:0182 self.runtime = runtimeInstance;
Tim van der Lippe6d51bf02020-03-18 12:15:1483 if (coreModuleNames) {
Tim van der Lippe7f2002c2020-09-28 14:54:0184 await runtimeInstance.loadAutoStartModules(coreModuleNames);
Tim van der Lippe6d51bf02020-03-18 12:15:1485 }
86 appStartedPromiseCallback();
87}
88
89/**
90 * @param {string} appName
91 * @return {!Promise.<void>}
92 */
93export async function startWorker(appName) {
94 return startApplication(appName).then(sendWorkerReady);
95
96 function sendWorkerReady() {
97 self.postMessage('workerReady');
98 }
99}