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==========================================================================
18 import { Event } from "../common/event"
20 import { Action } from './action';
21 import { IActionHandler } from './action';
23 export interface Dispatch {
24 <TAction extends Action>(action: TAction): TAction;
27 export interface Enhancer<TStoreState> {
28 (store: Store<TStoreState>): Dispatch;
31 class InitialisationAction extends Action { };
32 const initialisationAction = new InitialisationAction();
34 export class Store<TStoreState> {
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;
44 this._isDispatching = false;
46 this.changed = new Event<void>(); // sollten wir hier eventuell sogar den state mit übergeben ?
48 this._actionHandler = actionHandler;
50 this._state = initialState as TStoreState;
51 if (enhancer) this._dispatch = enhancer(this);
53 this._dispatch(initialisationAction);
56 public changed: Event<void>;
58 private _dispatch: Dispatch = <TAction extends Action>(payload: TAction): TAction => {
59 if (payload == null || !(payload instanceof Action)) {
61 'Actions must inherit from type Action. ' +
62 'Use a custom middleware for async actions.'
66 if (this._isDispatching) {
67 throw new Error('ActionHandler may not dispatch actions.');
70 const oldState = this._state;
72 this._isDispatching = true;
73 this._state = this._actionHandler(oldState, payload);
75 this._isDispatching = false;
78 if (this._state !== oldState) {
79 this.changed.invoke();
85 public get dispatch(): Dispatch {
86 return this._dispatch;
93 private _state: TStoreState;
94 private _isDispatching: boolean;
95 private _actionHandler: IActionHandler<TStoreState>;