1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import '../common/recordingV2/target_factories'; 16 17import {assertExists, assertTrue} from '../base/logging'; 18 19import {AppController} from './app_controller'; 20import {ControllerAny} from './controller'; 21 22let rootController: ControllerAny; 23let runningControllers = false; 24 25export function initController(extensionPort: MessagePort) { 26 assertTrue(rootController === undefined); 27 rootController = new AppController(extensionPort); 28} 29 30export function runControllers() { 31 if (runningControllers) throw new Error('Re-entrant call detected'); 32 33 // Run controllers locally until all state machines reach quiescence. 34 let runAgain = true; 35 for (let iter = 0; runAgain; iter++) { 36 if (iter > 100) throw new Error('Controllers are stuck in a livelock'); 37 runningControllers = true; 38 try { 39 runAgain = assertExists(rootController).invoke(); 40 } finally { 41 runningControllers = false; 42 } 43 } 44} 45