ReplaySubject.next(value)是否违反了redux存储的不可变状态原则?

时间:2017-02-09 16:19:49

标签: angular redux rxjs5 redux-observable

我有一个Angular2应用程序从需要身份验证的源中获取数据,因此应用程序组件init事件启动auth令牌提取,如下所示:

import { Component, OnInit } from '@angular/core';
import { NgRedux } from '@angular-redux/store';
import { rootReducer } from './app.reducer';
import * as createLogger from 'redux-logger';
import { AppActions } from './app.actions';
import { createEpicMiddleware, combineEpics } from 'redux-observable';
import { UserEpics } from './users/user.epics';
import { ReplaySubject } from 'rxjs/ReplaySubject';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  constructor(
    private ngRedux: NgRedux<any>,
    private actions: AppActions,
    private userEpics: UserEpics,
  ) {
    ngRedux.configureStore(
      rootReducer,
      { user: { auth: { token$: new ReplaySubject(1) } } },
      [
        createLogger(),
        createEpicMiddleware(combineEpics(...userEpics.epics)),
      ]);
  }
  ngOnInit() {
    this.ngRedux.dispatch(this.actions.getAuthToken());
  }
}

Redux正在存储一个Rxjs5 ReplaySubject,这个主题将从reducer中提供令牌值,如下所示:

case AppActions.GET_AUTH_TOKEN_DONE:
  user.auth.token$.next(action.token);
  return Object.assign({}, user, {
    auth: {
      isFetching: false,
      token$: user.auth.token$,
      reason: undefined
    }
 });

这段代码对我有用,但它是否违反了redux商店的不可变状态原则?

1 个答案:

答案 0 :(得分:2)

我会这样说 - 因为你可以通过调用.next(...)从任何地方改变状态 - 如果它是Observable而不是Subject,它可能会有点不同 - 但无论如何,我个人认为将Observables存储为州的一部分是不好的做法。