Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 当某个动作出现时,如何关闭eventChannel_React Native_Redux Saga - Fatal编程技术网

React native 当某个动作出现时,如何关闭eventChannel

React native 当某个动作出现时,如何关闭eventChannel,react-native,redux-saga,React Native,Redux Saga,我有一个用于收听WebSocket的eventChannel,这很好用 我还想听到一个动作用户退出并关闭频道 在套接字错误或套接字关闭的情况下,我通过发出END从通道内部关闭通道,但是如何基于外部操作来实现这一点 这是通道环路: export function* websocketWatcher() { const tokenResponse = yield take(RECEIVE_USER_TOKEN); const accessToken = tokenResponse.paylo

我有一个用于收听WebSocket的eventChannel,这很好用

我还想听到一个动作用户退出并关闭频道

在套接字错误或套接字关闭的情况下,我通过发出END从通道内部关闭通道,但是如何基于外部操作来实现这一点

这是通道环路:

export function* websocketWatcher() {
  const tokenResponse = yield take(RECEIVE_USER_TOKEN);
  const accessToken = tokenResponse.payload.data.access_token;

  const channel = yield call(createWebsocketChannel, accessToken);
  try {
    while (true) {
      const action = yield take(channel);
      yield put(action);
    }
  } finally {
    console.log('Websocket channel terminated');
  }
}

我自己回答

我刚在文档中找到channel.close。不知道我怎么会错过。我通过通道传入套接字数据和注销操作之间的竞争解决了这个问题

export function* websocketWatcher() {
  const tokenResponse = yield take(RECEIVE_USER_TOKEN);
  const accessToken = tokenResponse.payload.data.access_token;

  const channel = yield call(createWebsocketChannel, accessToken);
  try {
    while (true) {
      const { logoutAction, socketAction } = yield race({
        logoutAction: take(USER_LOGGED_OUT),
        socketAction: take(channel)
      });

      if (logoutAction) {
        channel.close();
      } else {
        yield put(socketAction);
      }
    }
  } finally {
    console.log('Websocket channel terminated');
  }
}

我自己回答

我刚在文档中找到channel.close。不知道我怎么会错过。我通过通道传入套接字数据和注销操作之间的竞争解决了这个问题

export function* websocketWatcher() {
  const tokenResponse = yield take(RECEIVE_USER_TOKEN);
  const accessToken = tokenResponse.payload.data.access_token;

  const channel = yield call(createWebsocketChannel, accessToken);
  try {
    while (true) {
      const { logoutAction, socketAction } = yield race({
        logoutAction: take(USER_LOGGED_OUT),
        socketAction: take(channel)
      });

      if (logoutAction) {
        channel.close();
      } else {
        yield put(socketAction);
      }
    }
  } finally {
    console.log('Websocket channel terminated');
  }
}

你最后做了什么?当它被终止时?最终做什么?何时终止?