rxjs icon indicating copy to clipboard operation
rxjs copied to clipboard

zipAll not working as expected ? or just wrong TypeScript typings ? (rx newbie need your help)

Open virtualvoid opened this issue 10 years ago • 9 comments

RxJS version: 5.0.0-beta.7

Code to reproduce: http://jsbin.com/milute/edit?js,console

Expected behavior: at the subscribe callback i expected to see the length of array that was made by zipAll

Actual behavior: something with ZipBufferIterator which i don't understand

Additional information: what's wrong with my or yours code ?

Thanks

virtualvoid avatar May 02 '16 07:05 virtualvoid

for better explanation what I need to do :

    Observable
      .from(this.resourceStaffs)
      .map((resourceStaff: ResourceStaff, index: number) => this._resourceService.saveResourceStaff(resourceStaff, index))
      .zipAll()
      .subscribe((objectIndices: Array<ObjectIndex<ResourceStaff>>) => {
        debugger;
     ...

or even

    Observable
      .from(this.resourceStaffs)
      .flatMap((resourceStaff: ResourceStaff, index: number) => this._resourceService.saveResourceStaff(resourceStaff, index))
      .combineAll()
      .subscribe((objectIndices: Array<ObjectIndex<ResourceStaff>>) => {
        debugger;

I have array of objects and I need to save them (using ng2 http) one-by-one, wait for all results and then zip it into one result (array of objectIndices).

Can anyone help me out please ? Thanks in advance.

virtualvoid avatar May 02 '16 11:05 virtualvoid

with the second example from the previous comment i get this exception :

EXCEPTION: TypeError: unknown type returned
   EXCEPTION: TypeError: unknown type returned
   angular2.dev.js (25644,9)
   STACKTRACE:
   angular2.dev.js (25644,9)
   TypeError: unknown type returned
   at SafeSubscriber.prototype.error (https://localhost:44300/app/libs/rx.js:10829:11)
   at Subscriber.prototype._error (https://localhost:44300/app/libs/rx.js:10769:7)
   at Subscriber.prototype.error (https://localhost:44300/app/libs/rx.js:10749:9)
   at OuterSubscriber.prototype.notifyError (https://localhost:44300/app/libs/rx.js:805:7)
   at InnerSubscriber.prototype._error (https://localhost:44300/app/libs/rx.js:891:7)
   at Subscriber.prototype.error (https://localhost:44300/app/libs/rx.js:10749:9)
   at subscribeToResult (https://localhost:44300/app/libs/rx.js:8257:7)
   at ZipBufferIterator.prototype.subscribe (https://localhost:44300/app/libs/rx.js:2505:7)
   at ZipSubscriber.prototype._complete (https://localhost:44300/app/libs/rx.js:2358:11)
   at Subscriber.prototype.complete (https://localhost:44300/app/libs/rx.js:10755:9)
  -------------   Elapsed: 166 ms; At: Mon May 02 2016 13:10:11 GMT+0200 (Central Europe Daylight Time)   -------------  
   at Zone[longStackTraceZoneSpec].onScheduleTask (https://localhost:44300/app/libs/angular2-polyfills.js:1452:14)
   at ZoneDelegate.prototype.scheduleTask (https://localhost:44300/app/libs/angular2-polyfills.js:359:22)
   at Zone.prototype.scheduleEventTask (https://localhost:44300/app/libs/angular2-polyfills.js:302:14)
   at zoneAwareAddEventListener (https://localhost:44300/app/libs/angular2-polyfills.js:915:6)
   at addEventListener (Function code:1:36)
   at Anonymous function (https://localhost:44300/app/libs/http.dev.js:693:9)
   at Observable.prototype.subscribe (https://localhost:44300/app/libs/rx.js:11218:7)
   at Observable.prototype._subscribe (https://localhost:44300/app/libs/rx.js:11255:7)
   angular2.dev.js (25644,9)

SCRIPT5022: Exception thrown and not caught
rx.js (10858,9)

virtualvoid avatar May 02 '16 11:05 virtualvoid

In the JSBin, you're trying to zip together POJO objects. Basically you're calling it on an observable of objects. zipAll should only be called on a an observable of observables, promises, iterables, etc.

I have array of objects and I need to save them (using ng2 http) one-by-one, wait for all results and then zip it into one result (array of objectIndices).

Presuming that this.resourceStaffs is an array, and this._resourceService.saveResourceStaff() returns a Promise<ObjectIndex<ResourceStaff>> or Observable<ObjectIndex<ResourceStaff>>, this code you provided should work:

Observable
      .from(this.resourceStaffs)
      .map((resourceStaff: ResourceStaff, index: number) => this._resourceService.saveResourceStaff(resourceStaff, index))
      .zipAll()
      .subscribe((objectIndices: Array<ObjectIndex<ResourceStaff>>) => {
        debugger;
       });

benlesh avatar May 02 '16 17:05 benlesh

with the second example from the previous comment i get this exception :

It sounds like saveResourceStaff is returning something that isn't observable, promise or iterable.

Hopefully you found my answers helpful, @virtualvoid. I'm going to close this issue for now, as I don't see a bug in zipAll or combineAll, but feel free to reopen if you can provide a complete example we can test against. (The JSBin you provided was misusing the operator).

benlesh avatar May 02 '16 17:05 benlesh

Thanks for information, however I still think there's something weird, or maybe I'm missing something here.

I've created this repository ng2rxjs-sample1 because i can't do it well with jsbin.

Let's just say I wanted to query github repositories for more than 1 user (two in this sample) and I want to get the results in one subscribe callback for all of the network calls (the method from the service class returns observable).

Now take a look at this line : https://github.com/virtualvoid/ng2rxjs-sample1/blob/master/app/app.ts#L37

I expected that the value type after calling zipAll will be two dimensional array, like .subscribe((values: Repository[][]) => { and not .subscribe((values: Observable<Repository[]>[]) => { like it's now.

Can you please take a look at that repo and point me to the right direction ? Thanks !

virtualvoid avatar May 03 '16 07:05 virtualvoid

Oh this is funny actually ! I think there's something wrong with the TypeScript typings for RxJS because if you look at the transpiled code of my app, and points debugger on that incriminating line the results are actually two dimensional array :

bad_typings_for_rxjs

virtualvoid avatar May 03 '16 07:05 virtualvoid

Unfortunately the typings can only take us so far. In the case of combineAll and zipAll there is a raw limitation that we haven't found a solid work around for. Basically from a typings perspective, we only have T, which in your case is Observable<Something>. We have no way of extracting Something out to produce Observable<Something[][]>, the two options we have is to assume T, and do Observable<Observable<Something>[]> or manually specify it.

It may be that the definition (): Observable<T[]>; can never really work from a typescript perspective, so perhaps we should remove it.

In your specific case, once #1640 lands, you will be able to do Observable.zip(userNames.map(userName => this._repositoryService.getRepositories(userName))). Which will have similar behaviors.

david-driscoll avatar May 03 '16 11:05 david-driscoll

@david-driscoll sounds like a plan, thanks for your reply.

virtualvoid avatar May 03 '16 11:05 virtualvoid

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jun 07 '18 05:06 lock[bot]