diegomvh / angular-odata

Client side OData typescript library for Angular
https://www.npmjs.com/package/angular-odata
MIT License
46 stars 14 forks source link

Broken .exec method since update to 0.121? #80

Closed sandersdevelopments closed 10 months ago

sandersdevelopments commented 1 year ago
public batchDelete<T extends IEntity>(endpoint: ENDPOINT, entities: T[]): Observable<T[]> {
    return this.client
      .batch(endpoint)
      .exec(() => {
        const linkObservables: Observable<T>[] = [];
        entities.map((entity) => {
          console.log('Create batchDelete request for entity: ', entity);
          linkObservables.push(this.client.entitySet<T>(endpoint).entity(entity.Id).destroy());
        });
        return forkJoin(linkObservables);
      })
      .pipe(
        switchMap(([entries, response]) => { // This is what we had to add after updating your library
          console.log('Batchdelete switchMap complete', entries);
          return entries;
        })
      );
  }

Hello, it looks like the .exec method is broken somehow since we have updated your npm package. Apparantely you have updated the return type of the exec method (to entries AND the corresponding response) and now it looks like the resulting observable never completes or something? When piping the result of this method (should be an observable) it does not work since the pipe is never triggered:

this.api.batchDelete<T>(ENDPOINT.T, options.XYZ).pipe(
          tap((x) => {
            console.log('Batchdelete completed . This should work, but it does not log', x);
          }),

Can you have a look it this issue is on your side? Thanks!

sandersdevelopments commented 1 year ago

Looks like the ....entity(entity.Id).destroy() method never returns anything..

diegomvh commented 1 year ago

Hi @sandersdevelopments

A few months ago I changed the implementation of the batch type request to incorporate the Json type representation and on the other hand remove the conversion to promise that was made on the send of the batch.

https://github.com/diegomvh/angular-odata/blob/cba5168515319686e52afa7a6484602bd0e748de/projects/angular-odata/src/lib/resources/types/batch.ts#L203

The exec forced the execution of the send and returned the execution of the context, in his example the batchDelete function was always executed. In the new implementation, to execute the batch it is necessary to subscribe to its return otherwise it will not be sent to the server.

https://github.com/diegomvh/angular-odata/blob/40bbadaf19a65a07fd4e5e316b524ea3fea73804/projects/angular-odata/src/lib/resources/types/batch.ts#L280

In this way it is achieved that the execution of the batch and subsequent interpretation of the response remain connected, for your example:

public batchDelete<T extends IEntity>(endpoint: ENDPOINT, entities: T[]) {
    return this.client
      .batch(endpoint)
      .exec(() => {
        const linkObservables: Observable<T>[] = [];
        entities.map((entity) => {
          console.log('Create delete request for entity: ', entity);
          linkObservables.push(this.client.entitySet<T>(endpoint).entity(entity.Id).destroy());
        });
        forkJoin(linkObservables).subscribe(entries => {
          console.log('Deletes complete', entries);
        });
      });
  }
this.api.batchDelete<T>(ENDPOINT.T, options.XYZ).subscribe(([resultOfContext, rawResponse]) => {
    console.log('Batch completed', resultOfContext, rawResponse);
});

I hope I have helped with the answer, otherwise we can see more in depth the reason for the change.