Skip to content

Commit

Permalink
fix(every): handle thisArg for scalar and array observables
Browse files Browse the repository at this point in the history
- adds some tests around this as well.

related #889
  • Loading branch information
benlesh committed Dec 9, 2015
1 parent 48c0927 commit eae4b00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
38 changes: 38 additions & 0 deletions spec/operators/every-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,44 @@ describe('Observable.prototype.every()', function () {
return x % 5 === 0;
}

it('should accept thisArg with scalar observables', function () {
var thisArg = {};
var result;

Observable.of(1).every(function () {
result = this;
}, thisArg).subscribe();

expect(result).toBe(thisArg);
});


it('should accept thisArg with array observables', function () {
var thisArg = {};
var result;

Observable.of(1,2,3,4).every(function () {
result = this;
}, thisArg).subscribe();

expect(result).toBe(thisArg);
});

it('should accept thisArg with ordinary observables', function () {
var thisArg = {};
var result;

Observable.create(function (observer) {
observer.next(1);
observer.complete();
})
.every(function () {
result = this;
}, thisArg).subscribe();

expect(result).toBe(thisArg);
});

it('should emit true if source is empty', function () {
var source = hot('-----|');
var sourceSubs = '^ !';
Expand Down
13 changes: 5 additions & 8 deletions src/operator/every.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function every<T>(predicate: (value: T, index: number, source: Observable
let result;

if (source._isScalar) {
result = tryCatch(predicate)(source.value, 0, source);
result = tryCatch(predicate).call(thisArg || this, source.value, 0, source);
if (result === errorObject) {
return new ErrorObservable(errorObject.e, source.scheduler);
} else {
Expand All @@ -24,7 +24,7 @@ export function every<T>(predicate: (value: T, index: number, source: Observable

if (source instanceof ArrayObservable) {
const array = (<ArrayObservable<T>>source).array;
let result = tryCatch((array, predicate) => array.every(<any>predicate))(array, predicate);
let result = tryCatch((array, predicate, thisArg) => array.every(<any>predicate, thisArg))(array, predicate, thisArg);
if (result === errorObject) {
return new ErrorObservable(errorObject.e, source.scheduler);
} else {
Expand All @@ -49,8 +49,8 @@ class EverySubscriber<T, R> extends Subscriber<T> {
private index: number = 0;

constructor(destination: Observer<R>,
private predicate?: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg?: any,
private predicate: (value: T, index: number, source: Observable<T>) => boolean,
private thisArg: any,
private source?: Observable<T>) {
super(destination);
}
Expand All @@ -61,10 +61,7 @@ class EverySubscriber<T, R> extends Subscriber<T> {
}

_next(value: T): void {
const { predicate, thisArg, source } = this;
const tryCaught = tryCatch(predicate);
const index = this.index++;
let result = thisArg ? tryCaught.call(thisArg, value, index, source) : tryCaught(value, index, source);
const result = tryCatch(this.predicate).call(this.thisArg || this, value, this.index++, this.source);

if (result === errorObject) {
this.destination.error(result.e);
Expand Down

0 comments on commit eae4b00

Please sign in to comment.