export class RequestQueue {
  private queue = {};

  /**
   * is the request already registered in this queue and resolvers are waiting its end
   * @param requestKey
   * @returns
   */
  public isRequestAlreadyFetched(requestKey): boolean {
    return requestKey in this.queue;
  }

  /**
   * Register a resolver callback for the request.
   * @param requestKey
   * @param callback
   */
  public registerToRequestQueue(requestKey, callback) {
    if (requestKey in this.queue) {
      this.queue[requestKey].push(callback);
    } else {
      this.queue[requestKey] = [callback];
    }
  }

  /**
   * return with the all registered resolver, what need to call at the end of the process
   * @param requestKey
   * @returns
   */
  public getRequestQueue(requestKey): Function[] {
    return this.queue[requestKey];
  }

  /**
   * clean the queue
   * @param requestKey
   */
  public emptyRequestQueue(requestKey) {
    delete this.queue[requestKey];
  }

  public reset(){
    this.queue = {};
  }
}
