Merge pull request #17980 from BendingBender/redux-doghouse

[redux-doghouse] introduce typings for redux-doghouse package
This commit is contained in:
Ryan Cavanaugh
2017-07-14 00:09:03 -07:00
committed by GitHub
5 changed files with 148 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
// Type definitions for redux-doghouse 1.0
// Project: https://github.com/DataDog/redux-doghouse#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export as namespace ReduxDoghouse;
import { ActionCreator, ActionCreatorsMapObject, bindActionCreators, Dispatch, Reducer } from 'redux';
export class ScopedActionFactory<A extends ActionCreator<any> | ActionCreatorsMapObject> {
constructor(actionCreator: A);
scope(id: string | number): A;
}
export interface ActionFactoriesMap {
[key: string]: ScopedActionFactory<any> | ActionCreator<any> | ActionFactoriesMap;
}
export function bindActionCreatorsDeep<M extends ActionFactoriesMap, S>(actionFactories: M,
dispatch: Dispatch<S>): M;
export function bindActionCreatorsDeep<A extends ActionCreator<any>, S>(actionFactories: A,
dispatch: Dispatch<S>): A;
export function bindActionCreatorsDeep<A extends ActionCreator<any> | ActionCreatorsMapObject,
F extends ScopedActionFactory<A>, S>(actionFactories: F,
dispatch: Dispatch<S>): F;
export function bindScopedActionFactories<M extends ActionFactoriesMap, S>(actionFactories: M,
dispatch: Dispatch<S>,
bindFn?: typeof bindActionCreators): M;
export function bindScopedActionFactories<A extends ActionCreator<any>, S>(actionFactories: A,
dispatch: Dispatch<S>,
bindFn?: typeof bindActionCreators): A;
export function bindScopedActionFactories<A extends ActionCreator<any> | ActionCreatorsMapObject,
F extends ScopedActionFactory<A>, S>(actionFactories: F,
dispatch: Dispatch<S>,
bindFn?: typeof bindActionCreators): F;
export function scopeActionCreators<A extends ActionCreator<any> | ActionCreatorsMapObject>(actionCreator: A, scopeID: string | number): A;
export function scopeReducers(reducers?: { [key: string]: Reducer<any> }): Reducer<any>;
+5
View File
@@ -0,0 +1,5 @@
{
"dependencies": {
"redux": "^3.7.1"
}
}
@@ -0,0 +1,79 @@
import { Action, ActionCreator, createStore, Dispatch } from 'redux';
import {
bindActionCreatorsDeep,
bindScopedActionFactories,
scopeActionCreators,
ScopedActionFactory,
scopeReducers
} from 'redux-doghouse';
// ==== scopeActionCreators
function fooActionCreator(bar: string): { [key: string]: any } {
return {};
}
const actionCreators = {
foo: fooActionCreator
};
scopeActionCreators(actionCreators, 'a').foo('bar');
scopeActionCreators(fooActionCreator, 'a')('bar');
scopeActionCreators(fooActionCreator, 1)('bar');
// ==== ScopedActionFactory
const scopeableActionsFromCreators = new ScopedActionFactory(actionCreators);
const actionCreatorsScopedToA = scopeableActionsFromCreators.scope('a');
actionCreatorsScopedToA.foo('bar');
const scopeableActionsFromFooCreator = new ScopedActionFactory(fooActionCreator);
const actionCreatorScopedToB = scopeableActionsFromFooCreator.scope('b');
actionCreatorScopedToB('bar');
// ==== bindScopedActionFactories
const store = createStore((state: any, action: Action) => state);
const scopeableActionsFactories = {
myComponentActions: new ScopedActionFactory(actionCreators),
otherActions: {
fooAction: new ScopedActionFactory(fooActionCreator),
plainFooAction: fooActionCreator,
},
};
const boundScopeableActions = bindScopedActionFactories(scopeableActionsFactories, store.dispatch);
boundScopeableActions['myComponentActions'].scope('bar');
boundScopeableActions['otherActions']['plainFooAction']('bar');
const boundActions = bindScopedActionFactories(actionCreators, store.dispatch);
boundActions.foo('bar');
bindScopedActionFactories(fooActionCreator, store.dispatch, (a: ActionCreator<any>, d: Dispatch<any>) => a)('bar');
// ==== bindActionCreatorsDeep
const boundScopeableActionFactories = bindActionCreatorsDeep(scopeableActionsFactories, store.dispatch);
boundScopeableActionFactories['myComponentActions'].scope('bar');
boundScopeableActionFactories['otherActions']['plainFooAction']('bar');
const boundActionFactories = bindActionCreatorsDeep(actionCreators, store.dispatch);
boundActionFactories.foo('bar');
bindActionCreatorsDeep(fooActionCreator, store.dispatch)('bar');
// ==== scopeReducers
const reducers = {
foo: (state = 0, action: Action) => {
return state;
},
};
const actionCreatorsA = scopeActionCreators({
incrementFoo: () => ({type: 'INCREMENT_FOO'})
}, 'a');
const scopedReducers = scopeReducers(reducers);
const state = {
a: {foo: 0},
b: {foo: 2}
};
const newState = scopedReducers(state, actionCreatorsA.incrementFoo());
const aScopedFoo: number = newState.a.foo;
const bScopedFoo: number = newState.b.foo;
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"redux-doghouse-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }