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 { Log } from '../utils/Log'; 17import { Constants } from '../utils/Constants'; 18import type { EventBus } from './eventbus/EventBus'; 19import { EventBusManager } from './eventbus/EventBusManager'; 20import { WorkerManager } from './WorkerManager'; 21import type { IModeMap } from '../featureservice/IModeMap'; 22 23const TAG = '[CameraWorker]:'; 24 25export class CameraWorker { 26 private appEventBus: EventBus = EventBusManager.getWorkerInstance().getEventBus(); 27 private workerManager: WorkerManager; 28 29 constructor(modeMap: IModeMap) { 30 this.workerManager = new WorkerManager(); 31 // 接收UI线程的消息,并继续发送 32 this.appEventBus.on('MAIN_TO_WORKER', (msg) => { 33 Log.info(`[CameraWorker]: action from main thread: ${JSON.stringify(msg)}`); 34 this.workerManager.onMessage(msg); 35 }) 36 } 37 38 public static getInstance(modeMap: IModeMap): CameraWorker { 39 if (!AppStorage.Has(Constants.APP_KEY_CAMERA_WORKER)) { 40 AppStorage.SetOrCreate(Constants.APP_KEY_CAMERA_WORKER, new CameraWorker(modeMap)); 41 Log.info(`${TAG} build new CameraWorker.`); 42 } 43 return AppStorage.Get(Constants.APP_KEY_CAMERA_WORKER); 44 } 45}