skip.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Operator } from '../Operator';
  2. import { Subscriber } from '../Subscriber';
  3. import { Observable } from '../Observable';
  4. import { MonoTypeOperatorFunction, TeardownLogic } from '../types';
  5. /**
  6. * Returns an Observable that skips the first `count` items emitted by the source Observable.
  7. *
  8. * ![](skip.png)
  9. *
  10. * @param {Number} count - The number of times, items emitted by source Observable should be skipped.
  11. * @return {Observable} An Observable that skips values emitted by the source Observable.
  12. *
  13. * @method skip
  14. * @owner Observable
  15. */
  16. export function skip<T>(count: number): MonoTypeOperatorFunction<T> {
  17. return (source: Observable<T>) => source.lift(new SkipOperator(count));
  18. }
  19. class SkipOperator<T> implements Operator<T, T> {
  20. constructor(private total: number) {
  21. }
  22. call(subscriber: Subscriber<T>, source: any): TeardownLogic {
  23. return source.subscribe(new SkipSubscriber(subscriber, this.total));
  24. }
  25. }
  26. /**
  27. * We need this JSDoc comment for affecting ESDoc.
  28. * @ignore
  29. * @extends {Ignored}
  30. */
  31. class SkipSubscriber<T> extends Subscriber<T> {
  32. count: number = 0;
  33. constructor(destination: Subscriber<T>, private total: number) {
  34. super(destination);
  35. }
  36. protected _next(x: T) {
  37. if (++this.count > this.total) {
  38. this.destination.next(x);
  39. }
  40. }
  41. }