context.ts 825 B

123456789101112131415161718192021222324252627
  1. import type {
  2. SessionFormatEvent,
  3. SessionFormatEventRun,
  4. SessionFormatMigrationContext,
  5. } from './types.ts'
  6. /** Migration output context that expands compact runs into retained events. */
  7. export class SessionFormatEventCollector implements SessionFormatMigrationContext {
  8. /** Events retained by this collector in source order. */
  9. readonly values: SessionFormatEvent[] = []
  10. /**
  11. * Retain one settled event.
  12. * @param event - settled event emitted by the upstream stage.
  13. */
  14. emitEvent(event: SessionFormatEvent): void {
  15. this.values.push(event)
  16. }
  17. /**
  18. * Expand one compact run directly into retained events.
  19. * @param run - compact event run emitted by the upstream stage.
  20. */
  21. emitRun(run: SessionFormatEventRun): void {
  22. for (const event of run.expand()) this.values.push(event)
  23. }
  24. }