Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

timestamp.ts 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { async } from '../scheduler/async';
  2. import { OperatorFunction, SchedulerLike, Timestamp as TimestampInterface } from '../types';
  3. import { map } from './map';
  4. /**
  5. * Attaches a timestamp to each item emitted by an observable indicating when it was emitted
  6. *
  7. * The `timestamp` operator maps the *source* observable stream to an object of type
  8. * `{value: T, timestamp: R}`. The properties are generically typed. The `value` property contains the value
  9. * and type of the *source* observable. The `timestamp` is generated by the schedulers `now` function. By
  10. * default it uses the *async* scheduler which simply returns `Date.now()` (milliseconds since 1970/01/01
  11. * 00:00:00:000) and therefore is of type `number`.
  12. *
  13. * ![](timestamp.png)
  14. *
  15. * ## Example
  16. *
  17. * In this example there is a timestamp attached to the documents click event.
  18. *
  19. * ```ts
  20. * import { fromEvent } from 'rxjs';
  21. * import { timestamp } from 'rxjs/operators';
  22. *
  23. * const clickWithTimestamp = fromEvent(document, 'click').pipe(
  24. * timestamp()
  25. * );
  26. *
  27. * // Emits data of type {value: MouseEvent, timestamp: number}
  28. * clickWithTimestamp.subscribe(data => {
  29. * console.log(data);
  30. * });
  31. * ```
  32. *
  33. * @param scheduler
  34. * @return {Observable<Timestamp<any>>|WebSocketSubject<T>|Observable<T>}
  35. * @method timestamp
  36. * @owner Observable
  37. */
  38. export function timestamp<T>(scheduler: SchedulerLike = async): OperatorFunction<T, Timestamp<T>> {
  39. return map((value: T) => new Timestamp(value, scheduler.now()));
  40. // return (source: Observable<T>) => source.lift(new TimestampOperator(scheduler));
  41. }
  42. export class Timestamp<T> implements TimestampInterface<T> {
  43. constructor(public value: T, public timestamp: number) {
  44. }
  45. }