取消除 redux saga 中的任务之外的所有任务

Cancel all tasks except from one in redux saga

我正在阅读 redux saga 文档中的一些高级概念,但我在这里有一个问题。在文档中使用以下代码来调度我们代码中存在的所有操作:

import { select, take } from 'redux-saga/effects'

function* watchAndLog() {
  while (true) {
    const action = yield take('*')
    const state = yield select()

    console.log('action', action)
    console.log('state after', state)
  }
}

使用通配符 * 我们将在应用程序中执行所有操作。有没有一种方法可以采取一两个特定行动之外的所有行动?我想分派 "LOGIN" 和 "LOGOUT" 操作所期望的所有操作?有什么想法吗?

来自 take 上的文档:

If it is a function, the action is matched if pattern(action) is true (e.g. take(action => action.entities) will match all actions having a (truthy) entitiesfield.)

所以你可以这样做:

function* watchAndLog() {
  while (true) {
    const action = take(action => ['LOGIN', 'LOGOUT'].indexOf(action.type) < 0);
    ...
  }
}