|
138 | 138 |
|
139 | 139 | var ReduceObservable = (function(__super__) {
|
140 | 140 | inherits(ReduceObservable, __super__);
|
141 |
| - function ReduceObservable(source, acc, hasSeed, seed) { |
| 141 | + function ReduceObservable(source, accumulator, hasSeed, seed) { |
142 | 142 | this.source = source;
|
143 |
| - this.acc = acc; |
| 143 | + this.accumulator = accumulator; |
144 | 144 | this.hasSeed = hasSeed;
|
145 | 145 | this.seed = seed;
|
146 | 146 | __super__.call(this);
|
147 | 147 | }
|
148 | 148 |
|
149 | 149 | ReduceObservable.prototype.subscribeCore = function(observer) {
|
150 |
| - return this.source.subscribe(new InnerObserver(observer,this)); |
| 150 | + return this.source.subscribe(new ReduceObserver(observer,this)); |
151 | 151 | };
|
152 | 152 |
|
153 |
| - function InnerObserver(o, parent) { |
154 |
| - this.o = o; |
155 |
| - this.acc = parent.acc; |
156 |
| - this.hasSeed = parent.hasSeed; |
157 |
| - this.seed = parent.seed; |
158 |
| - this.hasAccumulation = false; |
159 |
| - this.result = null; |
160 |
| - this.hasValue = false; |
161 |
| - this.isStopped = false; |
| 153 | + return ReduceObservable; |
| 154 | + }(ObservableBase)); |
| 155 | + |
| 156 | + var ReduceObserver = (function (__super__) { |
| 157 | + inherits(ReduceObserver, __super__); |
| 158 | + function ReduceObserver(o, parent) { |
| 159 | + this._o = o; |
| 160 | + this._p = parent; |
| 161 | + this._fn = parent.accumulator; |
| 162 | + this._hs = parent.hasSeed; |
| 163 | + this._s = parent.seed; |
| 164 | + this._ha = false; |
| 165 | + this._a = null; |
| 166 | + this._hv = false; |
| 167 | + this._i = 0; |
| 168 | + __super__.call(this); |
162 | 169 | }
|
163 |
| - InnerObserver.prototype.onNext = function (x) { |
164 |
| - if (this.isStopped) { return; } |
165 |
| - !this.hasValue && (this.hasValue = true); |
166 |
| - if (this.hasAccumulation) { |
167 |
| - this.result = tryCatch(this.acc)(this.result, x); |
| 170 | + |
| 171 | + ReduceObserver.prototype.next = function (x) { |
| 172 | + !this._hv && (this._hv = true); |
| 173 | + if (this._ha) { |
| 174 | + this._a = tryCatch(this._fn)(this._a, x, this._i, this._p); |
168 | 175 | } else {
|
169 |
| - this.result = this.hasSeed ? tryCatch(this.acc)(this.seed, x) : x; |
170 |
| - this.hasAccumulation = true; |
| 176 | + this._a = this._hs ? tryCatch(this._fn)(this._s, x, this._i, this._p) : x; |
| 177 | + this._ha = true; |
171 | 178 | }
|
172 |
| - if (this.result === errorObj) { this.o.onError(this.result.e); } |
173 |
| - }; |
174 |
| - InnerObserver.prototype.onError = function (e) { |
175 |
| - if (!this.isStopped) { this.isStopped = true; this.o.onError(e); } |
| 179 | + if (this._a === errorObj) { return this._o.onError(this._a.e); } |
| 180 | + this._i++; |
176 | 181 | };
|
177 |
| - InnerObserver.prototype.onCompleted = function () { |
178 |
| - if (!this.isStopped) { |
179 |
| - this.isStopped = true; |
180 |
| - this.hasValue && this.o.onNext(this.result); |
181 |
| - !this.hasValue && this.hasSeed && this.o.onNext(this.seed); |
182 |
| - !this.hasValue && !this.hasSeed && this.o.onError(new EmptyError()); |
183 |
| - this.o.onCompleted(); |
184 |
| - } |
| 182 | + |
| 183 | + ReduceObserver.prototype.error = function (e) { |
| 184 | + this._o.onError(e); |
185 | 185 | };
|
186 |
| - InnerObserver.prototype.dispose = function () { this.isStopped = true; }; |
187 |
| - InnerObserver.prototype.fail = function(e) { |
188 |
| - if (!this.isStopped) { |
189 |
| - this.isStopped = true; |
190 |
| - this.o.onError(e); |
191 |
| - return true; |
192 |
| - } |
193 |
| - return false; |
| 186 | + |
| 187 | + ReduceObserver.prototype.completed = function () { |
| 188 | + this._hv && this._o.onNext(this._a); |
| 189 | + !this._hv && this._hs && this._o.onNext(this._s); |
| 190 | + !this._hv && !this._hs && this._o.onError(new EmptyError()); |
| 191 | + this._o.onCompleted(); |
194 | 192 | };
|
195 | 193 |
|
196 |
| - return ReduceObservable; |
197 |
| - }(ObservableBase)); |
| 194 | + return ReduceObserver; |
| 195 | + }(AbstractObserver)); |
198 | 196 |
|
199 | 197 | /**
|
200 | 198 | * Applies an accumulator function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified seed value is used as the initial accumulator value.
|
|
203 | 201 | * @param {Any} [seed] The initial accumulator value.
|
204 | 202 | * @returns {Observable} An observable sequence containing a single element with the final accumulator value.
|
205 | 203 | */
|
206 |
| - observableProto.reduce = function (accumulator) { |
207 |
| - var hasSeed = false; |
| 204 | + observableProto.reduce = function () { |
| 205 | + var hasSeed = false, seed, accumulator = arguments[0]; |
208 | 206 | if (arguments.length === 2) {
|
209 | 207 | hasSeed = true;
|
210 |
| - var seed = arguments[1]; |
| 208 | + seed = arguments[1]; |
211 | 209 | }
|
212 | 210 | return new ReduceObservable(this, accumulator, hasSeed, seed);
|
213 | 211 | };
|
|
481 | 479 | return new CountObservable(this, fn);
|
482 | 480 | };
|
483 | 481 |
|
| 482 | + var IndexOfObservable = (function (__super__) { |
| 483 | + inherits(IndexOfObservable, __super__); |
| 484 | + function IndexOfObservable(source, e, n) { |
| 485 | + this.source = source; |
| 486 | + this._e = e; |
| 487 | + this._n = n; |
| 488 | + __super__.call(this); |
| 489 | + } |
| 490 | + |
| 491 | + IndexOfObservable.prototype.subscribeCore = function (o) { |
| 492 | + if (this._n < 0) { |
| 493 | + o.onNext(-1); |
| 494 | + o.onCompleted(); |
| 495 | + return disposableEmpty; |
| 496 | + } |
| 497 | + |
| 498 | + return this.source.subscribe(new IndexOfObserver(o, this._e, this._n)); |
| 499 | + }; |
| 500 | + |
| 501 | + return IndexOfObservable; |
| 502 | + }(ObservableBase)); |
| 503 | + |
| 504 | + var IndexOfObserver = (function (__super__) { |
| 505 | + inherits(IndexOfObserver, __super__); |
| 506 | + function IndexOfObserver(o, e, n) { |
| 507 | + this._o = o; |
| 508 | + this._e = e; |
| 509 | + this._n = n; |
| 510 | + this._i = 0; |
| 511 | + __super__.call(this); |
| 512 | + } |
| 513 | + |
| 514 | + IndexOfObserver.prototype.next = function (x) { |
| 515 | + if (this._i >= this._n && x === this._e) { |
| 516 | + this._o.onNext(this._i); |
| 517 | + this._o.onCompleted(); |
| 518 | + } |
| 519 | + this._i++; |
| 520 | + }; |
| 521 | + IndexOfObserver.prototype.error = function (e) { this._o.onError(e); }; |
| 522 | + IndexOfObserver.prototype.completed = function () { this._o.onNext(-1); this._o.onCompleted(); }; |
| 523 | + |
| 524 | + return IndexOfObserver; |
| 525 | + }(AbstractObserver)); |
| 526 | + |
484 | 527 | /**
|
485 | 528 | * Returns the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
|
486 | 529 | * @param {Any} searchElement Element to locate in the array.
|
487 | 530 | * @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
|
488 | 531 | * @returns {Observable} And observable sequence containing the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
|
489 | 532 | */
|
490 | 533 | observableProto.indexOf = function(searchElement, fromIndex) {
|
491 |
| - var source = this; |
492 |
| - return new AnonymousObservable(function (o) { |
493 |
| - var i = 0, n = +fromIndex || 0; |
494 |
| - Math.abs(n) === Infinity && (n = 0); |
495 |
| - if (n < 0) { |
496 |
| - o.onNext(-1); |
497 |
| - o.onCompleted(); |
498 |
| - return disposableEmpty; |
499 |
| - } |
500 |
| - return source.subscribe( |
501 |
| - function (x) { |
502 |
| - if (i >= n && x === searchElement) { |
503 |
| - o.onNext(i); |
504 |
| - o.onCompleted(); |
505 |
| - } |
506 |
| - i++; |
507 |
| - }, |
508 |
| - function (e) { o.onError(e); }, |
509 |
| - function () { |
510 |
| - o.onNext(-1); |
511 |
| - o.onCompleted(); |
512 |
| - }); |
513 |
| - }, source); |
| 534 | + var n = +fromIndex || 0; |
| 535 | + Math.abs(n) === Infinity && (n = 0); |
| 536 | + return new IndexOfObservable(this, searchElement, n); |
514 | 537 | };
|
515 | 538 |
|
516 | 539 | /**
|
|
1165 | 1188 | return new ToMapObservable(this, keySelector, elementSelector);
|
1166 | 1189 | };
|
1167 | 1190 |
|
| 1191 | + var SliceObservable = (function (__super__) { |
| 1192 | + inherits(SliceObservable, __super__); |
| 1193 | + function SliceObservable(source, b, e) { |
| 1194 | + this.source = source; |
| 1195 | + this._b = b; |
| 1196 | + this._e = e; |
| 1197 | + __super__.call(this); |
| 1198 | + } |
| 1199 | + |
| 1200 | + SliceObservable.prototype.subscribeCore = function (o) { |
| 1201 | + return this.source.subscribe(new SliceObserver(o, this._b, this._e)); |
| 1202 | + }; |
| 1203 | + |
| 1204 | + return SliceObservable; |
| 1205 | + }(ObservableBase)); |
| 1206 | + |
| 1207 | + var SliceObserver = (function (__super__) { |
| 1208 | + inherits(SliceObserver, __super__); |
| 1209 | + |
| 1210 | + function SliceObserver(o, b, e) { |
| 1211 | + this._o = o; |
| 1212 | + this._b = b; |
| 1213 | + this._e = e; |
| 1214 | + this._i = 0; |
| 1215 | + __super__.call(this); |
| 1216 | + } |
| 1217 | + |
| 1218 | + SliceObserver.prototype.next = function (x) { |
| 1219 | + if (this._i >= this._b) { |
| 1220 | + if (this._e === this._i) { |
| 1221 | + this._o.onCompleted(); |
| 1222 | + } else { |
| 1223 | + this._o.onNext(x); |
| 1224 | + } |
| 1225 | + } |
| 1226 | + this._i++; |
| 1227 | + }; |
| 1228 | + SliceObserver.prototype.error = function (e) { this._o.onError(e); }; |
| 1229 | + SliceObserver.prototype.completed = function () { this._o.onCompleted(); }; |
| 1230 | + |
| 1231 | + return SliceObserver; |
| 1232 | + }(AbstractObserver)); |
| 1233 | + |
| 1234 | + /* |
| 1235 | + * The slice() method returns a shallow copy of a portion of an Observable into a new Observable object. |
| 1236 | + * Unlike the array version, this does not support negative numbers for being or end. |
| 1237 | + * @param {Number} [begin] Zero-based index at which to begin extraction. If omitted, this will default to zero. |
| 1238 | + * @param {Number} [end] Zero-based index at which to end extraction. slice extracts up to but not including end. |
| 1239 | + * If omitted, this will emit the rest of the Observable object. |
| 1240 | + * @returns {Observable} A shallow copy of a portion of an Observable into a new Observable object. |
| 1241 | + */ |
| 1242 | + observableProto.slice = function (begin, end) { |
| 1243 | + var start = begin || 0; |
| 1244 | + if (start < 0) { throw new Rx.ArgumentOutOfRangeError(); } |
| 1245 | + if (typeof end === 'number' && end < start) { |
| 1246 | + throw new Rx.ArgumentOutOfRangeError(); |
| 1247 | + } |
| 1248 | + return new SliceObservable(this, start, end); |
| 1249 | + }; |
| 1250 | + |
1168 | 1251 | return Rx;
|
1169 | 1252 | }));
|
0 commit comments