diff options
Diffstat (limited to 'sdnr/wt/odlux/framework/src/flux')
-rw-r--r-- | sdnr/wt/odlux/framework/src/flux/action.ts | 18 | ||||
-rw-r--r-- | sdnr/wt/odlux/framework/src/flux/connect.ts | 26 | ||||
-rw-r--r-- | sdnr/wt/odlux/framework/src/flux/middleware.ts | 6 |
3 files changed, 32 insertions, 18 deletions
diff --git a/sdnr/wt/odlux/framework/src/flux/action.ts b/sdnr/wt/odlux/framework/src/flux/action.ts index 8a90f24b2..76890257e 100644 --- a/sdnr/wt/odlux/framework/src/flux/action.ts +++ b/sdnr/wt/odlux/framework/src/flux/action.ts @@ -1,4 +1,22 @@ /** + * ============LICENSE_START======================================================================== + * ONAP : ccsdk feature sdnr wt odlux + * ================================================================================================= + * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved. + * ================================================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + * ============LICENSE_END========================================================================== + */ + +/** * Represents an action in the odlux flux architecture. */ export abstract class Action { } diff --git a/sdnr/wt/odlux/framework/src/flux/connect.ts b/sdnr/wt/odlux/framework/src/flux/connect.ts index fca7df6fa..f54e4e0f0 100644 --- a/sdnr/wt/odlux/framework/src/flux/connect.ts +++ b/sdnr/wt/odlux/framework/src/flux/connect.ts @@ -38,19 +38,15 @@ interface IDispatchProps { dispatch: Dispatch; } -type FuncInfer<T> = { - ([...args]: any): T; -}; - -type FunctionResult<T> = T extends FuncInfer<infer U> ? U : never; +type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> type ComponentDecoratorInfer<TMergedProps> = { - <TProps>(wrappedComponent: React.ComponentType<TProps & TMergedProps>): React.ComponentClass<TProps>; + <TProps>(wrappedComponent: React.ComponentType<TProps & TMergedProps>): React.ComponentClass<Omit<TProps & TMergedProps, keyof TMergedProps>>; }; -export type Connect<TMapProps = undefined, TMapDispatch = undefined> = - (TMapProps extends undefined ? IApplicationStoreProps : FunctionResult<TMapProps>) & - (TMapDispatch extends undefined ? IDispatchProps : FunctionResult<TMapDispatch>); +export type Connect<TMapProps extends ((...args: any) => any) | undefined = undefined, TMapDispatch extends ((...args: any) => any) | undefined = undefined> = + (TMapProps extends ((...args: any) => any) ? ReturnType<TMapProps> : IApplicationStoreProps) & + (TMapDispatch extends ((...args: any) => any) ? ReturnType<TMapDispatch> : IDispatchProps); export function connect(): ComponentDecoratorInfer<IApplicationStoreProps & IDispatchProps>; @@ -73,9 +69,9 @@ export function connect<TDispatchProps>( export function connect<TProps, TStateProps, TDispatchProps>( mapStateToProps?: ((state: IApplicationStoreState) => TStateProps), mapDispatchToProps?: ((dispatcher: IDispatcher) => TDispatchProps) -) : +): ((WrappedComponent: React.ComponentType<TProps & (IApplicationStoreProps | TStateProps) & IDispatchProps>) => React.ComponentType<TProps>) { - + const injectApplicationStore = (WrappedComponent: React.ComponentType<TProps & (IApplicationStoreProps | TStateProps) & IDispatchProps>): React.ComponentType<TProps> => { class StoreAdapter extends React.Component<TProps, {}> { @@ -83,7 +79,7 @@ export function connect<TProps, TStateProps, TDispatchProps>( context: IApplicationStoreContext; render(): JSX.Element { - + if (isWrappedComponentIsVersion1(WrappedComponent)) { const element = React.createElement(WrappedComponent, { ...(this.props as any), state: this.store.state, dispatch: this.store.dispatch.bind(this.store) }); return element; @@ -91,10 +87,10 @@ export function connect<TProps, TStateProps, TDispatchProps>( const element = React.createElement(WrappedComponent, { ...(this.props as any), ...(mapStateToProps(this.store.state) as any), dispatch: this.store.dispatch.bind(this.store) }); return element; } else if (mapStateToProps && mapDispatchToProps && isWrappedComponentIsVersion3(WrappedComponent)) { - const element = React.createElement(WrappedComponent, { ...(this.props as any), ...(mapStateToProps(this.store.state) as any), ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store)}) as any) }); + const element = React.createElement(WrappedComponent, { ...(this.props as any), ...(mapStateToProps(this.store.state) as any), ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store) }) as any) }); return element; } else if (!mapStateToProps && mapDispatchToProps && isWrappedComponentIsVersion4(WrappedComponent)) { - const element = React.createElement(WrappedComponent, { ...(this.props as any), state: this.store.state, ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store)}) as any) }); + const element = React.createElement(WrappedComponent, { ...(this.props as any), state: this.store.state, ...(mapDispatchToProps({ dispatch: this.store.dispatch.bind(this.store) }) as any) }); return element; } throw new Error("Invalid arguments in connect."); @@ -158,7 +154,7 @@ export class ApplicationStoreProvider extends React.Component<ApplicationStorePr } render(): JSX.Element { - return React.Children.only(this.props.children); + return React.Children.only(this.props.children) as any; //type error, fix when possible } } diff --git a/sdnr/wt/odlux/framework/src/flux/middleware.ts b/sdnr/wt/odlux/framework/src/flux/middleware.ts index 6179f3bcf..de6505c4f 100644 --- a/sdnr/wt/odlux/framework/src/flux/middleware.ts +++ b/sdnr/wt/odlux/framework/src/flux/middleware.ts @@ -35,7 +35,7 @@ export type ActionHandlerMapObject<S extends { [key: string]: any }, A extends A } export const combineActionHandler = <TState extends { [key: string]: any }, TAction extends Action = Action>(actionHandlers: ActionHandlerMapObject<TState, TAction>) : IActionHandler<TState, TAction> => { - const finalActionHandlers: ActionHandlerMapObject<TState> = {} as ActionHandlerMapObject<TState>; + const finalActionHandlers = {} as { [key: string]: any }; // https://github.com/microsoft/TypeScript/issues/31808 Object.keys(actionHandlers).forEach(actionHandlerKey => { const handler = actionHandlers[actionHandlerKey]; if (typeof handler === 'function') { @@ -55,7 +55,7 @@ export const combineActionHandler = <TState extends { [key: string]: any }, TAct return function combination<TAction extends Action>(state: TState = ({} as TState), action: TAction) { let hasChanged = false; - const nextState : TState = {} as TState; + const nextState = {} as { [key: string]: any }; // https://github.com/microsoft/TypeScript/issues/31808 Object.keys(finalActionHandlers).forEach(key => { const actionHandler = finalActionHandlers[key]; const previousState = state[key]; @@ -76,7 +76,7 @@ export const chainMiddleware = <TStoreState>(...middlewares: Middleware<TStoreSt const middlewareAPI = { getState() { return store.state }, dispatch: <TAction extends Action>(action: TAction) => store.dispatch(action) // we want to use the combinded dispatch - // we should NOT use the flux dispatcher here, since the action would affect ALL stores + // we should NOT use the flux dispatcher here, since the action would affect ALL stores }; const chain = middlewares.map(middleware => middleware(middlewareAPI)); return compose(...chain)(store.dispatch) as Dispatch; |