Редукс-наблюдаемое странное поведение при отправке другого действия - PullRequest
0 голосов
/ 29 октября 2019

Я пытаюсь написать кеш для моего серверного приложения. Я использую Redux-Observable на стороне сервера. Когда я пытался изменить действие над энхансером / редуктором, промежуточным программным обеспечением или эпопеей, вызывается эпопея, назначенная предыдущему действию. (Существует еще один эпос, зарегистрированный для YOUTUBE_DOWNLOAD_QUERY)

Другими словами: actions.youtubeDownload.YOUTUBE_DOWNLOAD_QUERY => actions.sendVideo.SEND_VIDEO_QUERY old state => новое состояние

код промежуточного программного обеспечения*

const cache: Middleware = (
  middlewareAPI: MiddlewareAPI
): ((next: Dispatch) => (action: IAction) => IAction) => (
  next: Dispatch
): ((action: IAction) => IAction) => (action: IAction): IAction => {
  appDebug("cache", action);
  const state: IState = middlewareAPI.getState();
  switch (action.type) {
    case actions.youtubeDownload.YOUTUBE_DOWNLOAD_QUERY:
      if (
        state.message.query !== undefined &&
        state.message.query.message !== undefined &&
        action.youtubeDownload.query !== undefined &&
        action.youtubeDownload.query === "QUERY"
      ) {
        const newAction: any = actions.sendVideo.query({});
        return next(newAction);
      }
      break;
    default:
  }

  return next(action);
};

усилитель / редуктор

const cacheReducer: (
  next: StoreCreator
) => (
  reducer: Reducer<IState, IAction>,
  preloadedState?: DeepPartial<IState>
) => StoreEnhancer = (
  next: StoreCreator
): ((
  reducer: Reducer<IState, IAction>,
  preloadedState?: DeepPartial<IState>
) => StoreEnhancer) => (
  reducer: Reducer<IState, IAction>,
  preloadedState?: DeepPartial<IState>
): StoreEnhancer => {
  const cachedReducer: (
    state: IState | undefined,
    action: IAction
  ) => IState = (state: IState | undefined, action: IAction): IState => {
    if (state !== undefined) {
      switch (action.type) {
        case actions.youtubeDownload.YOUTUBE_DOWNLOAD_QUERY:
          if (
            state.message.query !== undefined &&
            state.message.query.message !== undefined &&
            action.youtubeDownload.query !== undefined &&
            action.youtubeDownload.query === "QUERY"
          ) {
            const newAction: any = actions.sendVideo.query({});

            return reducer(state, newAction);
          }
          break;
        default:
      }
    }

    const newState: IState = reducer(state, action);

    return newState;
  };

  return next(cachedReducer, preloadedState);
};

эпический

const cache: (
  action$: Observable<IAction>,
  state$: StateObservable<IState> | undefined,
  dependencies: IDependencies
) => Observable<IActionSendVideo | IActionYoutubeDownload> = (
  action$: Observable<IAction>,
  state$: StateObservable<IState> | undefined,
  _dependencies: IDependencies
): Observable<IActionSendVideo | IActionYoutubeDownload> => {
  const actionObservable: (
    action: IAction
  ) => Observable<IActionSendVideo | IActionYoutubeDownload> = (
    _action: IAction
  ): Observable<IActionSendVideo | IActionYoutubeDownload> => {
    if (state$ === undefined) {
      return of(
        actions.youtubeDownload.error({
          error: new Error(texts.state$Undefined)
        })
      );
    }
    if (state$.value.message.query === undefined) {
      return of(
        actions.youtubeDownload.error({
          error: new Error(texts.state$ValueMessageQueryUndefined)
      );
    }

    return of(
      actions.sendVideo.query({})
    );
  };

  return action$.pipe(
    ofType(actions.youtubeDownload.YOUTUBE_DOWNLOAD_QUERY),
    switchMap(actionObservable)
  );
};
...