2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
19 import { Store } from '../flux/store';
20 import { combineActionHandler, MiddlewareArg, Middleware, chainMiddleware } from '../flux/middleware';
22 import applicationService from '../services/applicationManager';
24 import { applicationRegistryHandler, IApplicationRegistration } from '../handlers/applicationRegistryHandler';
25 import { authenticationStateHandler, IAuthenticationState } from '../handlers/authenticationHandler';
26 import { applicationStateHandler, IApplicationState } from '../handlers/applicationStateHandler';
27 import { navigationStateHandler, INavigationState } from '../handlers/navigationStateHandler';
29 import { setApplicationStore } from '../services/applicationApi';
31 import apiMiddleware from '../middleware/api';
32 import thunkMiddleware from '../middleware/thunk';
33 import loggerMiddleware from '../middleware/logger';
34 import routerMiddleware from '../middleware/navigation';
36 export type MiddlewareApi = MiddlewareArg<IApplicationStoreState>;
38 export interface IFrameworkStoreState {
39 applicationRegistraion: IApplicationRegistration;
40 applicationState: IApplicationState;
41 authenticationState: IAuthenticationState;
42 navigationState: INavigationState;
45 export interface IApplicationStoreState {
46 framework: IFrameworkStoreState;
49 const frameworkHandlers = combineActionHandler({
50 applicationRegistraion: applicationRegistryHandler,
51 applicationState: applicationStateHandler,
52 authenticationState: authenticationStateHandler,
53 navigationState: navigationStateHandler
56 export class ApplicationStore extends Store<IApplicationStoreState> { }
58 /** This function will create the application store considering the currently registered application ans their middlewares. */
59 export const applicationStoreCreator = (): ApplicationStore => {
60 const middlewares: Middleware<IApplicationStoreState>[] = [];
61 const actionHandlers = Object.keys(applicationService.applications).reduce((acc, cur) => {
62 const reg = applicationService.applications[cur];
63 reg && typeof reg.rootActionHandler === 'function' && (acc[cur] = reg.rootActionHandler);
64 reg && +(reg.middlewares || 0) && middlewares.push(...(reg.middlewares as Middleware<IApplicationStoreState>[]));
66 }, { framework: frameworkHandlers } as any);
68 const applicationStore = new ApplicationStore(combineActionHandler(actionHandlers), chainMiddleware(loggerMiddleware, thunkMiddleware, routerMiddleware, apiMiddleware, ...middlewares));
69 setApplicationStore(applicationStore);
70 return applicationStore;
73 export default applicationStoreCreator;