<commit_before>/* @flow */

type EventBufferMode = 'idle' | 'timeout'
type FlushCallback<EventType> = (EventType[]) => void

// An event buffer.
// Needs a flush callback to be called everytime the buffer is flushed.
//
// In *idle* mode (default), the buffer will only store pushed events and
// you'll have to flush it manually.
// In *timeout* mode, the buffer flushes stored events by itself when nothing
// happens during `timeoutInMs`.
//
// The buffer will also flush automatically when switching from one mode to
// another.
//
// Right now this class is in the local/ namespace because this is where it is
// used, but it could be anywhere else since it doesn't have any dependency.
export default class EventBuffer<EventType> {
  events: EventType[]
  mode: EventBufferMode
  timeoutInMs: number
  timeout: *
  flushed: FlushCallback<EventType>

  constructor (timeoutInMs: number, flushed: FlushCallback<EventType>) {
    this.events = []
    this.mode = 'idle'
    this.timeoutInMs = timeoutInMs
    this.timeout = null
    this.flushed = flushed
  }

  push (event: EventType): void {
    this.events.push(event)
    this.shiftTimeout()
  }

  shiftTimeout (): void {
    if (this.mode === 'timeout') {
      this.clearTimeout()
      this.timeout = setTimeout(this.flush, this.timeoutInMs)
    }
  }

  clearTimeout (): void {
    if (this.timeout != null) {
      clearTimeout(this.timeout)
      delete this.timeout
    }
  }

  flush (): void {
    this.clearTimeout()
    if (this.events.length > 0) {
      const flushedEvents = this.events
      this.events = []
      this.flushed(flushedEvents)
    }
  }

  switchMode (mode: EventBufferMode): void {
    this.flush()
    this.mode = mode
  }
}
<commit_msg>Allow FlushCallback to return a result (whatever its type)<commit_after>/* @flow */

type EventBufferMode = 'idle' | 'timeout'
type FlushCallback<EventType> = (EventType[]) => any

// An event buffer.
// Needs a flush callback to be called everytime the buffer is flushed.
//
// In *idle* mode (default), the buffer will only store pushed events and
// you'll have to flush it manually.
// In *timeout* mode, the buffer flushes stored events by itself when nothing
// happens during `timeoutInMs`.
//
// The buffer will also flush automatically when switching from one mode to
// another.
//
// Right now this class is in the local/ namespace because this is where it is
// used, but it could be anywhere else since it doesn't have any dependency.
export default class EventBuffer<EventType> {
  events: EventType[]
  mode: EventBufferMode
  timeoutInMs: number
  timeout: *
  flushed: FlushCallback<EventType>

  constructor (timeoutInMs: number, flushed: FlushCallback<EventType>) {
    this.events = []
    this.mode = 'idle'
    this.timeoutInMs = timeoutInMs
    this.timeout = null
    this.flushed = flushed
  }

  push (event: EventType): void {
    this.events.push(event)
    this.shiftTimeout()
  }

  shiftTimeout (): void {
    if (this.mode === 'timeout') {
      this.clearTimeout()
      this.timeout = setTimeout(this.flush, this.timeoutInMs)
    }
  }

  clearTimeout (): void {
    if (this.timeout != null) {
      clearTimeout(this.timeout)
      delete this.timeout
    }
  }

  flush (): void {
    this.clearTimeout()
    if (this.events.length > 0) {
      const flushedEvents = this.events
      this.events = []
      this.flushed(flushedEvents)
    }
  }

  switchMode (mode: EventBufferMode): void {
    this.flush()
    this.mode = mode
  }
}

{'fraction_non_alphanumeric': 0.11193603655054254, 'fraction_numerical': 0.0005711022272986865, 'mean_word_length': 3.0310701956271577, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '5975487', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1037, 'n_words': 342}