From bd08832e14cd61edc737a5d7793ee10ec130a738 Mon Sep 17 00:00:00 2001 From: Dimitri Rosenberg Date: Sun, 28 Feb 2016 23:57:22 +0300 Subject: [PATCH] add apply effect creater --- redux-saga/redux-saga-tests.ts | 6 +++--- redux-saga/redux-saga.d.ts | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/redux-saga/redux-saga-tests.ts b/redux-saga/redux-saga-tests.ts index 34f91310b..ce0591549 100644 --- a/redux-saga/redux-saga-tests.ts +++ b/redux-saga/redux-saga-tests.ts @@ -16,6 +16,7 @@ import { put, race, call, + apply, fork, select, cancel, @@ -55,13 +56,12 @@ namespace GettingStarted { } } - namespace EffectCombinators { const fetchPostsWithTimeout:Saga = function* fetchPostsWithTimeout() { while( yield take('FETCH_POSTS') ) { // starts a race between 2 effects const {posts, timeout} = yield race({ - posts : call(fetchApi, '/posts'), + posts : call([this, fetchApi], '/posts'), timeout : call(delay, 1000) }) @@ -140,7 +140,7 @@ namespace TaskCancellation { try { while(true) { yield put({type: 'REQUEST_START'}) - const result = yield call(someApi) + const result = yield apply(this, someApi) yield put({type: 'REQUEST_SUCCESS', result}) yield call(delay, 5000) } diff --git a/redux-saga/redux-saga.d.ts b/redux-saga/redux-saga.d.ts index 3df94243e..e68a2a256 100644 --- a/redux-saga/redux-saga.d.ts +++ b/redux-saga/redux-saga.d.ts @@ -55,6 +55,12 @@ declare module 'redux-saga/effects' { export type Effect = {}; + type EffectFunction = (arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]) => Promise | Iterable; + + interface EffectFunctionContext { + 0: any; + 1: EffectFunction; + } export function take(pattern?: Pattern): Effect; @@ -62,14 +68,17 @@ declare module 'redux-saga/effects' { export function race(effects: {[key:string]: any}): Effect; - export function call(fn: (arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]) => any, - arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; + export function call(fn: EffectFunction, arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; + export function call(fn: EffectFunctionContext, arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; + + export function apply(context: any, fn: EffectFunction, arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; export function fork(effect: Effect): Effect; - export function fork(fn: (arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]) => - Promise|Iterable, - arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; + + export function fork(fn: EffectFunction, arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; + + export function fork(fn: EffectFunctionContext, arg1?: T1, arg2?: T2, arg3?: T3, ...rest: any[]): Effect; export function join(task: Task): Effect;