es6/observable/RangeObservable.js
import { Observable } from '../Observable';
/**
* We need this JSDoc comment for affecting ESDoc.
* @extends {Ignored}
* @hide true
*/
export class RangeObservable extends Observable {
constructor(start, count, scheduler) {
super();
this.start = start;
this._count = count;
this.scheduler = scheduler;
}
/**
* 创建一个 Observable ,它发出指定范围内的数字序列。
*
* <span class="informal">发出区间范围内的数字序列。</span>
*
* <img src="./img/range.png" width="100%">
*
* `range` 操作符顺序发出一个区间范围内的连续整数, 你可以决定区间的开始和长度。 默认情况下, 不使用
* 调度器仅仅同步的发送通知, 但是也可以可选的使用可选的调度器来控制发送。
*
* @example <caption>发出从1到10的数</caption>
* var numbers = Rx.Observable.range(1, 10);
* numbers.subscribe(x => console.log(x));
*
* @see {@link timer}
* @see {@link interval}
*
* @param {number} [start=0] 序列中的第一个整数值。
* @param {number} [count=0] 要生成序列的长度。
* @param {Scheduler} [scheduler] 调度器 ( {@link IScheduler} ),用来调度通知的发送。
* @return {Observable} 该 Observable 发出有限区间范围内的连续整数。
* @static true
* @name range
* @owner Observable
*/
static create(start = 0, count = 0, scheduler) {
return new RangeObservable(start, count, scheduler);
}
static dispatch(state) {
const { start, index, count, subscriber } = state;
if (index >= count) {
subscriber.complete();
return;
}
subscriber.next(start);
if (subscriber.closed) {
return;
}
state.index = index + 1;
state.start = start + 1;
this.schedule(state);
}
_subscribe(subscriber) {
let index = 0;
let start = this.start;
const count = this._count;
const scheduler = this.scheduler;
if (scheduler) {
return scheduler.schedule(RangeObservable.dispatch, 0, {
index, count, start, subscriber
});
}
else {
do {
if (index++ >= count) {
subscriber.complete();
break;
}
subscriber.next(start++);
if (subscriber.closed) {
break;
}
} while (true);
}
}
}
//# sourceMappingURL=RangeObservable.js.map