Code coverage report for src/operator/sample.ts

Statements: 100% (35 / 35)      Branches: 100% (2 / 2)      Functions: 100% (13 / 13)      Lines: 100% (29 / 29)      Ignored: none     

All files » src/operator/ » sample.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    1   1 13     1 13     1 13   1   1   13   13 13 13     1 39 39     1 17 15     1   1 13 13     1 17     1 1     1     1  
import {Observable} from '../Observable';
import {Operator} from '../Operator';
import {Subscriber} from '../Subscriber';
 
export function sample<T>(notifier: Observable<any>): Observable<T> {
  return this.lift(new SampleOperator(notifier));
}
 
class SampleOperator<T, R> implements Operator<T, R> {
  constructor(private notifier: Observable<any>) {
  }
 
  call(subscriber: Subscriber<R>) {
    return new SampleSubscriber(subscriber, this.notifier);
  }
}
 
class SampleSubscriber<T> extends Subscriber<T> {
  private lastValue: T;
  private hasValue: boolean = false;
 
  constructor(destination: Subscriber<T>, private notifier: Observable<any>) {
    super(destination);
    this.add(notifier._subscribe(new SampleNotificationSubscriber(this)));
  }
 
  _next(value: T) {
    this.lastValue = value;
    this.hasValue = true;
  }
 
  notifyNext() {
    if (this.hasValue) {
      this.destination.next(this.lastValue);
    }
  }
}
 
class SampleNotificationSubscriber<T> extends Subscriber<T> {
  constructor(private parent: SampleSubscriber<T>) {
    super(null);
  }
 
  _next() {
    this.parent.notifyNext();
  }
 
  _error(err: any) {
    this.parent.error(err);
  }
 
  _complete() {
    //noop
  }
}