Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(typings): make dependencies generic type (#250)
Closes #231
  • Loading branch information
rafaelkallis authored and jayphelps committed Aug 8, 2017
1 parent acb3afe commit b690902
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
16 changes: 8 additions & 8 deletions index.d.ts
Expand Up @@ -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;
29 changes: 24 additions & 5 deletions test/typings.ts
Expand Up @@ -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({
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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" },
Expand Down Expand Up @@ -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(() => {});
Expand Down

0 comments on commit b690902

Please sign in to comment.