如何对秋田商店状态进行部分更新?

how to make a partial update of an akita store state?

非常基本的问题,google没有帮助。

我的主要课程有一家秋田店。它有这个状态

    export interface SessionState {
      language: Language;
      activeRepo: number;
      auth_token: string;
      user : AppUser
      userRepos : UserRepo[]
    }

如何在服务中只更新其中一个属性?例如,"userRepos" 仅

我有这个服务电话

 getUserRepos() {
      return httpclient.post("User","GetRepos", {}).pipe(tap<UserRepo[]>(repos  => {
           // THIS IS THE LINE THAT I CANNOT GET TO WORK--->
             this.sessionStore.update({...userRepos : repos});
      }));
 }

我试过很多构造,就是做不出来。 是否可以只更新状态的一部分?

getUserRepos() {
 return httpclient.post("User","GetRepos", {}).pipe(tap<UserRepo[]>(repos  => {
      this.sessionStore.update({ userRepos: repos});
 }));
 }

只需删除点差。

对于 EntityStore,update() 函数存在不同的变体。

myStore.update(currentValue => {
      return {
        propery1: {
          ...currentValue.propery1,
          propertyWithUpdate: {
            ...currentValue.propery1.propertyWithUpdate,
            field1: "new_value1",
            field5: "new_value5"
          }
        }
      };

您无需发出 POST 更新秋田状态的请求。您可以使用 javascript destructuring 来更新商店的那个属性。像这样:

    this._sessionStore.update(state => ({
      ...state,
      userRepos: repos,
    }));

这会获取状态对象并分配相同的先前状态值(解构),并使用传递的值更改 userRepos 属性。

Akita 有一个方法可以做到这一点,它通过使用 upsert 部分更新商店。 Upsert 将使用给定参数更新商店,或者如果没有要更新的内容,它将创建实体,因为给定的 ID 不存在,以防您尝试更新不存在的内容。代码示例:

getUserInformation(id: string): Observable<UserInformation> {
    this.userInformationStore.setLoading(true);

    return this.userInformationProxy.getUserInformation(id).pipe(
      tap((userInformation: UserInformation) => {
        this.userInformationStore.upsert(id, userInformation);
        this.userInformationStore.setError(null);
      }),
      catchError(error => {
        this.userInformationStore.setError(error);
        return throwError(error);
      }),
      finalize(() => {
        this.userInformationStore.setLoading(false);
      })
    );
  }