-
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.
feat(operator): Add minimal delay operator.
- Loading branch information
Showing
5 changed files
with
133 additions
and
4 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,18 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
|
||
describe('Observable.prototype.delay()', function () { | ||
it('should delay by 100ms', function (done) { | ||
var time = Date.now(); | ||
Observable | ||
.value(42) | ||
.delay(100) | ||
.subscribe(function (x) { | ||
expect(Date.now() - time >= 100).toBe(true); | ||
}, null, function() { | ||
expect(Date.now() - time >= 100).toBe(true); | ||
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,103 @@ | ||
import Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Scheduler from '../Scheduler'; | ||
import Subscriber from '../Subscriber'; | ||
import Notification from '../Notification'; | ||
|
||
export default function delay<T>(delay: number, scheduler: Scheduler = Scheduler.immediate) { | ||
return this.lift(new DelayOperator(delay, scheduler)); | ||
} | ||
|
||
export class DelayOperator<T, R> extends Operator<T, R> { | ||
|
||
delay: number; | ||
scheduler: Scheduler; | ||
|
||
constructor(delay: number, scheduler: Scheduler) { | ||
super(); | ||
this.delay = delay; | ||
this.scheduler = scheduler; | ||
} | ||
|
||
call(observer: Observer<T>): Observer<T> { | ||
return new DelaySubscriber(observer, this.delay, this.scheduler); | ||
} | ||
} | ||
|
||
export class DelaySubscriber<T> extends Subscriber<T> { | ||
|
||
protected delay: number; | ||
protected queue: Array<any>=[]; | ||
protected scheduler: Scheduler; | ||
protected active: boolean = false; | ||
protected errored: boolean = false; | ||
|
||
static dispatch(state) { | ||
const source = state.source; | ||
const queue = source.queue; | ||
const scheduler = state.scheduler; | ||
const destination = state.destination; | ||
while (queue.length > 0 && (queue[0].time - scheduler.now()) <= 0) { | ||
queue.shift().notification.observe(destination); | ||
} | ||
if (queue.length > 0) { | ||
(<any> this).delay = Math.max(0, queue[0].time - scheduler.now()); | ||
(<any> this).schedule(state); | ||
} else { | ||
source.active = false; | ||
} | ||
} | ||
|
||
constructor(destination: Observer<T>, delay: number, scheduler: Scheduler) { | ||
super(destination); | ||
this.delay = delay; | ||
this.scheduler = scheduler; | ||
} | ||
|
||
_next(x) { | ||
if (this.errored) { | ||
return; | ||
} | ||
const scheduler = this.scheduler; | ||
this.queue.push(new DelayMessage<T>(scheduler.now() + this.delay, Notification.createNext(x))); | ||
if (this.active === false) { | ||
this._schedule(scheduler); | ||
} | ||
} | ||
|
||
_error(e) { | ||
const scheduler = this.scheduler; | ||
this.errored = true; | ||
this.queue = [new DelayMessage<T>(scheduler.now() + this.delay, Notification.createError(e))]; | ||
if (this.active === false) { | ||
this._schedule(scheduler); | ||
} | ||
} | ||
|
||
_complete() { | ||
if (this.errored) { | ||
return; | ||
} | ||
const scheduler = this.scheduler; | ||
this.queue.push(new DelayMessage<T>(scheduler.now() + this.delay, Notification.createComplete())); | ||
if (this.active === false) { | ||
this._schedule(scheduler); | ||
} | ||
} | ||
|
||
_schedule(scheduler) { | ||
this.active = true; | ||
this.add(scheduler.schedule(this.delay, { | ||
source: this, destination: this.destination, scheduler: scheduler | ||
}, DelaySubscriber.dispatch)); | ||
} | ||
} | ||
|
||
class DelayMessage<T> { | ||
time: number; | ||
notification: any; | ||
constructor(time: number, notification: any) { | ||
this.time = time; | ||
this.notification = notification; | ||
} | ||
} |