<commit_before>import {Observer} from '../Observer';
import {Producer} from '../Producer';
import {Stream} from '../Stream';
import {emptyObserver} from '../utils/emptyObserver';

export class DebugProducer<T> implements Producer<T> {
  public proxy: Observer<T> = emptyObserver;

  constructor(public spy: (t: T) => void = null,
              public ins: Stream<T>) {
  }

  start(out: Stream<T>): void {
    this.proxy = {
      next: (t: T) => {
        if (this.spy) {
          this.spy(t);
        } else {
          console.log(t);
        }
        out.next(t);
      },
      error: (err) => out.error(err),
      end: () => out.end(),
    };
    this.ins.subscribe(this.proxy);
  }

  stop(): void {
    this.ins.unsubscribe(this.proxy);
  }
}<commit_msg>perf(debug): improve performance of debug() operator, using Proxy class<commit_after>import {Observer} from '../Observer';
import {Producer} from '../Producer';
import {Stream} from '../Stream';
import {emptyObserver} from '../utils/emptyObserver';

export class Proxy<T> implements Observer<T> {
  constructor(public out: Stream<T>,
              public p: DebugProducer<T>) {
  }

  next(t: T) {
    if (this.p.spy) {
      this.p.spy(t);
    } else {
      console.log(t);
    }
    this.out.next(t);
  }

  error(err: any) {
    this.out.error(err);
  }

  end() {
    this.out.end();
  }
}

export class DebugProducer<T> implements Producer<T> {
  public proxy: Observer<T> = emptyObserver;

  constructor(public spy: (t: T) => void = null,
              public ins: Stream<T>) {
  }

  start(out: Stream<T>): void {
    this.ins.subscribe(this.proxy = new Proxy(out, this));
  }

  stop(): void {
    this.ins.unsubscribe(this.proxy);
  }
}
{'fraction_non_alphanumeric': 0.17416715371127994, 'fraction_numerical': 0.0, 'mean_word_length': 2.634819532908705, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7223114', 'n_tokens_mistral': 590, 'n_tokens_neox': 576, 'n_words': 132}