-
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
1 parent
c0f8508
commit 9156d61
Showing
4 changed files
with
61 additions
and
1 deletion.
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,15 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.repeat()', function () { | ||
it('should resubscribe count number of times', function (done) { | ||
var expected = [1, 2, 1, 2]; | ||
Observable.of(1,2) | ||
.repeat(2) | ||
.subscribe(function(x){ | ||
expect(x).toBe(expected.shift()); | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
import Observable from '../Observable'; | ||
import Subject from '../Subject'; | ||
import Subscription from '../Subscription'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
|
||
export default function repeat<T>(count: number): Observable<T> { | ||
return this.lift(new RepeatOperator(count, this)); | ||
} | ||
|
||
export class RepeatOperator<T, R> extends Operator<T, R> { | ||
constructor(protected count: number, protected original:Observable<T>) { | ||
super(); | ||
} | ||
|
||
call(observer: Observer<T>): Observer<T> { | ||
return new RepeatSubscriber<T>(observer, this.count, this.original); | ||
} | ||
} | ||
|
||
export class RepeatSubscriber<T> extends Subscriber<T> { | ||
private repeated: number = 0; | ||
constructor(destination: Observer<T>, public count: number, public original: Observable<T>) { | ||
super(destination); | ||
} | ||
|
||
_complete(){ | ||
if (this.count === (this.repeated+=1)){ | ||
this.destination.complete(); | ||
}else{ | ||
this.resubscribe(); | ||
} | ||
} | ||
|
||
resubscribe() { | ||
this.original.subscribe(this); | ||
} | ||
} |