Skip to content

Commit

Permalink
fix: Meet TS requirements for stateParamsObserver HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
wms committed Sep 17, 2017
1 parent 10d396a commit 120a5bf
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/hoc/stateParamsObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import { Subscription } from 'rxjs/Subscription';
import { equals } from 'ramda';
import 'rxjs/add/operator/distinctUntilChanged';

interface Params {
export interface Params {
[paramName: string]: any;
}

interface State {
export type ParamMapper = <P extends any, C extends any>(params: Params, props: P, context: C) => any;

const defaultParamMapper: ParamMapper = (params, props, context) =>
({ ...params, ...props as object } as typeof props);

export interface State {
mappedProps: any;
}

interface Context {
export interface Context {
router: UIRouterReact;
}

export type ParamMapper = <P extends any, C extends any>(params: Params, props: P, context: C) => any;

const defaultParamMapper: ParamMapper = (params, props, context) =>
({ ...params, ...props as object } as typeof props);

export const stateParamsObserver =
<P, SP>(Component: React.ComponentType<P & SP>, mapParamsToProps = defaultParamMapper) =>
class StateParamsObserver extends React.Component<P, State> {
context: Context;

protected _paramsObserver: Subscription;
paramsObserver: Subscription;

static contextTypes = {
...Component.contextTypes,
Expand All @@ -54,17 +54,17 @@ export const stateParamsObserver =
throw Error('router.globals.params$ does not exist - have you installed the @ui-router/rx plugin?');
}

this._paramsObserver = params$
this.paramsObserver = params$
.map(params => mapParamsToProps(params, this.props, this.context))
.distinctUntilChanged(equals)
.subscribe(this._onMappedPropsChange);
.subscribe(this.onMappedPropsChange);
}

componentWillUnmount() {
this._paramsObserver.unsubscribe();
this.paramsObserver.unsubscribe();
}

protected _onMappedPropsChange = (mappedProps: any) => {
onMappedPropsChange = (mappedProps: any) => {
this.setState({ mappedProps });
}
}

0 comments on commit 120a5bf

Please sign in to comment.