/**
 *  An Observer is a class that has a listener method that accepts a value of type T
 *  and calls the listener method with a value of type T
 */
class Observer {
  /**
   * It accepts a function that accepts a value and returns a boolean, and a function that accepts a value of the type that
   * the first function accepts
   *
   * @typedef T generic type parameter
   * @param {(input: unknown) => input is T} accept - A function that indicate if the input is acceptable by the observer
   * @param {(input: T) => void} listener - A function handle the input for the observer if the input is accepted
   */
  constructor(accept, listener) {
    this.accept = accept;
    this.listener = listener;
  }
  /**
   * If the input is of the type that this handler is interested in, then call the listener function
   *
   * @param {unknown} input - unknown
   */
  notify(input) {
    if (this.accept(input)) {
      this.listener(input);
    }
  }
}

export { Observer as O };
