2 import { async } from '../scheduler/async';
3 import { OperatorFunction, SchedulerLike, Timestamp as TimestampInterface } from '../types';
4 import { map } from './map';
7 * Attaches a timestamp to each item emitted by an observable indicating when it was emitted
9 * The `timestamp` operator maps the *source* observable stream to an object of type
10 * `{value: T, timestamp: R}`. The properties are generically typed. The `value` property contains the value
11 * and type of the *source* observable. The `timestamp` is generated by the schedulers `now` function. By
12 * default it uses the *async* scheduler which simply returns `Date.now()` (milliseconds since 1970/01/01
13 * 00:00:00:000) and therefore is of type `number`.
19 * In this example there is a timestamp attached to the documents click event.
22 * import { fromEvent } from 'rxjs';
23 * import { timestamp } from 'rxjs/operators';
25 * const clickWithTimestamp = fromEvent(document, 'click').pipe(
29 * // Emits data of type {value: MouseEvent, timestamp: number}
30 * clickWithTimestamp.subscribe(data => {
36 * @return {Observable<Timestamp<any>>|WebSocketSubject<T>|Observable<T>}
40 export function timestamp<T>(scheduler: SchedulerLike = async): OperatorFunction<T, Timestamp<T>> {
41 return map((value: T) => new Timestamp(value, scheduler.now()));
42 // return (source: Observable<T>) => source.lift(new TimestampOperator(scheduler));
45 export class Timestamp<T> implements TimestampInterface<T> {
46 constructor(public value: T, public timestamp: number) {