-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RxJS now supports first-class interop with AsyncIterables
* feat: Observables support for await * feat: convert async iterable to observable * chore: update side-effects expectations * fix: make sure thrown errors in for await loop clean up subscription * chore: update side-effects assertions
- Loading branch information
Showing
17 changed files
with
258 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
import "tslib"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
import "tslib"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import "tslib"; | ||
|
||
var NotificationKind; | ||
|
||
(function(NotificationKind) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import "tslib"; | ||
|
||
var NotificationKind; | ||
|
||
(function(NotificationKind) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import "tslib"; | ||
|
||
var NotificationKind; | ||
|
||
(function(NotificationKind) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import "tslib"; | ||
|
||
var NotificationKind; | ||
|
||
(function(NotificationKind) { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Observable } from './Observable'; | ||
import { Deferred } from './util/deferred'; | ||
|
||
export function asyncIteratorFrom<T>(source: Observable<T>) { | ||
return coroutine(source); | ||
} | ||
|
||
async function* coroutine<T>(source: Observable<T>) { | ||
const deferreds: Deferred<IteratorResult<T>>[] = []; | ||
const values: T[] = []; | ||
let hasError = false; | ||
let error: any = null; | ||
let completed = false; | ||
|
||
const subs = source.subscribe({ | ||
next: value => { | ||
if (deferreds.length > 0) { | ||
deferreds.shift()!.resolve({ value, done: false }); | ||
} else { | ||
values.push(value); | ||
} | ||
}, | ||
error: err => { | ||
hasError = true; | ||
error = err; | ||
while (deferreds.length > 0) { | ||
deferreds.shift()!.reject(err); | ||
} | ||
}, | ||
complete: () => { | ||
completed = true; | ||
while (deferreds.length > 0) { | ||
deferreds.shift()!.resolve({ value: undefined, done: true }); | ||
} | ||
}, | ||
}); | ||
|
||
try { | ||
while (true) { | ||
if (values.length > 0) { | ||
yield values.shift(); | ||
} else if (completed) { | ||
return; | ||
} else if (hasError) { | ||
throw error; | ||
} else { | ||
const d = new Deferred<IteratorResult<T>>(); | ||
deferreds.push(d); | ||
const result = await d.promise; | ||
if (result.done) { | ||
return; | ||
} else { | ||
yield result.value; | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
subs.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { SchedulerLike } from '../types'; | ||
import { Observable } from '../Observable'; | ||
import { Subscription } from '../Subscription'; | ||
|
||
export function scheduleAsyncIterable<T>(input: AsyncIterable<T>, scheduler: SchedulerLike) { | ||
if (!input) { | ||
throw new Error('Iterable cannot be null'); | ||
} | ||
return new Observable<T>(subscriber => { | ||
const sub = new Subscription(); | ||
sub.add( | ||
scheduler.schedule(() => { | ||
const iterator = input[Symbol.asyncIterator](); | ||
sub.add(scheduler.schedule(function () { | ||
iterator.next().then(result => { | ||
if (result.done) { | ||
subscriber.complete(); | ||
} else { | ||
subscriber.next(result.value); | ||
this.schedule(); | ||
} | ||
}); | ||
})); | ||
}) | ||
); | ||
return sub; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export class Deferred<T> { | ||
resolve: (value?: T | PromiseLike<T> | undefined) => void = null!; | ||
reject: (reason?: any) => void = null!; | ||
promise = new Promise<T>((a, b) => { | ||
this.resolve = a; | ||
this.reject = b; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Subscriber } from '../Subscriber'; | ||
|
||
export function subscribeToAsyncIterable<T>(asyncIterable: AsyncIterable<T>) { | ||
return (subscriber: Subscriber<T>) => { | ||
process(asyncIterable, subscriber).catch(err => subscriber.error(err)); | ||
}; | ||
} | ||
|
||
async function process<T>(asyncIterable: AsyncIterable<T>, subscriber: Subscriber<T>) { | ||
for await (const value of asyncIterable) { | ||
subscriber.next(value); | ||
} | ||
subscriber.complete(); | ||
} |