update odlux and featureaggregator
[ccsdk/features.git] / sdnr / wt / odlux / framework / src / flux / store.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 import { Event } from "../common/event"
19
20 import { Action } from './action';
21 import { IActionHandler } from './action';
22
23 export interface Dispatch {
24   <TAction extends Action>(action: TAction): TAction;
25 }
26
27 export interface Enhancer<TStoreState> {
28   (store: Store<TStoreState>): Dispatch;
29 }
30
31 class InitialisationAction extends Action { };
32 const initialisationAction = new InitialisationAction();
33
34 export class Store<TStoreState> {
35
36   constructor(actionHandler: IActionHandler<TStoreState>, enhancer?: Enhancer<TStoreState>)
37   constructor(actionHandler: IActionHandler<TStoreState>, initialState: TStoreState, enhancer?: Enhancer<TStoreState>)
38   constructor(actionHandler: IActionHandler<TStoreState>, initialState?: TStoreState | Enhancer<TStoreState>, enhancer?: Enhancer<TStoreState>) {
39     if (typeof initialState === 'function') {
40       enhancer = initialState as Enhancer<TStoreState>;
41       initialState = undefined;
42     }
43
44     this._isDispatching = false;
45      
46     this.changed = new Event<void>(); // sollten wir hier eventuell sogar den state mit übergeben ?
47
48     this._actionHandler = actionHandler;
49     
50     this._state = initialState as TStoreState;
51     if (enhancer) this._dispatch = enhancer(this);
52
53     this._dispatch(initialisationAction);
54   }
55
56   public changed: Event<void>;
57
58   private _dispatch: Dispatch = <TAction extends Action>(payload: TAction): TAction => {
59     if (payload == null || !(payload instanceof Action)) {
60       throw new Error(
61         'Actions must inherit from type Action. ' +
62         'Use a custom middleware for async actions.'
63       );
64     }
65     
66     if (this._isDispatching) {
67       throw new Error('ActionHandler may not dispatch actions.');
68     }
69
70     const oldState = this._state;
71     try {
72       this._isDispatching = true;
73       this._state = this._actionHandler(oldState, payload);
74     } finally {
75       this._isDispatching = false;
76     }
77
78     if (this._state !== oldState) {
79       this.changed.invoke();
80     }
81
82     return payload;
83   }
84
85   public get dispatch(): Dispatch {
86     return this._dispatch;
87   }
88
89   public get state() {
90     return this._state
91   }
92
93   private _state: TStoreState;
94   private _isDispatching: boolean;
95   private _actionHandler: IActionHandler<TStoreState>;
96
97 }
98