简体   繁体   English

Angular/Typescript/RxJs:在观察变化的同时更新 BehaviorSubject(对象分配)的值

[英]Angular/Typescript/RxJs: updating value of BehaviorSubject (Object Assign) while observing changes

I have a user object as BehaviourSubject which I observe.我有一个用户 object 作为我观察到的 BehaviourSubject。 I would like to know if a particular value of my user has changed.我想知道我的用户的特定值是否发生了变化。

My code does not work, and I assume it's because of the way I assign a new object value to my user.我的代码不起作用,我认为这是因为我为我的用户分配了一个新的 object 值的方式。 I apparently overwrite both the old and new value of my BehaviorSubject, hence I would always compare the same object?我显然覆盖了我的 BehaviorSubject 的旧值和新值,因此我总是会比较相同的 object?

export interface User {
   id: string;
   username: string;
   isBanned: boolean;
   subscription?: UserSubscription;
}

export class AuthService {
  public readonly user$: Observable<User| undefined> = this.user.asObservable();
  user: BehaviorSubject<User| undefined> = new BehaviorSubject<User| undefined>(undefined);

  updateUserSubscription(newUserSubscription: UserSubscription): void {
    // Here I simply want to update the subscription-Object of my user
    this.user.next(Object.assign(this.user.value, newUserSubscription));
  }
}

export class MemberService {
  user?: User;
  private subscription?: Subscription;

  constructor(public auth: AuthService) {
     // Here I simply want to detect subscription changes
     this.subscription = this.auth.user$.pipe(
        pairwise(),
        filter(users => this.detectSubscriptionChange(users[0], users[1]))
     ).subscribe(([user1, user2] => { // some code ... })
   }

  detectSubscriptionChange(user1: User | undefined, user2: User | undefined): boolean {
      //subscription status is always the same, as the objects are always the same...
      return user1?.subscription?.status !== user2?.subscription?.status;
  }

}

Could you tell me how to properly assign new values to my current user object, so I can compare the values in the observable?你能告诉我如何正确地为我当前的用户 object 分配新值,以便我可以比较 observable 中的值吗?

Thanks!谢谢!

If using Object.assign() to clone, you'd want to put make the "target" in the first parameter an empty object {} , then "assign" the user object and subscription objects to that new empty object, which represents the new user clone. If using Object.assign() to clone, you'd want to put make the "target" in the first parameter an empty object {} , then "assign" the user object and subscription objects to that new empty object, which represents the新用户克隆。

this.user.next(Object.assign({}, this.user.value, newUserSubscription);

Currently you are merely "assigning" the subscription object to the current user.目前,您只是将订阅 object“分配”给当前用户。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

In latest JS / Typescript though, the spread operator is much nicer way to clone, and pretty much in line with how you were expecting Object.assign() to work for cloning:不过,在最新的 JS / Typescript 中,传播运算符是更好的克隆方式,并且与您期望Object.assign()用于克隆的方式非常一致:

this.user.next({...this.user.value, newUserSubscription});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM