如何防止在redux-observable中创建重复的Web套接字

时间:2017-12-18 13:40:48

标签: websocket redux rxjs observable

我在我的项目中使用了带有RxJS 5的redux-observable。出于某种原因,如果在调度'CLOSE_STREAM'之前调度'START_STREAM'动作,则此史诗将创建重复的websocket。有没有方法可以检查是否有连接的websocket?

import { WebSocketSubject } from 'rxjs/observable/dom/WebSocketSubject';

const fetchStreamEpic = (action$, store) => action$
  .ofType('START_STREAM')
  .mergeMap(
    () => {
      const socket$ = WebSocketSubject.create(`wss://somepath/ws/v1/`);
      return socket$.multiplex(
        () => console.log('subscribe'),
        () => console.log('unsubscribe'),
        () => true,
      );
    },
  )
  .takeUntil(action$.ofType('CLOSE_STREAM'))
  .map(msg => ({ type: 'RECEIVE_DATA', payload: msg }))
  .repeat();

export default fetchStreamEpic;

0 个答案:

没有答案