update odlux and featureaggregator
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / store / applicationStore.ts
1 /**
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import { Store } from '../flux/store';
20 import { combineActionHandler, MiddlewareArg, Middleware, chainMiddleware } from '../flux/middleware';
21
22 import applicationService from '../services/applicationManager';
23
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';
28
29 import { setApplicationStore } from '../services/applicationApi';
30
31 import apiMiddleware from '../middleware/api';
32 import thunkMiddleware from '../middleware/thunk';
33 import loggerMiddleware from '../middleware/logger';
34 import routerMiddleware from '../middleware/navigation';
35
36 export type MiddlewareApi = MiddlewareArg<IApplicationStoreState>;
37
38 export interface IFrameworkStoreState {
39   applicationRegistraion: IApplicationRegistration;
40   applicationState: IApplicationState;
41   authenticationState: IAuthenticationState;
42   navigationState: INavigationState;
43 }
44
45 export interface IApplicationStoreState {
46   framework: IFrameworkStoreState;
47 }
48
49 const frameworkHandlers = combineActionHandler({
50   applicationRegistraion: applicationRegistryHandler,
51   applicationState: applicationStateHandler,
52   authenticationState: authenticationStateHandler,
53   navigationState: navigationStateHandler
54 });
55
56 export class ApplicationStore extends Store<IApplicationStoreState> { }
57
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>[]));
65     return acc;
66   }, { framework: frameworkHandlers } as any);
67
68   const applicationStore = new ApplicationStore(combineActionHandler(actionHandlers), chainMiddleware(loggerMiddleware, thunkMiddleware, routerMiddleware, apiMiddleware, ...middlewares));
69   setApplicationStore(applicationStore);
70   return applicationStore;
71 }
72
73 export default applicationStoreCreator;