2016-06-22 8 views
0

노드 6.2.1에서 다음 코드를 실행하려고합니다. 1, 2,로 기록한 다음 연결됩니다. 왜 선회 수확 ('TEST')에 대한 실행을 계속하지 않는지 나는 undestand 수 없습니다 ... 다른 사가 완료 및 로그 2하지만 제어가 rootSaga로 반환되지 않습니다 것 같습니다. 누구든지 제발 도와 주실 래요?Redux-saga가 yield call에 걸렸습니다.

const {runSaga, delay} = require('redux-saga'); 
const {take, put, call} = require('redux-saga/effects'); 

function* anotherSaga() { 
    yield call(delay, 1000); 
    console.log(2); 
} 

function* rootSaga() { 
    while(true) { 
    console.log(1); 
    yield call(anotherSaga); 
    console.log(3); 
    const action = yield take('TEST'); 
    console.log(4); 
    yield put(action); 
    } 
} 

runSaga(
    rootSaga(), 
    { 
    subscribe(callback) { 
     return setInterval(() => (callback({type: 'TEST'})), 1000); 
    }, 
    dispatch(action) { 
     console.log(action); 
    }, 
    getState() {} 
    } 
); 

업데이트 : runSaga없이하지만 코드는 다음과 같이이는 subscribe 함수가 올바른 값을 반환하지 않습니다 때문입니다 것 같습니다 로깅 1,2,3,4

const {createStore, applyMiddleware} = require('redux'); 
const createSagaMiddleware = require('redux-saga').default; 
const {delay} = require('redux-saga'); 
const {take, put, call} = require('redux-saga/effects'); 

function* anotherSaga() { 
    yield call(delay, 2000); 
    console.log(2); 
} 

function* rootSaga() { 
    while(true) { 
    console.log(1); 
    yield call(anotherSaga); 
    console.log(3); 
    const action = yield take('TEST'); 
    console.log(4); 
    yield put(action); 
    console.log('---') 
    } 
} 

const rootReducer = (state, action) => { 
    if (state === undefined) { 
    return {}; 
    } 

    return state; 
} 

const sagaMiddleware = createSagaMiddleware(); 
const store = createStore(rootReducer, {}, applyMiddleware(sagaMiddleware)); 
sagaMiddleware.run(rootSaga); 

setInterval(() => (store.dispatch({type: 'TEST'})), 1000); 
+0

runSaga없이이 코드를 사용해 보셨습니까? – kuy

+0

@kuy 시도했는데 예상대로 작동합니다. 주요 주제의 코드를 확인하십시오. –

답변

0

예상 작동합니다. 위에는 setInterval의 결과가 반환되며 이는 간격의 ID가됩니다. 대신 redux-saga가 docs에 따라 이벤트 구독을 취소하는 데 사용할 수있는 함수를 반환해야합니다. 그래서 subscribe 기능은 더 다음과 같이한다 :이 그것을 실행

subscribe(callback) { 
    const intervalId = setInterval(() => (callback({type: 'TEST'})), 1000); 
    return() => { clearInterval(intervalId); }; 
}, 

, 나는

1 
2 
3 
4 
{ type: 'TEST' } 

이 당신의 무용담이 붙어 될 원인이 어떻게 이상한 인쇄를 통해 그것을 루프를 볼 수 있었지만 나는 에서 unsubscribe 함수를받지 못하면 redux-saga에서 내부적으로 엉망이되어 버렸다고 가정합니다.

+0

고마워요! 나는 그것을 놓쳤다. 그러나 yelouafi [github에 추가] (https://github.com/yelouafi/redux-saga/issues/401#issuecomment-228019459)에서 clearInterval을 반환하는 것으로 충분하지 않습니다. "구독 기능은 여러 구독 등록을 지원해야합니다." –

+0

특정 구독에 대한 intervalId가 우리가 구축 한 구독 취소 기능에 의해 생성 된 클로저에 캡처되기 때문에 실제로 작동 할 것이라고 생각합니다. 따라서 구독을 여러 번 호출하고 각 함수를 반환하는 함수로 개별적으로 구독을 취소 할 수 있습니다 (각 함수는 적절한 intervalId에 바인딩됩니다). – rayd