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 { EventBusManager } from '../worker/eventbus/EventBusManager' 18import { Log } from '../utils/Log' 19import { createStore, combineReducers, applyMiddleware } from '../redux/core/ohredux/index' 20import { logger } from './middlewares/logger' 21import { reduxWorkerMiddle } from './middlewares/ReduxWorkerMiddle' 22import CameraInitReducer, { CameraInitState } from './reducers/CameraInitReducer' 23import ContextReducer, { ContextState } from './reducers/ContextReducer' 24import CameraReducer, { CameraState } from './reducers/CameraReducer' 25import PreviewReducer, { PreviewState } from './reducers/PreviewReducer' 26import CaptureReducer, { CaptureState } from './reducers/CaptureReducer' 27import ModeChangeReducer, { ModeChangeState } from './reducers/ModeChangeReducer' 28import ModeReducer, { ModeState } from './reducers/ModeReducer' 29import SettingReducer, { SettingState } from './reducers/SettingReducer' 30import RecordReducer, { RecordState } from './reducers/RecordReducer' 31import ZoomReducer, { ZoomState } from './reducers/ZoomReducer' 32import { ActionData } from './actions/Action' 33import { CombinedState, Dispatch, Unsubscribe } from './core/redux' 34import { MapDispatchProp, MapStateProp } from './core/ohredux/connect' 35 36const TAG = '[store]:' 37const STORE_KEY = 'CAMERA_REDUX_STORE' 38 39export type OhCombinedState = CombinedState<{ 40 CameraInitReducer: CameraInitState; 41 ContextReducer: ContextState; 42 CameraReducer: CameraState; 43 PreviewReducer: PreviewState; 44 CaptureReducer: CaptureState; 45 RecordReducer: RecordState; 46 ModeChangeReducer: ModeChangeState; 47 ModeReducer: ModeState 48 SettingReducer: SettingState 49 ZoomReducer: ZoomState 50}> 51 52export function getStore(): { 53 getState: () => OhCombinedState, 54 dispatch: Dispatch<ActionData>, 55 subscribe: (listener: () => void) => Unsubscribe, 56 connect: (mapStateProp: MapStateProp, mapDispatchProp: MapDispatchProp) => (target: any) => void 57 } { 58 Log.info(`${TAG} store init.`) 59 if (!AppStorage.Has(STORE_KEY)) { 60 const store = createStore( 61 combineReducers({ 62 CameraInitReducer, 63 ContextReducer, 64 CameraReducer, 65 PreviewReducer, 66 CaptureReducer, 67 RecordReducer, 68 ModeChangeReducer, 69 ModeReducer, 70 SettingReducer, 71 ZoomReducer, 72 }), 73 applyMiddleware(logger, reduxWorkerMiddle) 74 ) 75 AppStorage.SetOrCreate(STORE_KEY, store) 76 AsyncManager.getInstance().onMessage = (msg: Message) => { 77 const action = {type: '', data: undefined, tag: 'FROM_WORKER'} 78 Log.info(`${TAG} store dispatch msg: ${JSON.stringify(msg)}`) 79 action.type = msg.type 80 action.data = msg.data 81 store.dispatch(action) 82 EventBusManager.getInstance().getEventBus().emit(msg.type, [msg.data]) 83 } 84 85 } 86 return AppStorage.Get(STORE_KEY) 87}