Permalink
Browse files

feat(typings): make dependencies generic type (#250)

Closes #231
  • Loading branch information...
rafaelkallis authored and jayphelps committed Aug 8, 2017
1 parent acb3afe commit b69090208fd46d880b2db863740a9aa163661262
Showing with 32 additions and 13 deletions.
  1. +8 −8 index.d.ts
  2. +24 −5 test/typings.ts
@@ -28,25 +28,25 @@ export declare class ActionsObservable<T> extends Observable<T> {
ofType(...key: any[]): ActionsObservable<T>;
}
export declare interface Epic<T, S> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>): Observable<T>;
export declare interface Epic<T, S, D = any> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>, dependencies: D): Observable<T>;
}
export interface EpicMiddleware<T, S> extends Middleware {
replaceEpic(nextEpic: Epic<T, S>): void;
export interface EpicMiddleware<T, S, D = any> extends Middleware {
replaceEpic(nextEpic: Epic<T, S, D>): void;
}
interface Adapter {
input: (input$: Observable<any>) => any;
output: (output$: any) => Observable<any>;
}
interface Options {
interface Options<D = any> {
adapter?: Adapter;
dependencies?: { [key: string]: any } | any;
dependencies?: D;
}
export declare function createEpicMiddleware<T, S>(rootEpic: Epic<T, S>, options?: Options): EpicMiddleware<T, S>;
export declare function createEpicMiddleware<T, S, D = any>(rootEpic: Epic<T, S, D>, options?: Options<D>): EpicMiddleware<T, S, D>;
export declare function combineEpics<T, S>(...epics: Epic<T, S>[]): Epic<T, S>;
export declare function combineEpics<T, S, D = any>(...epics: Epic<T, S, D>[]): Epic<T, S, D>;
export declare function combineEpics<E>(...epics: E[]): E;
@@ -22,6 +22,10 @@ interface FluxStandardAction {
meta?: any
}
interface Dependencies {
func(value: string): string;
}
const epic1: Epic<FluxStandardAction, State> = (action$, store) =>
action$.ofType('FIRST')
.mapTo({
@@ -59,11 +63,22 @@ const epic6: Epic<FluxStandardAction, State> = (action$, store) =>
payload
}));
const rootEpic1: Epic<FluxStandardAction, State> = combineEpics<FluxStandardAction, State>(epic1, epic2, epic3, epic4, epic5, epic6);
const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6);
const epic7: Epic<FluxStandardAction, State, Dependencies> = (action$, store, dependencies) =>
action$.ofType('SEVENTH')
.map(({ type, payload}) => ({
type: 'seventh',
payload: dependencies.func(payload)
}));
const epicMiddleware1: EpicMiddleware<FluxStandardAction, State> = createEpicMiddleware<FluxStandardAction, State>(rootEpic1);
const epicMiddleware2 = createEpicMiddleware(rootEpic2);
const rootEpic1: Epic<FluxStandardAction, State> = combineEpics<FluxStandardAction, State>(epic1, epic2, epic3, epic4, epic5, epic6, epic7);
const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6, epic7);
const dependencies: Dependencies = {
func(value: string) { return `func-${value}`}
}
const epicMiddleware1: EpicMiddleware<FluxStandardAction, State> = createEpicMiddleware<FluxStandardAction, State>(rootEpic1, { dependencies });
const epicMiddleware2 = createEpicMiddleware(rootEpic2, { dependencies });
interface CustomEpic<T, S, U> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>, api: U): Observable<T>;
@@ -102,6 +117,7 @@ store.dispatch({ type: 'FIRST' });
store.dispatch({ type: 'SECOND' });
store.dispatch({ type: 'FIFTH', payload: 'fifth-payload' });
store.dispatch({ type: 'SIXTH', payload: 'sixth-payload' });
store.dispatch({ type: 'SEVENTH', payload: 'seventh-payload' });
expect(store.getState()).to.deep.equal([
{ "type": "@@redux/INIT" },
@@ -138,7 +154,10 @@ expect(store.getState()).to.deep.equal([
{ "type": "fifth", "payload": "fifth-payload" },
{ "type": "SIXTH", "payload": "sixth-payload" },
{ "type": "sixth", "payload": "sixth-payload" },
{ "type": "sixth", "payload": "sixth-payload" }
{ "type": "sixth", "payload": "sixth-payload" },
{ "type": "SEVENTH", "payload": "seventh-payload" },
{ "type": "seventh", "payload": "func-seventh-payload" },
{ "type": "seventh", "payload": "func-seventh-payload" }
]);
const input$ = Observable.create(() => {});

0 comments on commit b690902

Please sign in to comment.