-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Showing
6 changed files
with
64 additions
and
10 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,13 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('count', function () { | ||
it('should count the values of an observable', function (done) { | ||
Observable.fromArray([1, 2, 3]) | ||
.count() | ||
.subscribe(function (total) { | ||
expect(total).toEqual(3); | ||
}, null, done); | ||
}); | ||
}); |
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
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
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
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
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,31 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
|
||
export default function count() { | ||
return this.lift(new CountOperator()); | ||
} | ||
|
||
export class CountOperator<T, R> extends Operator<T, R> { | ||
call(observer: Observer<number>): Observer<T> { | ||
return new CountSubscriber<T>(observer); | ||
} | ||
} | ||
|
||
export class CountSubscriber<T> extends Subscriber<T> { | ||
|
||
count: number = 0; | ||
|
||
constructor(destination: Observer<number>) { | ||
super(destination); | ||
} | ||
|
||
_next(x) { | ||
this.count += 1; | ||
} | ||
|
||
_complete() { | ||
this.destination.next(this.count); | ||
this.destination.complete(); | ||
} | ||
} |