tuil
ReferencePackages@mwillbanks/tuil-devtoolsAPI

DevtoolsStore

class exported by @mwillbanks/tuil-devtools.

View rawEdit

class

Public class exported by @mwillbanks/tuil-devtools.

export class DevtoolsStore {
  readonly #runtime: TuilRuntime;
  readonly #events: ObservedEvent[] = [];
  readonly #observers = new Set<() => void>();
  readonly #maxEvents: number;
  readonly #disposeObservers: readonly (() => void)[];
  #version = 0;
  #disposed = false;

  constructor(
    runtime: TuilRuntime,
    options: { readonly maxEvents?: number } = {},
  ) {
    this.#runtime = runtime;
    this.#maxEvents = options.maxEvents ?? 200;
    if (!Number.isSafeInteger(this.#maxEvents) || this.#maxEvents < 1) {
      throw new Error("Devtools maxEvents must be a positive integer");
    }
    this.#events.push(...runtime.events.history().slice(-this.#maxEvents));
    const commandObserver = runtime.commands.observeRegistry(() =>
      this.#notify(),
    );
    this.#disposeObservers = [
      runtime.events.observe((event) => {
        this.#events.push(event);
        if (this.#events.length > this.#maxEvents) this.#events.shift();
        this.#notify();
      }),
      () => {
        void commandObserver.dispose();
      },
      runtime.focus.observe(() => this.#notify()),
      runtime.hotkeys.observe(() => this.#notify()),
      runtime.editorSessions.observe(() => this.#notify()),
      runtime.logPipelines.observe(() => this.#notify()),
      runtime.streamingPipelines.observe(() => this.#notify()),
      runtime.extensions.devtoolsPanels.observe(() => this.#notify()),
    ];
  }

  subscribe = (observer: () => void): (() => void) => {
    this.#observers.add(observer);
    return () => this.#observers.delete(observer);
  };

  version = (): number => this.#version;

  inspect(panel: DevtoolsPanel): RuntimeInspection {
    return inspectRuntime(this.#runtime, panel, this.#events);
  }

  refresh(): void {
    this.#notify();
  }

  dispose(): void {
    if (this.#disposed) return;
    this.#disposed = true;
    for (const dispose of this.#disposeObservers) dispose();
    this.#observers.clear();
  }

  #notify(): void {
    if (this.#disposed) return;
    this.#version += 1;
    for (const observer of this.#observers) observer();
  }
}

Members

MemberTypeRequiredDescriptionRelated types
#runtimeTuilRuntime<import("@mwillbanks/tuil-events").EventMap, import("@mwillbanks/tuil").TuilExtensionPoints>YesThe #runtime member uses the TuilRuntime<import("@mwillbanks/tuil-events").EventMap, import("@mwillbanks/tuil").TuilExtensionPoints> contract.EventMap, TuilExtensionPoints, TuilRuntime
#eventsObservedEvent[]YesThe #events member uses the ObservedEvent[] contract.ObservedEvent
#observersSet<() => void>YesThe #observers member uses the Set<() => void> contract.
#maxEventsnumberYesThe #maxEvents member uses the number contract.
#disposeObserversreadonly (() => void)[]YesThe #disposeObservers member uses the readonly (() => void)[] contract.
#versionnumberYesThe #version member uses the number contract.
#disposedbooleanYesThe #disposed member uses the boolean contract.
__constructoranyYesThe __constructor member uses the any contract.
subscribe(observer: () => void) => (() => void)YesThe subscribe member uses the (observer: () => void) => (() => void) contract.
version() => numberYesThe version member uses the () => number contract.
inspect(panel: DevtoolsPanel) => RuntimeInspectionYesThe inspect member uses the (panel: DevtoolsPanel) => RuntimeInspection contract.DevtoolsPanel, RuntimeInspection
refresh() => voidYesThe refresh member uses the () => void contract.
dispose() => voidYesThe dispose member uses the () => void contract.
#notify() => voidYesThe #notify member uses the () => void contract.

Parameters

This declaration has no public members.

Returns

This declaration does not return a value.

Throws

No thrown errors are documented for this declaration.

Source

View the secondary source reference

Package

@mwillbanks/tuil-devtools

On this page