Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 38ae7be

Browse files
Adding slice
1 parent 6610415 commit 38ae7be

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1625
-885
lines changed

Gruntfile.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ module.exports = function (grunt) {
284284
'src/core/linq/observable/findindex.js', // _findvalue, where
285285
'src/core/linq/observable/toset.js',
286286
'src/core/linq/observable/tomap.js',
287+
'src/core/linq/observable/slice.js',
287288

288289
// Async operators
289290
'src/core/linq/observable/spawn.js',
@@ -567,6 +568,7 @@ module.exports = function (grunt) {
567568
'src/core/linq/observable/findindex.js', // _findvalue, where
568569
'src/core/linq/observable/toset.js',
569570
'src/core/linq/observable/tomap.js',
571+
'src/core/linq/observable/slice.js',
570572

571573
// Async compat operators
572574
'src/core/linq/observable/spawn.js',
@@ -1491,6 +1493,7 @@ module.exports = function (grunt) {
14911493
'src/core/linq/observable/findindex.js', // _findvalue, where
14921494
'src/core/linq/observable/toset.js',
14931495
'src/core/linq/observable/tomap.js',
1496+
'src/core/linq/observable/slice.js',
14941497
'src/core/headers/suboutro.js'
14951498
],
14961499
dest: 'dist/rx.aggregates.js'
@@ -1526,6 +1529,7 @@ module.exports = function (grunt) {
15261529
'src/core/linq/observable/findindex.js', // _findvalue, where
15271530
'src/core/linq/observable/toset.js',
15281531
'src/core/linq/observable/tomap.js',
1532+
'src/core/linq/observable/slice.js',
15291533
'src/core/headers/suboutro.js'
15301534
],
15311535
dest: 'modules/rx-lite-aggregates/rx.lite.aggregates.js'
@@ -1561,6 +1565,7 @@ module.exports = function (grunt) {
15611565
'src/core/linq/observable/findindex.js', // _findvalue, where
15621566
'src/core/linq/observable/toset.js',
15631567
'src/core/linq/observable/tomap.js',
1568+
'src/core/linq/observable/slice.js',
15641569
'src/core/headers/suboutro.js'
15651570
],
15661571
dest: 'modules/rx-lite-aggregates-compat/rx.lite.aggregates.compat.js'

dist/rx.aggregates.js

Lines changed: 150 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -138,63 +138,61 @@
138138

139139
var ReduceObservable = (function(__super__) {
140140
inherits(ReduceObservable, __super__);
141-
function ReduceObservable(source, acc, hasSeed, seed) {
141+
function ReduceObservable(source, accumulator, hasSeed, seed) {
142142
this.source = source;
143-
this.acc = acc;
143+
this.accumulator = accumulator;
144144
this.hasSeed = hasSeed;
145145
this.seed = seed;
146146
__super__.call(this);
147147
}
148148

149149
ReduceObservable.prototype.subscribeCore = function(observer) {
150-
return this.source.subscribe(new InnerObserver(observer,this));
150+
return this.source.subscribe(new ReduceObserver(observer,this));
151151
};
152152

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);
162169
}
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);
168175
} 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;
171178
}
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++;
176181
};
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);
185185
};
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();
194192
};
195193

196-
return ReduceObservable;
197-
}(ObservableBase));
194+
return ReduceObserver;
195+
}(AbstractObserver));
198196

199197
/**
200198
* 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,11 +201,11 @@
203201
* @param {Any} [seed] The initial accumulator value.
204202
* @returns {Observable} An observable sequence containing a single element with the final accumulator value.
205203
*/
206-
observableProto.reduce = function (accumulator) {
207-
var hasSeed = false;
204+
observableProto.reduce = function () {
205+
var hasSeed = false, seed, accumulator = arguments[0];
208206
if (arguments.length === 2) {
209207
hasSeed = true;
210-
var seed = arguments[1];
208+
seed = arguments[1];
211209
}
212210
return new ReduceObservable(this, accumulator, hasSeed, seed);
213211
};
@@ -481,36 +479,61 @@
481479
return new CountObservable(this, fn);
482480
};
483481

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+
484527
/**
485528
* Returns the first index at which a given element can be found in the observable sequence, or -1 if it is not present.
486529
* @param {Any} searchElement Element to locate in the array.
487530
* @param {Number} [fromIndex] The index to start the search. If not specified, defaults to 0.
488531
* @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.
489532
*/
490533
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);
514537
};
515538

516539
/**
@@ -1165,5 +1188,65 @@
11651188
return new ToMapObservable(this, keySelector, elementSelector);
11661189
};
11671190

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+
11681251
return Rx;
11691252
}));

dist/rx.aggregates.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)