Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

2.4 KB
Raw Blame
import { logger } from '@mimik/core/logger';
import type { CaptureStateUpdate } from '@/core/capture/capture-state-update';
import { answerChildFrames } from '@/core/capture/dom/frame-placement';
import { CaptureState } from '@/core/capture/machine';
import { sendMessage } from '@/lib/messaging';
import { type CaptureHandle, startCapture } from './events/handlers';

const FRAME_ANSWER_GRACE_MS = 2000;

export class CaptureSession {
  private capture: CaptureHandle | null = null;
  private activeGuideId: string | null = null;
  private disabled = false;
  private frameAnswersTeardown: (() => void) | null = null;
  private frameAnswerGrace: ReturnType<typeof setTimeout> | undefined;

  constructor(private readonly onSynced?: (state: CaptureStateUpdate) => void) {
    this.syncWithBackground();
  }

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

  get isDisabled(): boolean {
    return this.disabled;
  }

  get guideId(): string | null {
    return this.activeGuideId;
  }

  start(guideId: string): void {
    if (this.disabled) return;
    if (this.isActive) {
      this.stop();
    }

    logger.info('Capture started β†’ guideId:', guideId);
    this.activeGuideId = guideId;
    clearTimeout(this.frameAnswerGrace);
    this.frameAnswersTeardown ??= answerChildFrames();
    const isTopFrame = window.self === window.top;
    this.capture = startCapture(guideId, isTopFrame);
  }

  stop(): Promise<void> {
    if (!this.isActive) return Promise.resolve();

    logger.info('Capture stopped β†’ guideId:', this.activeGuideId);
    const draining = this.capture?.stop() ?? Promise.resolve();
    this.capture = null;
    this.activeGuideId = null;
    this.frameAnswerGrace = setTimeout(() => this.stopAnsweringFrames(), FRAME_ANSWER_GRACE_MS);
    return draining;
  }

  private syncWithBackground(): void {
    sendMessage('getState', undefined)
      .then((res) => {
        if (this.disabled) return;
        if (res.state === CaptureState.RECORDING && res.currentGuideId) {
          this.start(res.currentGuideId);
        }
        this.onSynced?.(res);
      })
      .catch(() => {});
  }

  private stopAnsweringFrames(): void {
    this.frameAnswersTeardown?.();
    this.frameAnswersTeardown = null;
  }

  dispose(): void {
    this.stop();
    clearTimeout(this.frameAnswerGrace);
    this.stopAnsweringFrames();
    this.disabled = true;
    logger.debug('Capture session disposed');
  }
}