<commit_before>/**
 * @license
 * Copyright 2016 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

require('./status.css');

let statusContainer: HTMLElement|null = null;

export var DEFAULT_STATUS_DELAY = 200;

export type Delay = boolean | number;

export class StatusMessage {
  element: HTMLElement;
  private timer: number|null;
  constructor(delay: Delay = false) {
    if (statusContainer === null) {
      statusContainer = document.createElement('ul');
      statusContainer.id = 'statusContainer';
      document.body.appendChild(statusContainer);
    }
    let element = document.createElement('li');
    this.element = element;
    if (delay === true) {
      delay = DEFAULT_STATUS_DELAY;
    }
    if (delay !== false) {
      this.setVisible(false);
      this.timer = setTimeout(this.setVisible.bind(this, true), delay);
    } else {
      this.timer = null;
    }
    statusContainer.appendChild(element);
  }
  dispose() {
    statusContainer!.removeChild(this.element);
    this.element = <any>undefined;
    if (this.timer !== null) {
      clearTimeout(this.timer);
    }
  }
  setText(text: string, makeVisible?: boolean) {
    this.element.textContent = text;
    if (makeVisible) {
      this.setVisible(true);
    }
  }
  setHTML(text: string, makeVisible?: boolean) {
    this.element.innerHTML = text;
    if (makeVisible) {
      this.setVisible(true);
    }
  }
  setVisible(value: boolean) {
    if (this.timer !== null) {
      clearTimeout(this.timer);
      this.timer = null;
    }
    this.element.style.display = value ? 'block' : 'none';
  }

  static forPromise<T>(
      promise: Promise<T>,
      options: {initialMessage: string, delay?: Delay, errorPrefix: string}): Promise<T> {
    let status = new StatusMessage(options.delay);
    status.setText(options.initialMessage);
    let dispose = status.dispose.bind(status);
    promise.then(dispose, reason => {
      let msg: string;
      if (reason instanceof Error) {
        msg = reason.message;
      } else {
        msg = '' + reason;
      }
      let {errorPrefix = ''} = options;
      status.element.textContent = errorPrefix + msg + '  ';
      let button = document.createElement('button');
      button.textContent = 'Dismiss';
      button.addEventListener('click', () => {
        status.dispose();
      });
      status.element.appendChild(button);
      status.setVisible(true);
    });
    return promise;
  }

  static showMessage(message: string): StatusMessage {
    const msg = new StatusMessage();
    msg.element.textContent = message;
    msg.setVisible(true);
    return msg;
  }

  static showTemporaryMessage(message: string, closeAfter: number = 2000): StatusMessage {
    const msg = this.showMessage(message);
    setTimeout(() => msg.dispose(), closeAfter);
    return msg;
  }
}<commit_msg>refactor(status): factor out setErrorMessage functionality<commit_after>/**
 * @license
 * Copyright 2016 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

require('./status.css');

let statusContainer: HTMLElement|null = null;

export var DEFAULT_STATUS_DELAY = 200;

export type Delay = boolean | number;

export class StatusMessage {
  element: HTMLElement;
  private timer: number|null;
  constructor(delay: Delay = false) {
    if (statusContainer === null) {
      statusContainer = document.createElement('ul');
      statusContainer.id = 'statusContainer';
      document.body.appendChild(statusContainer);
    }
    let element = document.createElement('li');
    this.element = element;
    if (delay === true) {
      delay = DEFAULT_STATUS_DELAY;
    }
    if (delay !== false) {
      this.setVisible(false);
      this.timer = setTimeout(this.setVisible.bind(this, true), delay);
    } else {
      this.timer = null;
    }
    statusContainer.appendChild(element);
  }
  dispose() {
    statusContainer!.removeChild(this.element);
    this.element = <any>undefined;
    if (this.timer !== null) {
      clearTimeout(this.timer);
    }
  }
  setText(text: string, makeVisible?: boolean) {
    this.element.textContent = text;
    if (makeVisible) {
      this.setVisible(true);
    }
  }
  setHTML(text: string, makeVisible?: boolean) {
    this.element.innerHTML = text;
    if (makeVisible) {
      this.setVisible(true);
    }
  }
  setVisible(value: boolean) {
    if (this.timer !== null) {
      clearTimeout(this.timer);
      this.timer = null;
    }
    this.element.style.display = value ? 'block' : 'none';
  }

  static forPromise<T>(
      promise: Promise<T>,
      options: {initialMessage: string, delay?: Delay, errorPrefix: string}): Promise<T> {
    let status = new StatusMessage(options.delay);
    status.setText(options.initialMessage);
    let dispose = status.dispose.bind(status);
    promise.then(dispose, reason => {
      let msg: string;
      if (reason instanceof Error) {
        msg = reason.message;
      } else {
        msg = '' + reason;
      }
      let {errorPrefix = ''} = options;
      status.setErrorMessage(errorPrefix + msg);
      status.setVisible(true);
    });
    return promise;
  }

  setErrorMessage(message: string) {
    this.element.textContent = message + ' ';
    let button = document.createElement('button');
    button.textContent = 'Dismiss';
    button.addEventListener('click', () => {
      this.dispose();
    });
    this.element.appendChild(button);
  }

  static showMessage(message: string): StatusMessage {
    const msg = new StatusMessage();
    msg.element.textContent = message;
    msg.setVisible(true);
    return msg;
  }

  static showTemporaryMessage(message: string, closeAfter: number = 2000): StatusMessage {
    const msg = this.showMessage(message);
    setTimeout(() => msg.dispose(), closeAfter);
    return msg;
  }
}
{'fraction_non_alphanumeric': 0.12291820191599116, 'fraction_numerical': 0.004421518054532056, 'mean_word_length': 3.1453879047037265, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5908102', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1931, 'n_words': 519}