Skip to content

Commit

Permalink
add bundle transform type
Browse files Browse the repository at this point in the history
  • Loading branch information
wangtao committed Nov 10, 2017
1 parent 15bf8b4 commit feb573c
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 36 deletions.
19 changes: 3 additions & 16 deletions src/core/enhancedRedux/createPropsPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import {
RootState
} from "../reducers/types";

function defaultPropsPicker<S>(
{ _arenaScene: state }: StateDict<S>,
{ _arenaScene: actions }: { _arenaScene: {} }
): DefaultPickedProps<S> {
return Object.assign({}, state, {
actions
});
}

function getRelativeLevel(name: string) {
let result = name.match(/^\$(\d+)$/);
return result && parseInt(result[1]);
Expand All @@ -34,22 +25,18 @@ function getLevelState(
return stateTree.getIn(path);
}

export type DefaultPickedProps<S> = {
actions: ActionCreatorsMapObject;
} & S;

export default function createPropsPicker<
S,
A extends ActionCreatorsMapObject,
P = DefaultPickedProps<S>
P
>(
propsPicker: PropsPicker<P, S, A, Partial<P>> = defaultPropsPicker,
propsPicker: PropsPicker<P, S, A, Partial<P>>,
reduxInfo: CurtainReduxInfo<S>,
mutableObj: CurtainMutableObj
) {
let { arenaReducerDict } = reduxInfo;
let sceneReducerKey = arenaReducerDict._arenaScene.reducerKey;
let latestProps: Partial<P> | Partial<DefaultPickedProps<S>>;
let latestProps: Partial<P>;
let stateHandler = {
get: function(target: { state: any }, name: string) {
let levelNum = getRelativeLevel(name);
Expand Down
3 changes: 2 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export {
ReducerFactory,
DefaultSceneActions,
ActionsDict,
StateDict
StateDict,
PropsPicker
} from "./types";
export {
arenaReducer,
Expand Down
11 changes: 3 additions & 8 deletions src/core/sagas/sceneReduxSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ import {
SceneBundle
} from "../types";
import { CurtainReduxInfo } from "../reducers/types";
import { ConnectedAction } from "build/core/types/actions";

const defaultActions = {
setState: (state: any) => ({ type: ActionTypes.ARENA_SCENE_SET_STATE, state })
};

function bindActions(
actions: ActionCreatorsMapObject | null | undefined,
actions: ActionCreatorsMapObject,
reducerKey: string,
dispatch: Dispatch<any>,
isSceneActions: boolean
) {
if (isSceneActions === false) {
return bindActionCreators(actions || defaultActions, dispatch);
return bindActionCreators(actions, dispatch);
} else {
return bindArenaActionCreators(
actions || defaultActions,
actions,
dispatch,
reducerKey
);
Expand Down
9 changes: 2 additions & 7 deletions src/core/types/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ export type SceneBundleOptions = {
isSceneReducer?: boolean;
};

export type SceneBundle<
P,
S = {},
A extends ActionCreatorsMapObject = { setState: (state: Partial<S>) => void },
PP = DefaultPickedProps<S>
> = {
export type SceneBundle<P, S, A extends ActionCreatorsMapObject, PP> = {
Component: ComponentType<P>;
state: S;
actions: ActionCreatorsMapObject;
actions: A;
propsPicker: PropsPicker<P, S, A, PP>;
saga?: (...params: any[]) => any;
reducer: SceneReducer<S>;
Expand Down
38 changes: 34 additions & 4 deletions src/tools/bundleToComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@ import * as React from "react";
import { ActionCreatorsMapObject } from "redux";
import { SceneBundle } from "../core";
import { ArenaSceneExtraProps, ArenaScene } from "../hocs";
import { Omit } from "./types";
import { Omit, SceneBundleNoS, SceneBundleNoA, SceneBundleNoPP } from "./types";
import { defaultPropsPicker } from "./commons";

export default function<P extends PP, S, A extends ActionCreatorsMapObject, PP>(
function isActionsEmpty(
actions: ActionCreatorsMapObject | undefined
): actions is ActionCreatorsMapObject {
return actions != null;
}

function bundleToComponent<
P extends PP,
S,
A extends ActionCreatorsMapObject,
PP
>(
bundle: SceneBundle<P, S, A, PP>,
extraProps?: ArenaSceneExtraProps
) {
): React.SFC<Omit<P, keyof PP>>;
function bundleToComponent<P extends PP, A extends ActionCreatorsMapObject, PP>(
bundle: SceneBundleNoS<P, A, PP>,
extraProps?: ArenaSceneExtraProps
): React.SFC<Omit<P, keyof PP>>;
function bundleToComponent<P extends PP, S, PP>(
bundle: SceneBundleNoA<P, S, PP>,
extraProps?: ArenaSceneExtraProps
): React.SFC<Omit<P, keyof PP>>;
function bundleToComponent<P extends S, S, A extends ActionCreatorsMapObject>(
bundle: SceneBundleNoPP<P, S, A>,
extraProps?: ArenaSceneExtraProps
): React.SFC<Omit<P, keyof S>>;
function bundleToComponent(
bundle: any,
extraProps?: ArenaSceneExtraProps
): any {
let newBundle= Object.assign({}, {}, bundle);
let WrapperClass: React.SFC<Omit<P, keyof PP>> = props => (
<ArenaScene sceneBundle={bundle} sceneProps={props} {...extraProps} />
);
WrapperClass.displayName = "ScenePropsProxy";
return WrapperClass;
return WrapperClass as any;
}
export default bundleToComponent;
15 changes: 15 additions & 0 deletions src/tools/commons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { StateDict } from "../core";
import { DefaultPickedProps } from "./types";

export function defaultPropsPicker<S>(
{ _arenaScene: state }: StateDict<S>,
{ _arenaScene: actions }: { _arenaScene: {} }
): DefaultPickedProps<S> {
return Object.assign({}, state, {
actions
});
}

const defaultActions = {
setState: (state: any) => ({ type: ActionTypes.ARENA_SCENE_SET_STATE, state })
};
8 changes: 8 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
export { default as bundleToComponent } from "./bundleToComponent";
export { default as bundleToElement } from "./bundleToElement";
export {
DefaultPickedProps,
Diff,
Omit,
DefaultState,
SceneBundleNoS,
SceneBundleBase
} from "./types";
66 changes: 66 additions & 0 deletions src/tools/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
import { ComponentType } from "react";
import { ActionCreatorsMapObject } from "redux";
import { PropsPicker, SceneReducer, SceneBundleOptions } from "../core";

export type Diff<T extends string, U extends string> = ({ [P in T]: P } &
{ [P in U]: never } & { [x: string]: never })[T];
export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;

export type DefaultPickedProps<S, A extends ActionCreatorsMapObject> = {
actions: A;
} & S;

export type DefaultState = {};

export type DefaultActions<S> = { setState: (state: Partial<S>) => void };

export type SceneBundleBase<P> = {
Component: ComponentType<P>;
saga?: (...params: any[]) => any;
options?: SceneBundleOptions;
};

export type SceneBundleNoS<P, A extends ActionCreatorsMapObject, PP> = {
actions: A;
propsPicker: PropsPicker<P, DefaultState, A, PP>;
reducer: SceneReducer<DefaultState>;
} & SceneBundleBase<P>;

export type SceneBundleNoR<P, S, A extends ActionCreatorsMapObject, PP> = {
state: S;
actions: A;
propsPicker: PropsPicker<P, S, A, PP>;
};

export type SceneBundleNoPP<P, S, A extends ActionCreatorsMapObject> = {
state: S;
actions: A;
reducer: SceneReducer<DefaultState>;
} & SceneBundleBase<P>;

export type SceneBundleNoA<P, S, PP> = {
state: S;
reducer: SceneReducer<DefaultState>;
propsPicker: PropsPicker<P, DefaultState, DefaultActions<S>, PP>;
} & SceneBundleBase<P>;

export type SceneBundleNoSR<P, A extends ActionCreatorsMapObject, PP> = {
actions: A;
propsPicker?: PropsPicker<P, DefaultState, DefaultActions<DefaultState>, PP>;
} & SceneBundleBase<P>;

export type SceneBundleNoSPP<P, S, A extends ActionCreatorsMapObject> = {
actions: A;
reducer: SceneReducer<S>;
} & SceneBundleBase<P>;

export type SceneBundleNoSA<P, PP> = {
reducer: SceneReducer<DefaultState>;
propsPicker: PropsPicker<P, DefaultState, DefaultActions<DefaultState>, PP>;
} & SceneBundleBase<P>;

export type SceneBundleNoAPP<P, S> = {
state: S;
reducer: SceneReducer<S>;
} & SceneBundleBase<P>;

export type SceneBundleNoASPP<P> = {
reducer: SceneReducer<DefaultState>;
} & SceneBundleBase<P>;

0 comments on commit feb573c

Please sign in to comment.