// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @interface
 */
/* eslint-disable rulesdir/no_underscored_properties */

export interface Runnable {
  run(): Promise<void>;
}

type LateInitializationLoader = () => Promise<Runnable>;
export interface LateInitializableRunnableSetting {
  id: string;
  loadRunnable: LateInitializationLoader;
}

const registeredLateInitializationRunnables = new Map<string, LateInitializationLoader>();

export function registerLateInitializationRunnable(setting: LateInitializableRunnableSetting): void {
  const {id, loadRunnable} = setting;
  if (registeredLateInitializationRunnables.has(id)) {
    throw new Error(`Duplicate late Initializable runnable id '${id}'`);
  }
  registeredLateInitializationRunnables.set(id, loadRunnable);
}

export function lateInitializationRunnables(): Array<LateInitializationLoader> {
  return [...registeredLateInitializationRunnables.values()];
}

const registeredEarlyInitializationRunnables: (() => Runnable)[] = [];

export function registerEarlyInitializationRunnable(runnable: () => Runnable): void {
  registeredEarlyInitializationRunnables.push(runnable);
}

export function earlyInitializationRunnables(): (() => Runnable)[] {
  return registeredEarlyInitializationRunnables;
}

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @interface
 */
/* eslint-disable rulesdir/no_underscored_properties */

export interface Runnable {
  run(): Promise<void>;
}

type LateInitializationLoader = () => Promise<Runnable>;
export interface LateInitializableRunnableSetting {
  id: string;
  loadRunnable: LateInitializationLoader;
}

const registeredLateInitializationRunnables = new Map<string, LateInitializationLoader>();

export function registerLateInitializationRunnable(setting: LateInitializableRunnableSetting): void {
  const {id, loadRunnable} = setting;
  if (registeredLateInitializationRunnables.has(id)) {
    throw new Error(`Duplicate late Initializable runnable id '${id}'`);
  }
  registeredLateInitializationRunnables.set(id, loadRunnable);
}

export function maybeRemoveLateInitializationRunnable(runnableId: string): boolean {
  return registeredLateInitializationRunnables.delete(runnableId);
}

export function lateInitializationRunnables(): Array<LateInitializationLoader> {
  return [...registeredLateInitializationRunnables.values()];
}

const registeredEarlyInitializationRunnables: (() => Runnable)[] = [];

export function registerEarlyInitializationRunnable(runnable: () => Runnable): void {
  registeredEarlyInitializationRunnables.push(runnable);
}

export function earlyInitializationRunnables(): (() => Runnable)[] {
  return registeredEarlyInitializationRunnables;
}

Add functionality to remove registered late initialization runnables
Add functionality to remove registered late initialization runnables

Bug: none
Change-Id: Ie44ba44c8845e050c402ebed5642908bb09eb5e8
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3020890
Reviewed-by: Tim van der Lippe <dba8716ee7f8d16236046f74d2167cb94410f6ed@chromium.org>
Commit-Queue: Andres Olivares <9d3bededfc9b98cb289557338a8f98e77d597607@chromium.org>
