@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
80 lines • 2.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var async_1 = require("../scheduler/async");
var scan_1 = require("./scan");
var defer_1 = require("../observable/defer");
var map_1 = require("./map");
/**
*
* Emits an object containing the current value, and the time that has
* passed between emitting the current value and the previous value, which is
* calculated by using the provided `scheduler`'s `now()` method to retrieve
* the current time at each emission, then calculating the difference. The `scheduler`
* defaults to {@link asyncScheduler}, so by default, the `interval` will be in
* milliseconds.
*
* <span class="informal">Convert an Observable that emits items into one that
* emits indications of the amount of time elapsed between those emissions.</span>
*
* 
*
* ## Examples
* Emit inteval between current value with the last value
*
* ```ts
* const seconds = interval(1000);
*
* seconds.pipe(timeInterval())
* .subscribe(
* value => console.log(value),
* err => console.log(err),
* );
*
* seconds.pipe(timeout(900))
* .subscribe(
* value => console.log(value),
* err => console.log(err),
* );
*
* // NOTE: The values will never be this precise,
* // intervals created with `interval` or `setInterval`
* // are non-deterministic.
*
* // {value: 0, interval: 1000}
* // {value: 1, interval: 1000}
* // {value: 2, interval: 1000}
* ```
*
* @param {SchedulerLike} [scheduler] Scheduler used to get the current time.
* @return {Observable<{ interval: number, value: T }>} Observable that emit infomation about value and interval
* @method timeInterval
*/
function timeInterval(scheduler) {
if (scheduler === void 0) { scheduler = async_1.async; }
return function (source) { return defer_1.defer(function () {
return source.pipe(
// TODO(benlesh): correct these typings.
scan_1.scan(function (_a, value) {
var current = _a.current;
return ({ value: value, current: scheduler.now(), last: current });
}, { current: scheduler.now(), value: undefined, last: undefined }), map_1.map(function (_a) {
var current = _a.current, last = _a.last, value = _a.value;
return new TimeInterval(value, current - last);
}));
}); };
}
exports.timeInterval = timeInterval;
// TODO(benlesh): make this an interface, export the interface, but not the implemented class,
// there's no reason users should be manually creating this type.
/**
* @deprecated exposed API, use as interface only.
*/
var TimeInterval = /** @class */ (function () {
function TimeInterval(value, interval) {
this.value = value;
this.interval = interval;
}
return TimeInterval;
}());
exports.TimeInterval = TimeInterval;
//# sourceMappingURL=timeInterval.js.map