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