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:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { SchedulerLike } from '../types';
|
---|
| 3 | import { Subscription } from '../Subscription';
|
---|
| 4 | import { iterator as Symbol_iterator } from '../symbol/iterator';
|
---|
| 5 |
|
---|
| 6 | export function scheduleIterable<T>(input: Iterable<T>, scheduler: SchedulerLike) {
|
---|
| 7 | if (!input) {
|
---|
| 8 | throw new Error('Iterable cannot be null');
|
---|
| 9 | }
|
---|
| 10 | return new Observable<T>(subscriber => {
|
---|
| 11 | const sub = new Subscription();
|
---|
| 12 | let iterator: Iterator<T>;
|
---|
| 13 | sub.add(() => {
|
---|
| 14 | // Finalize generators
|
---|
| 15 | if (iterator && typeof iterator.return === 'function') {
|
---|
| 16 | iterator.return();
|
---|
| 17 | }
|
---|
| 18 | });
|
---|
| 19 | sub.add(scheduler.schedule(() => {
|
---|
| 20 | iterator = input[Symbol_iterator]();
|
---|
| 21 | sub.add(scheduler.schedule(function () {
|
---|
| 22 | if (subscriber.closed) {
|
---|
| 23 | return;
|
---|
| 24 | }
|
---|
| 25 | let value: T;
|
---|
| 26 | let done: boolean;
|
---|
| 27 | try {
|
---|
| 28 | const result = iterator.next();
|
---|
| 29 | value = result.value;
|
---|
| 30 | done = result.done;
|
---|
| 31 | } catch (err) {
|
---|
| 32 | subscriber.error(err);
|
---|
| 33 | return;
|
---|
| 34 | }
|
---|
| 35 | if (done) {
|
---|
| 36 | subscriber.complete();
|
---|
| 37 | } else {
|
---|
| 38 | subscriber.next(value);
|
---|
| 39 | this.schedule();
|
---|
| 40 | }
|
---|
| 41 | }));
|
---|
| 42 | }));
|
---|
| 43 | return sub;
|
---|
| 44 | });
|
---|
| 45 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.