<commit_before>import { ICallRecord, ICallStart, callRecordType } from "../../collector/callrecord"
import { Event, IEvent, Timing } from "../../collector/event"
import { RxCollector } from "../../collector/ICollector"
import { formatArguments, formatObject } from "../../collector/logger"
import {
  IObservableTree, IObserverTree, ISchedulerInfo, ITreeLogger,
  ObservableTree, ObserverTree, SchedulerInfo, SchedulerType, SubjectTree,

} from "../../oct/oct"
import { getPrototype } from "../../prelude"
import { isObservable, isObserver, isScheduler, isSubject } from "./instrumentation"
import * as RxType from "rxjs"
import { IScheduler } from "rxjs/Scheduler"

let debug = false

type Role = "observable" | "observer" | "scheduler"

function getScheduler<T>(obs: RxType.Observable<T>, record?: ICallStart): IScheduler | undefined {
  return (obs as any).scheduler ||
    (obs as any)._scheduler ||
    (obs as any).operator && (obs as any).operator.scheduler ||
    record && ([].filter.call(record.arguments || [], isScheduler)[0])
}

class SequenceTicker {
  public last = 0
  public used = false

  public next(): void {
    if (this.used) {
      this.used = false
      this.last++
    }
  }
  public get(): number {
    this.used = true
    return this.last
  }
}

export class TreeCollector implements RxCollector {
  public call: ICallRecord
  public symbol: symbol
  public subSymbol: symbol
  public nextId = 1
  public logger: ITreeLogger
  private eventSequencer = new SequenceTicker()

  private wireStarts: WireStart[] = []
  private wires: Wire[] = []

  public otree: IObservableTree[] = []
  public stree: IObserverTree[] = []

  private schedulers: { scheduler: IScheduler, info: ISchedulerInfo }[] = []
  private scheduler?: { scheduler: IScheduler, info: ISchedulerInfo }

  public constructor(logger: ITreeLogger, private Rx: typeof RxType) {
    this.logger = logger
    this.symbol = Symbol("tree")
    this.subSymbol = Symbol("tree2")
  }

  public schedule(scheduler: IScheduler, method: string, action: Function, state: any) {
    this.eventSequencer.next()
  }

  public before(record: ICallStart, parents?: ICallStart[]): this {
    // tag all encountered Observables & Subscribers
    [record.subject, ...record.arguments].forEach(this.tag.bind(this, undefined))

    if (record.method === "schedule") {
      this.schedule(record.subject, record.method, undefined, undefined)
    }

    if (callRecordType(record) === "event" && isObserver(this.Rx, record.subject)) {
      let event = Event.fromRecord(record, this.getTiming())
      if (event) {
        let observer = this.tag("observer", record.subject)
        this.addEvent(observer, event, record.arguments[0])
      }
    }
    return this
  }

  public after(record: ICallRecord): void {<commit_msg>export Rx5 Subscribe-events<commit_after>import { ICallRecord, ICallStart, callRecordType } from "../../collector/callrecord"
import { Event, IEvent, Timing } from "../../collector/event"
import { RxCollector } from "../../collector/ICollector"
import { formatArguments, formatObject } from "../../collector/logger"
import {
  IObservableTree, IObserverTree, ISchedulerInfo, ITreeLogger,
  ObservableTree, ObserverTree, SchedulerInfo, SchedulerType, SubjectTree,

} from "../../oct/oct"
import { getPrototype } from "../../prelude"
import { isObservable, isObserver, isScheduler, isSubject } from "./instrumentation"
import * as RxType from "rxjs"
import { IScheduler } from "rxjs/Scheduler"

let debug = false

type Role = "observable" | "observer" | "scheduler"

function getScheduler<T>(obs: RxType.Observable<T>, record?: ICallStart): IScheduler | undefined {
  return (obs as any).scheduler ||
    (obs as any)._scheduler ||
    (obs as any).operator && (obs as any).operator.scheduler ||
    record && ([].filter.call(record.arguments || [], isScheduler)[0])
}

class SequenceTicker {
  public last = 0
  public used = false

  public next(): void {
    if (this.used) {
      this.used = false
      this.last++
    }
  }
  public get(): number {
    this.used = true
    return this.last
  }
}

export class TreeCollector implements RxCollector {
  public call: ICallRecord
  public symbol: symbol
  public subSymbol: symbol
  public nextId = 1
  public logger: ITreeLogger
  private eventSequencer = new SequenceTicker()

  private wireStarts: WireStart[] = []
  private wires: Wire[] = []

  public otree: IObservableTree[] = []
  public stree: IObserverTree[] = []

  private schedulers: { scheduler: IScheduler, info: ISchedulerInfo }[] = []
  private scheduler?: { scheduler: IScheduler, info: ISchedulerInfo }

  public constructor(logger: ITreeLogger, private Rx: typeof RxType) {
    this.logger = logger
    this.symbol = Symbol("tree")
    this.subSymbol = Symbol("tree2")
  }

  public schedule(scheduler: IScheduler, method: string, action: Function, state: any) {
    this.eventSequencer.next()
  }

  public before(record: ICallStart, parents?: ICallStart[]): this {
    // tag all encountered Observables & Subscribers
    [record.subject, ...record.arguments].forEach(this.tag.bind(this, undefined))

    if (record.method === "schedule") {
      this.schedule(record.subject, record.method, undefined, undefined)
    }

    // Record next/error/complete/dispose events
    if (callRecordType(record) === "event" && isObserver(this.Rx, record.subject)) {
      let event = Event.fromRecord(record, this.getTiming())
      if (event) {
        let observer = this.tag("observer", record.subject)
        this.addEvent(observer, event, record.arguments[0])
      }
    }
    // Record subscribe-event
    if (callRecordType(record) === "subscribe" && isObservable(this.Rx, record.subject) && isObserver(this.Rx, record.arguments[0])) {
      let event = Event.fromRecord(record, this.getTiming())
      if (event) {
        let observer = this.tag("observer", record.arguments[0], record)
        this.addEvent(observer, event)
      }
    }
    return this
  }

  public after(record: ICallRecord): void {
{'fraction_non_alphanumeric': 0.13533709730986385, 'fraction_numerical': 0.002158751245433411, 'mean_word_length': 3.709147771696638, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3974', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1809, 'n_words': 473}