Skip to content

Commit

Permalink
feat(operator):add repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremymwells committed Aug 22, 2015
1 parent c0f8508 commit 9156d61
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spec/operators/repeat-spec.js
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);
});

});
3 changes: 2 additions & 1 deletion src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ export default class Observable<T> {

catch: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
retryWhen: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;
repeat: <T>(count: number) => Observable<T>;

groupBy: <T, R>(keySelector: (value:T) => string, durationSelector?: (group:GroupSubject<R>) => Observable<any>, elementSelector?: (value:T) => R) => Observable<R>;

finally: (ensure: () => void, thisArg?: any) => Observable<T>;
}
}
2 changes: 2 additions & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ observableProto.materialize = materialize;

import _catch from './operators/catch';
import retryWhen from './operators/retryWhen';
import repeat from './operators/repeat';

observableProto.catch = _catch;
observableProto.retryWhen = retryWhen;
observableProto.repeat = repeat;

import _finally from './operators/finally';

Expand Down
42 changes: 42 additions & 0 deletions src/operators/repeat.ts
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);
}
}

0 comments on commit 9156d61

Please sign in to comment.