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 { Log } from '../utils/Log' 19import { Constants } from '../utils/Constants' 20import { EventBus } from './eventbus/EventBus' 21import { EventBusManager } from './eventbus/EventBusManager' 22 23export interface Message { 24 type: string, 25 data: any 26} 27 28export class AsyncManager { 29 private static TAG = '[AsyncManager]:' 30 private workerName: string 31 private workerUri: string 32 protected mWorker: any 33 private appEventBus: EventBus = EventBusManager.getWorkerInstance().getEventBus() 34 private _appEventBus: EventBus = EventBusManager.getCameraInstance().getEventBus() 35 36 constructor() { 37 // todo 此处暂时只考虑了一个worker,后续改造支持多个worker创建 38 this.workerName = 'AsyncManager' 39 this.workerUri = 'workers/CameraWorker.js' 40 this.initWorker() 41 } 42 43 public static getInstance(): AsyncManager { 44 if (!AppStorage.Has(Constants.APP_KEY_ASYNC_MANAGER)) { 45 AppStorage.SetOrCreate(Constants.APP_KEY_ASYNC_MANAGER, new AsyncManager()) 46 Log.info(`${this.TAG} build new AsyncManager.`) 47 } 48 return AppStorage.Get(Constants.APP_KEY_ASYNC_MANAGER) 49 } 50 51 //todo 预留实现,待能力稳定后开放 52 // private initWorker(): void { 53 // this.mWorker = new worker.Worker(this.workerUri, {type: 'classic', name: this.workerName}) 54 // Log.info(`${AsyncManager.TAG} build the worker.`) 55 // this.mWorker.onmessage = (...args) => { 56 // Log.info(`${AsyncManager.TAG} mWorker.onmessage`) 57 // this.onMessage(args[0].data) 58 // } 59 // this.mWorker.onmessageerror = this.onmessageerror.bind(this) 60 // this.mWorker.onerror = this.onerror.bind(this) 61 // this.mWorker.onexit = this.onexit.bind(this) 62 // } 63 64 private initWorker(): void { 65 this._appEventBus.on('WORKER_TO_MAIN', (...args) => { 66 Log.info(`${AsyncManager.TAG} mWorker.onmessage`) 67 this.onMessage(args[0]) 68 }) 69 } 70 71 //todo 预留实现,待能力稳定后开放 72 // // 向worker线程发送消息 73 // public postMessage(msg: any): AsyncManager { 74 // this.mWorker.postMessage(msg) 75 // return this 76 // } 77 78 // 向worker线程发送消息 79 public postMessage(msg: Message): void { 80 Log.info(`${AsyncManager.TAG} postMessage`) 81 this.appEventBus.emit('MAIN_TO_WORKER', [msg]) 82 } 83 84 // 接收worker线程返回的UiData 85 public onMessage(msg: Message): void { 86 Log.info(`${AsyncManager.TAG} onMessage uidata: ${JSON.stringify(msg.data)}`) 87 } 88 89 public onmessageerror(msg: Message): void { 90 91 } 92 93 public onerror(msg: Message): void { 94 95 } 96 97 public onexit(msg: Message): void { 98 99 } 100}