Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
830 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | import { Subscriber } from '../Subscriber';
|
---|
| 2 | import { iterator as Symbol_iterator } from '../symbol/iterator';
|
---|
| 3 |
|
---|
| 4 | export const subscribeToIterable = <T>(iterable: Iterable<T>) => (subscriber: Subscriber<T>) => {
|
---|
| 5 | const iterator = (iterable as any)[Symbol_iterator]();
|
---|
| 6 |
|
---|
| 7 | do {
|
---|
| 8 | let item: IteratorResult<T>;
|
---|
| 9 | try {
|
---|
| 10 | item = iterator.next();
|
---|
| 11 | } catch (err) {
|
---|
| 12 | subscriber.error(err);
|
---|
| 13 | return subscriber;
|
---|
| 14 | }
|
---|
| 15 | if (item.done) {
|
---|
| 16 | subscriber.complete();
|
---|
| 17 | break;
|
---|
| 18 | }
|
---|
| 19 | subscriber.next(item.value);
|
---|
| 20 | if (subscriber.closed) {
|
---|
| 21 | break;
|
---|
| 22 | }
|
---|
| 23 | } while (true);
|
---|
| 24 |
|
---|
| 25 | // Finalize the iterator if it happens to be a Generator
|
---|
| 26 | if (typeof iterator.return === 'function') {
|
---|
| 27 | subscriber.add(() => {
|
---|
| 28 | if (iterator.return) {
|
---|
| 29 | iterator.return();
|
---|
| 30 | }
|
---|
| 31 | });
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return subscriber;
|
---|
| 35 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.