-
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.
closes #191
- Loading branch information
Showing
5 changed files
with
95 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,19 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
var Scheduler = Rx.Scheduler; | ||
|
||
describe('Observable.prototype.throttle()', function () { | ||
it('should delay calls by the specified amount', function (done) { | ||
var expected = [3, 4]; | ||
var source = Observable.concat(Observable.value(1), | ||
Observable.timer(10).mapTo(2), | ||
Observable.timer(10).mapTo(3), | ||
Observable.timer(100).mapTo(4) | ||
) | ||
.throttle(50) | ||
.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
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,55 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
import Observable from '../Observable'; | ||
import Scheduler from '../Scheduler'; | ||
import Subscription from '../Subscription'; | ||
|
||
import tryCatch from '../util/tryCatch'; | ||
import {errorObject} from '../util/errorObject'; | ||
import bindCallback from '../util/bindCallback'; | ||
|
||
export default function throttle<T>(delay: number, scheduler: Scheduler = Scheduler.nextTick) { | ||
return this.lift(new ThrottleOperator(delay, scheduler)); | ||
} | ||
|
||
export class ThrottleOperator<T, R> extends Operator<T, R> { | ||
constructor(private delay:number, private scheduler:Scheduler) { | ||
super(); | ||
} | ||
|
||
call(observer: Observer<R>): Observer<T> { | ||
return new ThrottleSubscriber(observer, this.delay, this.scheduler); | ||
} | ||
} | ||
|
||
export class ThrottleSubscriber<T, R> extends Subscriber<T> { | ||
private throttled: Subscription<any>; | ||
|
||
constructor(destination:Observer<T>, private delay:number, private scheduler:Scheduler) { | ||
super(destination); | ||
} | ||
|
||
_next(x) { | ||
this.clearThrottle(); | ||
this.add(this.throttled = this.scheduler.schedule(this.delay, { value: x, subscriber: this }, dispatchNext)); | ||
} | ||
|
||
throttledNext(x) { | ||
this.clearThrottle(); | ||
this.destination.next(x); | ||
} | ||
|
||
clearThrottle() { | ||
const throttled = this.throttled; | ||
if (throttled) { | ||
this.remove(throttled); | ||
throttled.unsubscribe(); | ||
this.throttled = null; | ||
} | ||
} | ||
} | ||
|
||
function dispatchNext({ value, subscriber }) { | ||
subscriber.throttledNext(value); | ||
} |