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 type { EventBus } from './eventbus/EventBus';
21import { EventBusManager } from './eventbus/EventBusManager';
22import type { ActionData } from '../redux/actions/Action';
23import { getStore } from '../redux/store';
24
25const TAG = '[WorkerManager]:';
26
27export class WorkerManager {
28  static parentPort = worker.parentPort;
29  private actionHandler: ActionHandler = new ActionHandler();
30  private _appEventBus: EventBus = EventBusManager.getCameraInstance().getEventBus();
31
32  public onMessage(action: ActionData): void {
33    getStore().dispatch(action);
34  }
35
36  //todo 预留实现,待能力稳定后开放
37  //  // worker线程中通过该方法向UI线程发送消息,消息中包含type和data
38  //  public postMessage(msg: any): void {
39  //    Log.info(`${TAG} postMessage: ${JSON.stringify(msg)}`)
40  //    WorkerManager.parentPort.postMessage(msg)
41  //  }
42
43  // worker线程中通过该方法向UI线程发送消息,消息中包含type和data
44  public postMessage(msg: ActionData): void {
45    Log.info(`${TAG} postMessage: ${JSON.stringify(msg)}`);
46    this.onMessage(msg);
47  }
48}