订阅套接字io事件时,会触发重复的动作。
const onStartGameActionEpic = (action$, state$) =>
action$.pipe(
ofType(ON_START_GAME),
mergeMap(() =>
fromEvent(socket, 'newGameCreated').pipe(
map(response => onStartGameFulfilled(response)),
),
),
);
答案 0 :(得分:0)
每次ON_START_GAME
出现时,您都在创建一个新的侦听器,但是您绝不会杀死旧的侦听器。这是问题的根源。
将mergeMap
更改为switchMap
。
您可能希望一次玩多个游戏。如果是这样,请为您的namespace
动作分配一个ON_START_GAME
道具。当游戏结束动作触发时,杀死该特定的可观察对象。
const onStartGameActionEpic = (action$, state$) =>
action$.pipe(
ofType(ON_START_GAME),
mergeMap(onStartGameAction =>
fromEvent(socket, 'newGameCreated').pipe(
takeUntil(action$.pipe(
ofType(ON_END_GAME),
filter(onEndGameAction => (
endGameAction.namespace === startGameAction.namespace
)),
)),
map(response => onStartGameFulfilled(response)),
),
),
);