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 type { Message } from '../../worker/AsyncManager'; 17import { AsyncManager } from '../../worker/AsyncManager'; 18import { Log } from '../../utils/Log'; 19import type { AnyAction, Dispatch, Middleware, MiddlewareAPI } from '../core/redux'; 20 21const TAG = '[reduxWorkerMiddle]:'; 22 23/** 24 * Middleware to emit async operation like switching camera and so on. 25 * 26 * @param store the created old store 27 * @returns (next: Dispatch) => (action: AnyAction) => anyAction 28 */ 29export const reduxWorkerMiddle: Middleware = (store: MiddlewareAPI) => (next: Dispatch) => (action: AnyAction) => { 30 const asyncManager = AsyncManager.getInstance(); 31 const uiAction = { type: undefined, data: undefined }; 32 if (Object.keys(action).length == 2) { 33 asyncManager.postMessage(action as Message); 34 } 35 uiAction.type = action.type; 36 uiAction.data = action.data; 37 // EventBusManager.getInstance().getEventBus().emit(action.type, [action.data]) 38 const result = next(uiAction); 39 Log.info(`${TAG} logger: new state ${JSON.stringify(store.getState())}`); 40 return result; 41}