-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- expose 'mapStoreToProps' function from store - add 'mapDispatchToProps' functiion
- Loading branch information
Showing
12 changed files
with
2,135 additions
and
3,047 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,38 @@ | ||
import { ActionCreatorDefinition, DispatchToProps, Indexer } from '..' | ||
|
||
export const createAction = <TPayload = any, TMeta = any>( | ||
type: string, | ||
): ActionCreatorDefinition<TPayload, TMeta> => { | ||
const creator: any = (payload?: TPayload, meta?: TMeta) => ({ | ||
payload, | ||
meta, | ||
type, | ||
}) | ||
creator.type = type | ||
return creator | ||
} | ||
|
||
export const mapDispatchToProps: DispatchToProps = map => (dispatch, own) => { | ||
const mapper = <T extends Indexer>(m: T) => | ||
Object.keys(m).reduce( | ||
(prev, key) => ({ | ||
...prev, | ||
[key]: (...params: any[]) => dispatch(m[key](...params)), | ||
}), | ||
{}, | ||
) as typeof m | ||
|
||
return typeof map === 'function' ? mapper(map(dispatch, own)) : mapper(map) | ||
} | ||
import { ActionCreatorDefinition, DispatchToProps, Indexer } from '..' | ||
|
||
export const createAction = <TPayload = any, TMeta = any>( | ||
type: string, | ||
): ActionCreatorDefinition<TPayload, TMeta> => { | ||
const creator: any = (payload?: TPayload, meta?: TMeta) => ({ | ||
payload, | ||
meta, | ||
type, | ||
}) | ||
creator.type = type | ||
return creator | ||
} | ||
|
||
|
||
/** | ||
* Dummy function to return `MapDispatchToPropsParam` type that can be passed to `connect` | ||
* As paramter, either mapper function which takes dispatch object and returns indexer object or indexer object is required | ||
* ex. | ||
* const changeLayout = createAction('changeLayout') | ||
* const dispatchedProps = mapDispatchToProps((dispatch, own) => ({ changeLayout: () => dispatch(changeLayout()) })) | ||
* // or | ||
* const dispatchedProps = mapDispatchToProps({ changeLayout })) | ||
* | ||
* @param {*} map | ||
*/ | ||
export const mapDispatchToProps: DispatchToProps = map => (dispatch, own) => { | ||
const mapper = <T extends Indexer>(m: T) => | ||
Object.keys(m).reduce( | ||
(prev, key) => ({ | ||
...prev, | ||
[key]: (...params: any[]) => dispatch(m[key](...params)), | ||
}), | ||
{}, | ||
) as typeof m | ||
|
||
return typeof map === 'function' ? mapper(map(dispatch, own)) : mapper(map) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
import { Action as ReduxAction } from 'redux' | ||
|
||
export interface Action<TPayload = any, TMeta = any> | ||
extends ReduxAction<string> { | ||
payload: TPayload | ||
meta: TMeta | ||
} | ||
|
||
export interface ActionCreatorDefinition<TPayload, TMeta> { | ||
(payload?: TPayload, meta?: TMeta): Action<TPayload, TMeta> | ||
type: string | ||
} | ||
|
||
export type LazyDispatch = <TAction extends ReduxAction>( | ||
action: TAction, | ||
) => Promise<TAction> | ||
|
||
export type ActionBody<TState, TPayload> = ( | ||
state: TState, | ||
action: TPayload, | ||
dispatch: LazyDispatch, | ||
) => TState | ||
import { Action as ReduxAction } from 'redux' | ||
|
||
export interface Action<TPayload = any, TMeta = any> extends ReduxAction<string> { | ||
payload: TPayload | ||
meta: TMeta | ||
} | ||
|
||
export interface ActionCreatorDefinition<TPayload, TMeta> { | ||
(payload?: TPayload, meta?: TMeta): Action<TPayload, TMeta> | ||
type: string | ||
} | ||
|
||
export type LazyDispatch = <TAction extends ReduxAction>(action: TAction) => Promise<TAction> | ||
|
||
export type ActionBody<TState, TPayload> = ( | ||
state: TState, | ||
action: TPayload, | ||
dispatch: LazyDispatch, | ||
) => TState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { AnyAction } from 'redux' | ||
import { MapDispatchToPropsFunction, MapStateToProps } from 'react-redux' | ||
|
||
export type Indexer<T = any> = { [key: string]: T } | ||
|
||
export interface StateToProps<TState = any> { | ||
<T extends Indexer, TOwn>( | ||
map: MapStateToProps<T, TOwn, TState>, | ||
): MapStateToProps<T, TOwn, TState> | ||
} | ||
|
||
export interface DispatchToProps { | ||
<T extends Indexer<(...params: any[]) => AnyAction>, TOwn>( | ||
map: T | MapDispatchToPropsFunction<T, TOwn>, | ||
): MapDispatchToPropsFunction<T, TOwn> | ||
} | ||
import { MapDispatchToPropsFunction, MapStateToProps } from 'react-redux' | ||
import { AnyAction } from 'redux' | ||
|
||
export type Indexer<T = any> = { [key: string]: T } | ||
|
||
export type StateToProps<TState = any> = { | ||
<T extends Indexer, TOwn>( | ||
map: MapStateToProps<T, TOwn, TState>, | ||
): MapStateToProps<T, TOwn, TState> | ||
} | ||
|
||
export type DispatchToProps = { | ||
<T extends Indexer<(...params: any[]) => AnyAction>, TOwn>( | ||
map: T | MapDispatchToPropsFunction<T, TOwn>, | ||
): MapDispatchToPropsFunction<T, TOwn> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
interface PromiseConstructor { | ||
defer<T>(): { | ||
resolve: (value?: T | PromiseLike<T>) => void | ||
reject: (reason?: any) => void | ||
promise: Promise<T>, | ||
} | ||
} | ||
|
||
Promise['defer'] = function deferPolyfill() { | ||
const deferred = {} as any | ||
const promise = new Promise((resolve, reject) => { | ||
deferred.resolve = resolve | ||
deferred.reject = reject | ||
}) | ||
deferred.promise = promise | ||
return deferred | ||
} | ||
interface PromiseConstructor { | ||
defer<T>(): { | ||
resolve: (value?: T | PromiseLike<T>) => void | ||
reject: (reason?: any) => void | ||
promise: Promise<T>, | ||
} | ||
} | ||
|
||
Promise['defer'] = function deferPolyfill() { | ||
const deferred = {} as any | ||
const promise = new Promise((resolve, reject) => { | ||
deferred.resolve = resolve | ||
deferred.reject = reject | ||
}) | ||
deferred.promise = promise | ||
return deferred | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,51 @@ | ||
import { ActionBody, Action, ActionCreatorDefinition, LazyDispatch } from '.' | ||
import { Dispatch, Reducer } from 'redux' | ||
|
||
export class ReducerBuilder<State = {}> { | ||
private actions: { [type: string]: ActionBody<State, Action> } = {} | ||
private initState: State | ||
|
||
/** | ||
* Initial state of the reducer | ||
* | ||
* @param {State} state State object | ||
* @returns | ||
* @memberof ReducerBuilder | ||
*/ | ||
public init(state: State) { | ||
this.initState = state | ||
return this | ||
} | ||
|
||
/** | ||
* Consumer definition for given action type | ||
* | ||
* @template TPayload Action payload type | ||
* @param {ActionCreatorDefinition<TPayload>} creator Action creator function | ||
* @param {ActionBody<State, Action<TPayload>>} body Action body | ||
* @returns | ||
* @memberof ReducerBuilder | ||
*/ | ||
public handle<TPayload, TMeta>( | ||
creator: ActionCreatorDefinition<TPayload, TMeta>, | ||
body: ActionBody<State, Action<TPayload, TMeta>>, | ||
) { | ||
this.actions[creator.type] = body | ||
return this | ||
} | ||
|
||
private build(dispatchPromise: Promise<Dispatch>): Reducer<State, Action> { | ||
return (state = this.initState, action) => { | ||
const actionBody = this.actions[action.type] | ||
const lazyDispatch: LazyDispatch = nestedAction => | ||
dispatchPromise.then(dispatch => dispatch(nestedAction)) | ||
|
||
if (!!actionBody) { | ||
return actionBody(state, action, lazyDispatch) | ||
} | ||
|
||
return state | ||
} | ||
} | ||
} | ||
import { Dispatch, Reducer } from 'redux' | ||
|
||
import { Action, ActionBody, ActionCreatorDefinition, LazyDispatch } from '.' | ||
|
||
export class ReducerBuilder<State = {}> { | ||
private actions: { [type: string]: ActionBody<State, Action> } = {} | ||
private initState: State | ||
|
||
/** | ||
* Initial state of the reducer | ||
* | ||
* @param {State} state State object | ||
* @returns | ||
* @memberof ReducerBuilder | ||
*/ | ||
public init(state: State) { | ||
this.initState = state | ||
return this | ||
} | ||
|
||
/** | ||
* Consumer definition for given action type | ||
* | ||
* @template TPayload Action payload type | ||
* @param {ActionCreatorDefinition<TPayload>} creator Action creator function | ||
* @param {ActionBody<State, Action<TPayload>>} body Action body | ||
* @returns | ||
* @memberof ReducerBuilder | ||
*/ | ||
public handle<TPayload, TMeta>( | ||
creator: ActionCreatorDefinition<TPayload, TMeta>, | ||
body: ActionBody<State, Action<TPayload, TMeta>>, | ||
) { | ||
this.actions[creator.type] = body | ||
return this | ||
} | ||
|
||
private build(dispatchPromise: Promise<Dispatch>): Reducer<State, Action> { | ||
return (state = this.initState, action) => { | ||
const actionBody = this.actions[action.type] | ||
const lazyDispatch: LazyDispatch = nestedAction => | ||
dispatchPromise.then(dispatch => dispatch(nestedAction)) | ||
|
||
if (!!actionBody) { | ||
return actionBody(state, action, lazyDispatch) | ||
} | ||
|
||
return state | ||
} | ||
} | ||
} |
Oops, something went wrong.