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 * as init_trace_processor from '../gen/trace_processor'; 16 17import {WasmBridge, WasmBridgeRequest} from './wasm_bridge'; 18 19// tslint:disable no-any 20// Proxy all messages to WasmBridge#callWasm. 21const anySelf = (self as any); 22 23// Messages can arrive before we are initialized, queue these for later. 24const msgQueue: MessageEvent[] = []; 25anySelf.onmessage = (msg: MessageEvent) => { 26 msgQueue.push(msg); 27}; 28 29const bridge = new WasmBridge(init_trace_processor); 30bridge.whenInitialized.then(() => { 31 const handleMsg = (msg: MessageEvent) => { 32 const request: WasmBridgeRequest = msg.data; 33 anySelf.postMessage(bridge.callWasm(request)); 34 }; 35 36 // Dispatch queued messages. 37 let msg; 38 while (msg = msgQueue.shift()) { 39 handleMsg(msg); 40 } 41 42 anySelf.onmessage = handleMsg; 43}); 44