forked from Reactive-Extensions/RxJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13e8b54
commit 52f153f
Showing
2 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
var ObservableBase = require('./observablebase'); | ||
var AbstractObserver = require('../observer/abstractobserver'); | ||
var bindCallback = require('../internal/bindcallback'); | ||
var tryCatch = require('../internal/trycatchutils').tryCatch; | ||
var inherits = require('util').inherits; | ||
|
||
function CountObserver(o, fn, s) { | ||
this._o = o; | ||
this._fn = fn; | ||
this._s = s; | ||
this._i = 0; | ||
this._c = 0; | ||
AbstractObserver.call(this); | ||
} | ||
|
||
inherits(CountObserver, AbstractObserver); | ||
|
||
CountObserver.prototype.next = function (x) { | ||
if (this._fn) { | ||
var result = tryCatch(this._fn)(x, this._i++, this._s); | ||
if (result === global.Rx.errorObj) { return this._o.onError(result.e); } | ||
Boolean(result) && (this._c++); | ||
} else { | ||
this._c++; | ||
} | ||
}; | ||
CountObserver.prototype.error = function (e) { this._o.onError(e); }; | ||
CountObserver.prototype.completed = function () { | ||
this._o.onNext(this._c); | ||
this._o.onCompleted(); | ||
}; | ||
|
||
function CountObservable(source, fn) { | ||
this.source = source; | ||
this._fn = fn; | ||
ObservableBase.call(this); | ||
} | ||
|
||
inherits(CountObservable, ObservableBase); | ||
|
||
CountObservable.prototype.subscribeCore = function (o) { | ||
return this.source.subscribe(new CountObserver(o, this._fn, this.source)); | ||
}; | ||
|
||
module.exports = function count (source, predicate, thisArg) { | ||
var fn = bindCallback(predicate, thisArg, 3); | ||
return new CountObservable(source, fn); | ||
}; |
Oops, something went wrong.