const handler = (action$: O<A<any>>, options: any): O<A<any>> => { const is = (action: A<any>): boolean => action.type === typeT; const other$: O<A<any>> = action$ .filter(action => !is(action)); const target$: O<A<T>> = action$ .filter(is); const result$: O<A<U>> = dispatch(target$) .filter(a => !!a) .share(); return O.merge(result$, other$); };
return (action$: O<A<any>>, _: any): O<A<any>> => { return O.merge( action$, action$.first().map(() => { runServer(serverOptions, expressHandler); return; // return undefined }) ) .filter(a => !!a) // remove undefined .share(); };
const init = (options: XXXOptions): XXXResponse => { const { fs, tActionType, uActionType } = options; const typeT = tActionType ? tActionType : tActionTypeDefault; const typeU = uActionType ? uActionType : uActionTypeDefault; const dispatch = (action$: O<A<T>>): O<A<U>> => { return action$ .map(({ data }) => fs[data.name](data.params)) .map(data => ({ type: typeU, data })); }; const handler = (action$: O<A<any>>, options: any): O<A<any>> => { const is = (action: A<any>): boolean => action.type === typeT; const other$: O<A<any>> = action$ .filter(action => !is(action)); const target$: O<A<T>> = action$ .filter(is); const result$: O<A<U>> = dispatch(target$) .filter(a => !!a) .share(); return O.merge(result$, other$); }; return { handler }; };
test(t => { t.plan(3); const init: typeof initType = t.context.init; const History: sinon.SinonStub = t.context.History; const go: sinon.SinonStub = t.context.go; const start: sinon.SinonStub = t.context.start; const routes: Route[] = []; const action$ = O.of({ type: 'foo' }); // ignore const options = { re: (): any => null }; init({ routes }).handler(action$, options).subscribe(); t.truthy(History.callCount === 1); t.truthy(start.callCount === 1); t.truthy(go.callCount === 0); });
test(t => { t.plan(4); const init: typeof initType = t.context.init; const History: sinon.SinonStub = t.context.History; const go: sinon.SinonStub = t.context.go; const start: sinon.SinonStub = t.context.start; const routes: Route[] = []; const action$ = O.of<A<any>>({ type: 'go-to', data: '/' }); const options = { re: (): any => null }; init({ routes }).handler(action$, options).subscribe(); t.truthy(History.callCount === 1); t.truthy(start.callCount === 1); t.truthy(go.callCount === 1); t.deepEqual(go.getCall(0).args, ['/']); });
const dispatch = (action$: O<A<T>>): O<A<U>> => { return action$ .map(({ data }) => fs[data.name](data.params)) .map(data => ({ type: typeU, data })); };