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