summaryrefslogtreecommitdiff
path: root/apps/web/src/commands/preview-tracker.ts
blob: 68c8dee37aa039bb6b3a41151ce718de5a7e5025 (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
export class PreviewTracker<T> {
	private snapshot: T | null = null;

	begin({ state }: { state: T }): void {
		if (this.snapshot === null) {
			this.snapshot = structuredClone(state);
		}
	}

	isActive(): boolean {
		return this.snapshot !== null;
	}

	getSnapshot(): T | null {
		return this.snapshot;
	}

	end(): T | null {
		const snapshot = this.snapshot;
		this.snapshot = null;
		return snapshot;
	}
}