2017-09-26 8 views
0

Redux Saga의 부작용과 같은 것을 통해 eventChannel을 취소 할 수 있습니까? 중포 기지 실시간 데이터베이스이 경우, 외부 이벤트/데이터 스트림에 "child_added" 이벤트 연결React Redux Saga 이벤트 채널 취소

주어진 eventChannel :

// action 
const types = { SYNC: 'SYNC_TODOS' }; 
function syncTodos(todos) { 
    return { types: types.SYNC, todos } 
} 

// saga 
function todosChannel() { 
    // firebase database ref 
    const ref = firebase.database().ref('todos/'); 

    const channel = eventChannel(emit => { 
    const callback = ref.on('child_added', (data) => { 
     emit({ snapshot: data, value: data.val() }) 
    }); 

    // unsubscribe function 
    return() => ref.off('child_added', callback); 
    }); 

    return channel; 
} 

function* sync() { 
    const channel = yield call(todosChannel); 

    try { 
    while (true) { 
     const { value } = yield take(todosChannel); 
     yield put(actions.syncTodos(value)); 
    } 
    } 
    finally { 
    if(yield cancelled()) { 
     channel.close(); 
    } 
    } 
} 

export default function* rootSaga() { 
    yield fork(sync); 
} 

을 같은과() 등의 포크 효과 측면을 사용하는 방법은 없나요 이벤트 채널을 취소하고 Firebase "child_added" 이벤트/데이터 스트림 수신 대기 작업을 수신하려면 takeEvery()를 사용 하시겠습니까? 아니면 어떻게 든 채널에 대한 참조를 저장하고 채널 참조 자체에서 cancel()을 실행해야합니까?

제공 할 수있는 도움에 감사드립니다.

답변

1

이 뜻인가요?

function* sync() { 
    const channel = yield call(todosChannel); 

    yield takeEvery(channel, function*({value}){ 
    yield put(actions.syncTodos(value)) 
    } 

    yield take('CANCEL_WATCH') 
    channel.close(); 
} 

BTW, takeEvery은 효과가 없습니다.

+0

정말 깨끗합니다. 고마워요! 조건부로 채널 생성을 실행하려면'yield 호출 (todosChannel) '앞에 yield 산출 ('SOME_ACITON ')을 넣으면됩니까? –

+0

to alex : 예, 'take'는 차단 효과입니다. – cyrilluce