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 {WasmBridge} from './wasm_bridge'; 16 17const selfWorker = self as {} as Worker; 18const wasmBridge = new WasmBridge(); 19 20// There are two message handlers here: 21// 1. The Worker (self.onmessage) handler. 22// 2. The MessagePort handler. 23// The sequence of actions is the following: 24// 1. The frontend does one postMessage({port: MessagePort}) on the Worker 25// scope. This message transfers the MessagePort. 26// This is the only postMessage we'll ever receive here. 27// 2. All the other messages (i.e. the TraceProcessor RPC binary pipe) will be 28// received on the MessagePort. 29 30// Receives the boostrap message from the frontend with the MessagePort. 31selfWorker.onmessage = (msg: MessageEvent) => { 32 const port = msg.data as MessagePort; 33 wasmBridge.initialize(port); 34}; 35