1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 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 */ 15 16import worker from '@ohos.worker' 17 18import { ActionHandler } from './ActionHandler' 19import { Log } from '../utils/Log' 20import { EventBus } from './eventbus/EventBus' 21import { EventBusManager } from './eventbus/EventBusManager' 22 23export class WorkerManager { 24 private TAG = '[WorkerManager]:' 25 private actionHandler: ActionHandler = new ActionHandler() 26 static parentPort = worker.parentPort 27 private _appEventBus: EventBus = EventBusManager.getCameraInstance().getEventBus() 28 29 public onMessage(action: any): void { 30 Log.info(`${this.TAG} action from main thread: ${JSON.stringify(action)}`) 31 this.actionHandler.handleAction(action) 32 } 33 34 //todo 预留实现,待能力稳定后开放 35 // // worker线程中通过该方法向UI线程发送消息,消息中包含type和data 36 // public postMessage(msg: any): void { 37 // Log.info(`${this.TAG} postMessage: ${JSON.stringify(msg)}`) 38 // WorkerManager.parentPort.postMessage(msg) 39 // } 40 41 // worker线程中通过该方法向UI线程发送消息,消息中包含type和data 42 public postMessage(msg: any): void { 43 Log.info(`${this.TAG} postMessage: ${JSON.stringify(msg)}`) 44 this._appEventBus.emit('WORKER_TO_MAIN', [msg]) 45 } 46}