gpt4 book ai didi

react-native - 当某个 Action 发生时如何关闭 eventChannel

转载 作者:行者123 更新时间:2023-12-02 20:44:33 27 4
gpt4 key购买 nike

我有一个 eventChannel 用于监听 webSockets,这工作正常。

我还想监听 USER_LOGGED_OUT 操作并关闭 channel 。

如果出现套接字错误或套接字关闭,我将通过发出 END 从 channel 内关闭 channel ,但如何根据外部操作执行此操作?

这是 channel 循环:

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()。不知道我怎么错过了。我通过 channel (传入套接字数据)和注销操作之间的竞争解决了这个问题。

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');
}
}

关于react-native - 当某个 Action 发生时如何关闭 eventChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44926758/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com