|
1 |
| -import {Scheduler} from '../Scheduler'; |
2 |
| -import {Observable} from '../Observable'; |
3 |
| - |
4 |
| -import {root} from '../util/root'; |
5 |
| -import {$$iterator} from '../util/Symbol_iterator'; |
6 |
| -import {tryCatch} from '../util/tryCatch'; |
7 |
| -import {errorObject} from '../util/errorObject'; |
8 |
| -import {_IndexSelector} from '../types'; |
9 |
| - |
10 |
| -export class IteratorObservable<T, R> extends Observable<R> { |
11 |
| - |
12 |
| - static create<T>(iterator: Iterator<T>, |
13 |
| - project?: _IndexSelector<T, T>, |
14 |
| - thisArg?: any, |
15 |
| - scheduler?: Scheduler): Observable<T>; |
16 |
| - static create<T, R>(iterator: Iterator<T>, |
17 |
| - project?: _IndexSelector<T, R>, |
18 |
| - thisArg?: any, |
19 |
| - scheduler?: Scheduler): Observable<R> { |
20 |
| - if (iterator == null) { |
21 |
| - throw new Error('iterator cannot be null.'); |
22 |
| - } |
23 |
| - if (project && typeof project !== 'function') { |
24 |
| - throw new Error('When provided, `project` must be a function.'); |
25 |
| - } |
26 |
| - return new IteratorObservable<T, R>(iterator, project, thisArg, scheduler); |
27 |
| - } |
28 |
| - |
29 |
| - static dispatch(state) { |
30 |
| - |
31 |
| - const { index, hasError, thisArg, project, iterator, subscriber } = state; |
32 |
| - |
33 |
| - if (hasError) { |
34 |
| - subscriber.error(state.error); |
35 |
| - return; |
36 |
| - } |
37 |
| - |
38 |
| - let result = iterator.next(); |
39 |
| - |
40 |
| - if (result.done) { |
41 |
| - subscriber.complete(); |
42 |
| - return; |
43 |
| - } |
44 |
| - |
45 |
| - if (project) { |
46 |
| - result = tryCatch(project).call(thisArg, result.value, index); |
47 |
| - if (result === errorObject) { |
48 |
| - state.error = errorObject.e; |
49 |
| - state.hasError = true; |
50 |
| - } else { |
51 |
| - subscriber.next(result); |
52 |
| - state.index = index + 1; |
53 |
| - } |
54 |
| - } else { |
55 |
| - subscriber.next(result.value); |
56 |
| - state.index = index + 1; |
57 |
| - } |
58 |
| - |
59 |
| - if (subscriber.isUnsubscribed) { |
60 |
| - return; |
61 |
| - } |
62 |
| - |
63 |
| - (<any> this).schedule(state); |
64 |
| - } |
65 |
| - |
66 |
| - constructor(private iterator: Iterator<T>, |
67 |
| - private project?: (x?: any, i?: number) => R, |
68 |
| - private thisArg?: any, |
69 |
| - private scheduler?: Scheduler) { |
70 |
| - super(); |
71 |
| - } |
72 |
| - |
73 |
| - _subscribe(subscriber) { |
74 |
| - |
75 |
| - let index = 0; |
76 |
| - const project = this.project; |
77 |
| - const thisArg = this.thisArg; |
78 |
| - const iterator = getIterator(Object(this.iterator)); |
79 |
| - const scheduler = this.scheduler; |
80 |
| - |
81 |
| - if (scheduler) { |
82 |
| - subscriber.add(scheduler.schedule(IteratorObservable.dispatch, 0, { |
83 |
| - index, thisArg, project, iterator, subscriber |
84 |
| - })); |
85 |
| - } else { |
86 |
| - do { |
87 |
| - let result = iterator.next(); |
88 |
| - if (result.done) { |
89 |
| - subscriber.complete(); |
90 |
| - break; |
91 |
| - } else if (project) { |
92 |
| - result = tryCatch(project).call(thisArg, result.value, index++); |
93 |
| - if (result === errorObject) { |
94 |
| - subscriber.error(errorObject.e); |
95 |
| - break; |
96 |
| - } |
97 |
| - subscriber.next(result); |
98 |
| - } else { |
99 |
| - subscriber.next(result.value); |
100 |
| - } |
101 |
| - if (subscriber.isUnsubscribed) { |
102 |
| - break; |
103 |
| - } |
104 |
| - } while (true); |
105 |
| - } |
106 |
| - } |
107 |
| -} |
108 |
| - |
109 |
| -const maxSafeInteger = Math.pow(2, 53) - 1; |
110 |
| - |
111 |
| -class StringIterator { |
112 |
| - constructor(private str: string, |
113 |
| - private idx: number = 0, |
114 |
| - private len: number = str.length) { |
115 |
| - } |
116 |
| - [$$iterator]() { return (this); } |
117 |
| - next() { |
118 |
| - return this.idx < this.len ? { |
119 |
| - done: false, |
120 |
| - value: this.str.charAt(this.idx++) |
121 |
| - } : { |
122 |
| - done: true, |
123 |
| - value: undefined |
124 |
| - }; |
125 |
| - } |
126 |
| -} |
127 |
| - |
128 |
| -class ArrayIterator { |
129 |
| - constructor(private arr: Array<any>, |
130 |
| - private idx: number = 0, |
131 |
| - private len: number = toLength(arr)) { |
132 |
| - } |
133 |
| - [$$iterator]() { return this; } |
134 |
| - next() { |
135 |
| - return this.idx < this.len ? { |
136 |
| - done: false, |
137 |
| - value: this.arr[this.idx++] |
138 |
| - } : { |
139 |
| - done: true, |
140 |
| - value: undefined |
141 |
| - }; |
142 |
| - } |
143 |
| -} |
144 |
| - |
145 |
| -function getIterator(o) { |
146 |
| - const i = o[$$iterator]; |
147 |
| - if (!i && typeof o === 'string') { |
148 |
| - return new StringIterator(o); |
149 |
| - } |
150 |
| - if (!i && o.length !== undefined) { |
151 |
| - return new ArrayIterator(o); |
152 |
| - } |
153 |
| - if (!i) { |
154 |
| - throw new TypeError('Object is not iterable'); |
155 |
| - } |
156 |
| - return o[$$iterator](); |
157 |
| -} |
158 |
| - |
159 |
| -function toLength(o) { |
160 |
| - let len = +o.length; |
161 |
| - if (isNaN(len)) { |
162 |
| - return 0; |
163 |
| - } |
164 |
| - if (len === 0 || !numberIsFinite(len)) { |
165 |
| - return len; |
166 |
| - } |
167 |
| - len = sign(len) * Math.floor(Math.abs(len)); |
168 |
| - if (len <= 0) { |
169 |
| - return 0; |
170 |
| - } |
171 |
| - if (len > maxSafeInteger) { |
172 |
| - return maxSafeInteger; |
173 |
| - } |
174 |
| - return len; |
175 |
| -} |
176 |
| - |
177 |
| -function numberIsFinite(value) { |
178 |
| - return typeof value === 'number' && root.isFinite(value); |
179 |
| -} |
180 |
| - |
181 |
| -function sign(value) { |
182 |
| - let valueAsNumber = +value; |
183 |
| - if (valueAsNumber === 0) { |
184 |
| - return valueAsNumber; |
185 |
| - } |
186 |
| - if (isNaN(valueAsNumber)) { |
187 |
| - return valueAsNumber; |
188 |
| - } |
189 |
| - return valueAsNumber < 0 ? -1 : 1; |
190 |
| -} |
| 1 | +import {Scheduler} from '../Scheduler'; |
| 2 | +import {Observable} from '../Observable'; |
| 3 | + |
| 4 | +import {root} from '../util/root'; |
| 5 | +import {$$iterator} from '../util/Symbol_iterator'; |
| 6 | +import {tryCatch} from '../util/tryCatch'; |
| 7 | +import {errorObject} from '../util/errorObject'; |
| 8 | +import {_IndexSelector} from '../types'; |
| 9 | + |
| 10 | +export class IteratorObservable<T, R> extends Observable<R> { |
| 11 | + |
| 12 | + static create<T>(iterator: Iterator<T>, |
| 13 | + project?: _IndexSelector<T, T>, |
| 14 | + thisArg?: any, |
| 15 | + scheduler?: Scheduler): Observable<T>; |
| 16 | + static create<T, R>(iterator: Iterator<T>, |
| 17 | + project?: _IndexSelector<T, R>, |
| 18 | + thisArg?: any, |
| 19 | + scheduler?: Scheduler): Observable<R> { |
| 20 | + if (iterator == null) { |
| 21 | + throw new Error('iterator cannot be null.'); |
| 22 | + } |
| 23 | + if (project && typeof project !== 'function') { |
| 24 | + throw new Error('When provided, `project` must be a function.'); |
| 25 | + } |
| 26 | + return new IteratorObservable<T, R>(iterator, project, thisArg, scheduler); |
| 27 | + } |
| 28 | + |
| 29 | + static dispatch(state) { |
| 30 | + |
| 31 | + const { index, hasError, thisArg, project, iterator, subscriber } = state; |
| 32 | + |
| 33 | + if (hasError) { |
| 34 | + subscriber.error(state.error); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + let result = iterator.next(); |
| 39 | + |
| 40 | + if (result.done) { |
| 41 | + subscriber.complete(); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + if (project) { |
| 46 | + result = tryCatch(project).call(thisArg, result.value, index); |
| 47 | + if (result === errorObject) { |
| 48 | + state.error = errorObject.e; |
| 49 | + state.hasError = true; |
| 50 | + } else { |
| 51 | + subscriber.next(result); |
| 52 | + state.index = index + 1; |
| 53 | + } |
| 54 | + } else { |
| 55 | + subscriber.next(result.value); |
| 56 | + state.index = index + 1; |
| 57 | + } |
| 58 | + |
| 59 | + if (subscriber.isUnsubscribed) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + (<any> this).schedule(state); |
| 64 | + } |
| 65 | + |
| 66 | + constructor(private iterator: Iterator<T>, |
| 67 | + private project?: (x?: any, i?: number) => R, |
| 68 | + private thisArg?: any, |
| 69 | + private scheduler?: Scheduler) { |
| 70 | + super(); |
| 71 | + } |
| 72 | + |
| 73 | + _subscribe(subscriber) { |
| 74 | + |
| 75 | + let index = 0; |
| 76 | + const project = this.project; |
| 77 | + const thisArg = this.thisArg; |
| 78 | + const iterator = getIterator(Object(this.iterator)); |
| 79 | + const scheduler = this.scheduler; |
| 80 | + |
| 81 | + if (scheduler) { |
| 82 | + subscriber.add(scheduler.schedule(IteratorObservable.dispatch, 0, { |
| 83 | + index, thisArg, project, iterator, subscriber |
| 84 | + })); |
| 85 | + } else { |
| 86 | + do { |
| 87 | + let result = iterator.next(); |
| 88 | + if (result.done) { |
| 89 | + subscriber.complete(); |
| 90 | + break; |
| 91 | + } else if (project) { |
| 92 | + result = tryCatch(project).call(thisArg, result.value, index++); |
| 93 | + if (result === errorObject) { |
| 94 | + subscriber.error(errorObject.e); |
| 95 | + break; |
| 96 | + } |
| 97 | + subscriber.next(result); |
| 98 | + } else { |
| 99 | + subscriber.next(result.value); |
| 100 | + } |
| 101 | + if (subscriber.isUnsubscribed) { |
| 102 | + break; |
| 103 | + } |
| 104 | + } while (true); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +const maxSafeInteger = Math.pow(2, 53) - 1; |
| 110 | + |
| 111 | +class StringIterator { |
| 112 | + constructor(private str: string, |
| 113 | + private idx: number = 0, |
| 114 | + private len: number = str.length) { |
| 115 | + } |
| 116 | + [$$iterator]() { return (this); } |
| 117 | + next() { |
| 118 | + return this.idx < this.len ? { |
| 119 | + done: false, |
| 120 | + value: this.str.charAt(this.idx++) |
| 121 | + } : { |
| 122 | + done: true, |
| 123 | + value: undefined |
| 124 | + }; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +class ArrayIterator { |
| 129 | + constructor(private arr: Array<any>, |
| 130 | + private idx: number = 0, |
| 131 | + private len: number = toLength(arr)) { |
| 132 | + } |
| 133 | + [$$iterator]() { return this; } |
| 134 | + next() { |
| 135 | + return this.idx < this.len ? { |
| 136 | + done: false, |
| 137 | + value: this.arr[this.idx++] |
| 138 | + } : { |
| 139 | + done: true, |
| 140 | + value: undefined |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +function getIterator(o) { |
| 146 | + const i = o[$$iterator]; |
| 147 | + if (!i && typeof o === 'string') { |
| 148 | + return new StringIterator(o); |
| 149 | + } |
| 150 | + if (!i && o.length !== undefined) { |
| 151 | + return new ArrayIterator(o); |
| 152 | + } |
| 153 | + if (!i) { |
| 154 | + throw new TypeError('Object is not iterable'); |
| 155 | + } |
| 156 | + return o[$$iterator](); |
| 157 | +} |
| 158 | + |
| 159 | +function toLength(o) { |
| 160 | + let len = +o.length; |
| 161 | + if (isNaN(len)) { |
| 162 | + return 0; |
| 163 | + } |
| 164 | + if (len === 0 || !numberIsFinite(len)) { |
| 165 | + return len; |
| 166 | + } |
| 167 | + len = sign(len) * Math.floor(Math.abs(len)); |
| 168 | + if (len <= 0) { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + if (len > maxSafeInteger) { |
| 172 | + return maxSafeInteger; |
| 173 | + } |
| 174 | + return len; |
| 175 | +} |
| 176 | + |
| 177 | +function numberIsFinite(value) { |
| 178 | + return typeof value === 'number' && root.isFinite(value); |
| 179 | +} |
| 180 | + |
| 181 | +function sign(value) { |
| 182 | + let valueAsNumber = +value; |
| 183 | + if (valueAsNumber === 0) { |
| 184 | + return valueAsNumber; |
| 185 | + } |
| 186 | + if (isNaN(valueAsNumber)) { |
| 187 | + return valueAsNumber; |
| 188 | + } |
| 189 | + return valueAsNumber < 0 ? -1 : 1; |
| 190 | +} |
0 commit comments