summaryrefslogtreecommitdiff
path: root/apps/web/src/core/managers/diagnostics-manager.ts
blob: d022a350d38864d28c32404418b21acd786565c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { EditorCore } from "@/core";
import type { DiagnosticDefinition } from "@/lib/diagnostics/types";

interface DiagnosticRegistration extends DiagnosticDefinition {
	check: (editor: EditorCore) => boolean;
}

export class DiagnosticsManager {
	private readonly registrations: DiagnosticRegistration[] = [];
	private readonly listeners = new Set<() => void>();

	constructor(private editor: EditorCore) {}

	register(registration: DiagnosticRegistration): void {
		this.registrations.push(registration);
		this.notify();
	}

	getActive(options?: { scope?: string }): ReadonlyArray<DiagnosticDefinition> {
		const candidates =
			options?.scope !== undefined
				? this.registrations.filter((r) => r.scope === options.scope)
				: this.registrations;

		return candidates.filter((r) => r.check(this.editor));
	}

	subscribe(listener: () => void): () => void {
		this.listeners.add(listener);
		return () => this.listeners.delete(listener);
	}

	notify(): void {
		this.listeners.forEach((listener) => {
			listener();
		});
	}
}