如何与ngrx一起创建或删除多个数据?

How to create or delete multiple data together with ngrx?

结合所有 "flux architecture"actionseffectsreducers 等...)和 rxjs 我正在尝试创建和/或删除多个数据

我有以下问题:

  1. 如何在后台和后台创建或删除多个数据 该州的实体?
  2. 我一直在研究,有人说要使用 forkJoin,但它怎么样 与通量架构一起使用?
  3. 如何实时接收这个数据,让其他请求等待 让他们到达?

这是我正在做的一个例子:

后端服务:

create(peoples: Array<IPeople>): Observable<Array<IPeople>> {
        const https = [];
        peoples.forEach((people) => https.push(this.httpClient.post<IPeople>(this.endpoint, people)));
        return forkJoin([]);
    }

效果

createAll$ = createEffect(() =>
    this.action$.pipe(
        ofType(fromPeopleActions.CREATE),
        mergeMap((action) => {
            return this.backend.create(action.people).pipe(
                map((people) => fromPeopleActions.CREATE_SUCCESS({ people })),
                catchError((error) => of(fromPeopleActions.CREATE_FAIL({ error: this.requestHandler.getError(error) })))
            );
        })
    )
);

减速器:

const peopleReducer = createReducer(
    INIT_STATE,
    on(fromPeopleAction.GET_ALL_SUCCESS, fromPeopleAction.CREATE_SUCCESS, fromPeopleAction.DELETE_SUCCESS, (state, { peoples }) => adapter.addMany(peoples, { ...state, loading: false })),
    on(fromPeopleAction.GET_ALL_FAIL, fromPeopleAction.CREATE_FAIL, fromPeopleAction.DELETE_FAIL, (state, { error }) => ({ ...state, error, loading: false }))
);

呼叫

ngOnInit(): void {
    this.peopleDispatchService.getAll();
    this.peopleSelectorsService.loading.subscribe((isLoading) => {
        if (!isLoading) {
            this.peopleSelectorsService.total.subscribe((total) => {
                console.log(total);
                if (total === 0) {
                    this.peopleDispatchService.create([
                        { id: '0', isMain: true, name: 'THIAGO DE BONIS CARVALHO SAAD SAUD', avatar: 'assets/users/thiagobonis.jpg', messages: null },
                        { id: '1', isMain: false, name: 'BILL GATES', avatar: 'assets/users/billgates.jpg', messages: null },
                        { id: '2', isMain: false, name: 'STEVE JOBS', avatar: 'assets/users/stevejobs.jpg', messages: null },
                        { id: '3', isMain: false, name: 'LINUS TORVALDS', avatar: 'assets/users/linustorvalds.jpg', messages: null },
                        { id: '4', isMain: false, name: 'EDSGER DIJKSTRA', avatar: 'assets/users/dijkstra.jpg', messages: null },
                    ])
                } else {
                    this.peopleSelectorsService.allIds.subscribe((ids) => this.peopleDispatchService.delete(ids.toString()));
                }
            });
        }
    });
}

你几乎做对了

create(people: Array<IPeople>): Observable<Array<IPeople>> {
  return forkJoin(
    ...people.map(person => this.httpClient.post<IPeople>(this.endpoint, person)),
  );
}

另外 fromPeopleAction.DELETE_SUCCESSaddMany 放在一起听起来很奇怪。我会为它添加一个独立的on

现在创建将引起创建请求。

如何在后端和状态实体中创建或删除多个数据?

  • 你的做法很好。

我一直在研究,有人说可以使用 forkJoin,但它如何与 flux 架构一起使用?

  • 检查我上面的例子

如何实时接收这个数据,让其他请求等待他们到达?

  • 更好的做法是让一个请求获取所有数据,然后将响应转换为许多操作,例如 ADD_MANY 等。或者如果您的后端不提供,则使用 forkJoin用于批量数据交换的单一端点。