-
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.
refactor(interval): implemented as a simple function (#3251)
- remove IntervalObservable - update tests
- Loading branch information
Showing
3 changed files
with
95 additions
and
125 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,69 @@ | ||
import { IntervalObservable } from './IntervalObservable'; | ||
import { Observable } from '../Observable'; | ||
import { async } from '../scheduler/async'; | ||
import { IScheduler } from '../Scheduler'; | ||
import { isNumeric } from '../util/isNumeric'; | ||
import { Subscriber } from '../Subscriber'; | ||
import { Action } from '../scheduler/Action'; | ||
|
||
export const interval = IntervalObservable.create; | ||
/** | ||
* Creates an Observable that emits sequential numbers every specified | ||
* interval of time, on a specified IScheduler. | ||
* | ||
* <span class="informal">Emits incremental numbers periodically in time. | ||
* </span> | ||
* | ||
* <img src="./img/interval.png" width="100%"> | ||
* | ||
* `interval` returns an Observable that emits an infinite sequence of | ||
* ascending integers, with a constant interval of time of your choosing | ||
* between those emissions. The first emission is not sent immediately, but | ||
* only after the first period has passed. By default, this operator uses the | ||
* `async` IScheduler to provide a notion of time, but you may pass any | ||
* IScheduler to it. | ||
* | ||
* @example <caption>Emits ascending numbers, one every second (1000ms)</caption> | ||
* var numbers = Rx.Observable.interval(1000); | ||
* numbers.subscribe(x => console.log(x)); | ||
* | ||
* @see {@link timer} | ||
* @see {@link delay} | ||
* | ||
* @param {number} [period=0] The interval size in milliseconds (by default) | ||
* or the time unit determined by the scheduler's clock. | ||
* @param {Scheduler} [scheduler=async] The IScheduler to use for scheduling | ||
* the emission of values, and providing a notion of "time". | ||
* @return {Observable} An Observable that emits a sequential number each time | ||
* interval. | ||
* @static true | ||
* @name interval | ||
* @owner Observable | ||
*/ | ||
export function interval(period = 0, | ||
scheduler: IScheduler = async): Observable<number> { | ||
if (!isNumeric(period) || period < 0) { | ||
period = 0; | ||
} | ||
|
||
if (!scheduler || typeof scheduler.schedule !== 'function') { | ||
scheduler = async; | ||
} | ||
|
||
return new Observable<number>(subscriber => { | ||
subscriber.add( | ||
scheduler.schedule(dispatch, period, { subscriber, counter: 0, period }) | ||
); | ||
return subscriber; | ||
}); | ||
} | ||
|
||
function dispatch(this: Action<IntervalState>, state: IntervalState) { | ||
const { subscriber, counter, period } = state; | ||
subscriber.next(counter); | ||
this.schedule({ subscriber, counter: counter + 1, period }, period); | ||
} | ||
|
||
interface IntervalState { | ||
subscriber: Subscriber<number>; | ||
counter: number; | ||
period: number; | ||
} |