如何在不调用 next 的情况下设置新的 BehaviorSubject 值?

Sco*_*tie 6 rxjs behaviorsubject angular

我需要在不触发对任何订阅的下一次调用的情况下设置我的 BehaviorSubject 的值。

我尝试这样做:

this.mySubject = new BehaviorSubject(newVal);
Run Code Online (Sandbox Code Playgroud)

但这也会删除所有订阅。

this.mySubject.value = newVal;
Run Code Online (Sandbox Code Playgroud)

不起作用,因为.value是只读的。

有什么办法可以做到这一点吗?

编辑:对于那些问我为什么需要这样做的人...

这是一个 Ionic 4 应用程序,因此有一个根列表页面调用另外 2 个子页面(一个详细信息视图和一个编辑页面)。进入视图页面的第一个导航需要选择的模型。列表页面将此模型的初始值设置到 BehaviorSubject 中,然后导航到视图页面。但是,这个初始值的设置会触发列表的刷新,这是我不想做的。我只想初始化该值,然后监听以防编辑页面更改它,然后刷新我的列表。

Jes*_*ter 6

我很想知道为什么您实际上想要这样做,但您可以考虑的一个选项是将一个标志作为您的值的一部分,该标志指示是否应该传播更新,然后让您的所有订阅都从过滤的视图中派生主题。

this.mySubject.next({propagate: false, value: 42});

makeObservable() {
   return this.mySubject.pipe(filter(x => x.propagate));
}
Run Code Online (Sandbox Code Playgroud)


Leo*_*ley 1

BehaviourSubject 并没有那么复杂。您可以创建自己的实现:)

import { Subject } from './Subject';
import { Subscriber } from './Subscriber';
import { Subscription } from './Subscription';
import { SubscriptionLike } from './types';
import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';

/**
 * A variant of Subject that requires an initial value and emits its current
 * value whenever it is subscribed to.
 *
 * @class BehaviorSubject<T>
 */
export class BehaviorSubject<T> extends Subject<T> {

  constructor(private _value: T) {
    super();
  }

  get value(): T {
    return this.getValue();
  }

  /** @deprecated This is an internal implementation detail, do not use. */
  _subscribe(subscriber: Subscriber<T>): Subscription {
    const subscription = super._subscribe(subscriber);
    if (subscription && !(<SubscriptionLike>subscription).closed) {
      subscriber.next(this._value);
    }
    return subscription;
  }

  getValue(): T {
    if (this.hasError) {
      throw this.thrownError;
    } else if (this.closed) {
      throw new ObjectUnsubscribedError();
    } else {
      return this._value;
    }
  }

  next(value: T): void {
    super.next(this._value = value);
  }
}

Run Code Online (Sandbox Code Playgroud)

要么只是复制上面的内容,要么创建一个公开 setValue 的子类

export class SettableBehaviorSubject<T> extends BehaviorSubject<T> {
  setValue(value: T): void {
    this._value = value;
  }
}
Run Code Online (Sandbox Code Playgroud)

BehaviorSubject 的来源在这里:https://github.com/ReactiveX/rxjs/blob/master/src/internal/BehaviorSubject.ts