<commit_before>/**
 * Copyright (c) 2022 The xterm.js authors. All rights reserved.
 * @license MIT
 */

/**
 * A queue of that runs tasks over several idle callbacks, trying to maintain the specified
 * frame rate. The tasks will run in the order they are enqueued, but they will run some time later,
 * and care should be taken to ensure they're non-urgent and will not introduce race conditions.
 */
export class IdleTaskQueue {
  private _tasks: (() => void)[] = [];
  private _idleCallback?: number;
  private _maxTaskDuration: number;
  private _i = 0;

  /**
   * @param targetFps The target frame rate.
   */
  constructor(targetFps: number = 240) {
    this._maxTaskDuration = 1000 / targetFps;
  }

  /**
   * Adds a task to the queue which will run in a future idle callback.
   */
  public enqueue(task: () => void): void {
    this._tasks.push(task);
    this._start();
  }

  /**
   * Flushes the queue, running all remaining tasks synchronously.
   */
  public flush(): void {
    while (this._i < this._tasks.length) {
      this._tasks[this._i++]();
    }
    this.clear();
  }

  /**
   * Clears any remaining tasks from the queue, these will not be run.
   */
  public clear(): void {
    if (this._idleCallback) {
      cancelIdleCallback(this._idleCallback);
      this._idleCallback = undefined;
    }
    this._i = 0;
    this._tasks.length = 0;
  }

  private _start(): void {
    if (!this._idleCallback) {
      this._idleCallback = requestIdleCallback(() => this._process());
    }
  }

  private _process(): void {
    const start = performance.now();
    this._idleCallback = undefined;
    while (this._i < this._tasks.length) {
      this._tasks[this._i++]();
      if (performance.now() - start > this._maxTaskDuration) {
        this._start();
        return;
      }
    }
    this.clear();
  }
}

export class DebouncedIdleTask {
  private _queue: IdleTaskQueue;

  /**
   * @param targetFps The target frame rate.
   */
  constructor(targetFps: number = 240) {
    this._queue = new IdleTaskQueue(targetFps);
  }

  public set(task: () => void): void {
    this._queue.clear();
    this._queue.enqueue(task);
  }

  public flush(): void {<commit_msg>Use the browser's IdleDeadline to determine max task time<commit_after>/**
 * Copyright (c) 2022 The xterm.js authors. All rights reserved.
 * @license MIT
 */

/**
 * A queue of that runs tasks over several idle callbacks, trying to maintain the specified
 * frame rate. The tasks will run in the order they are enqueued, but they will run some time later,
 * and care should be taken to ensure they're non-urgent and will not introduce race conditions.
 */
export class IdleTaskQueue {
  private _tasks: (() => void)[] = [];
  private _idleCallback?: number;
  private _i = 0;

  /**
   * Adds a task to the queue which will run in a future idle callback.
   */
  public enqueue(task: () => void): void {
    this._tasks.push(task);
    this._start();
  }

  /**
   * Flushes the queue, running all remaining tasks synchronously.
   */
  public flush(): void {
    while (this._i < this._tasks.length) {
      this._tasks[this._i++]();
    }
    this.clear();
  }

  /**
   * Clears any remaining tasks from the queue, these will not be run.
   */
  public clear(): void {
    if (this._idleCallback) {
      cancelIdleCallback(this._idleCallback);
      this._idleCallback = undefined;
    }
    this._i = 0;
    this._tasks.length = 0;
  }

  private _start(): void {
    if (!this._idleCallback) {
      this._idleCallback = requestIdleCallback(this._process.bind(this));
    }
  }

  private _process(deadline: IdleDeadline): void {
    this._idleCallback = undefined;
    let taskDuration = 0;
    let longestTask = 0;
    while (this._i < this._tasks.length) {
      taskDuration = performance.now();
      this._tasks[this._i++]();
      taskDuration = performance.now() - taskDuration;
      longestTask = Math.max(taskDuration, longestTask);
      // Guess the following task will take a similar time to task that just finished, allow
      // additional room to try avoid exceeding the deadline
      if (longestTask * 1.5 > deadline.timeRemaining()) {
        this._start();
        return;
      }
    }
    this.clear();
  }
}

/**
 * An object that tracks a single debounced task that will run on the next idle frame. When called
 * multiple times, only the last set task will run.
 */
export class DebouncedIdleTask {
  private _queue: IdleTaskQueue;

  constructor() {
    this._queue = new IdleTaskQueue();
  }

  public set(task: () => void): void {
    this._queue.clear();
    this._queue.enqueue(task);
  }

  public flush(): void {
{'fraction_non_alphanumeric': 0.12109459168282698, 'fraction_numerical': 0.006033182503770739, 'mean_word_length': 2.8049180327868855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '3348947', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1401, 'n_words': 450}