From 73da3ae62932b9b09189aee2f1e59d827662dd7b Mon Sep 17 00:00:00 2001 From: Benjamin Kane Date: Thu, 12 Dec 2024 18:14:41 -0500 Subject: [PATCH] use buffers for hasFrame (#5264) --- app/packages/looker/src/lookers/utils.test.ts | 19 +++++++++++++++++++ app/packages/looker/src/lookers/utils.ts | 7 +++++++ app/packages/looker/src/lookers/video.ts | 9 ++------- 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 app/packages/looker/src/lookers/utils.test.ts create mode 100644 app/packages/looker/src/lookers/utils.ts diff --git a/app/packages/looker/src/lookers/utils.test.ts b/app/packages/looker/src/lookers/utils.test.ts new file mode 100644 index 0000000000..6c0d307e6e --- /dev/null +++ b/app/packages/looker/src/lookers/utils.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from "vitest"; +import type { Buffers } from "../state"; +import { hasFrame } from "./utils"; + +describe("looker utilities", () => { + it("determines frame availability given a buffer list", () => { + const BUFFERS: Buffers = [ + [1, 3], + [5, 25], + ]; + for (const frameNumber of [1, 10, 25]) { + expect(hasFrame(BUFFERS, frameNumber)).toBe(true); + } + + for (const frameNumber of [0, 4, 26]) { + expect(hasFrame(BUFFERS, frameNumber)).toBe(false); + } + }); +}); diff --git a/app/packages/looker/src/lookers/utils.ts b/app/packages/looker/src/lookers/utils.ts new file mode 100644 index 0000000000..ea645401f0 --- /dev/null +++ b/app/packages/looker/src/lookers/utils.ts @@ -0,0 +1,7 @@ +import type { Buffers } from "../state"; + +export const hasFrame = (buffers: Buffers, frameNumber: number) => { + return buffers.some( + ([start, end]) => start <= frameNumber && frameNumber <= end + ); +}; diff --git a/app/packages/looker/src/lookers/video.ts b/app/packages/looker/src/lookers/video.ts index 24ab04feb0..2fe24f7fab 100644 --- a/app/packages/looker/src/lookers/video.ts +++ b/app/packages/looker/src/lookers/video.ts @@ -19,6 +19,7 @@ import { addToBuffers, removeFromBuffers } from "../util"; import { AbstractLooker } from "./abstract"; import { type Frame, acquireReader, clearReader } from "./frame-reader"; import { LookerUtils, withFrames } from "./shared"; +import { hasFrame } from "./utils"; let LOOKER_WITH_READER: VideoLooker | null = null; @@ -394,13 +395,7 @@ export class VideoLooker extends AbstractLooker { } private hasFrame(frameNumber: number) { - if (frameNumber === this.firstFrameNumber) { - return this.firstFrame; - } - return ( - this.frames.has(frameNumber) && - this.frames.get(frameNumber)?.deref() !== undefined - ); + return hasFrame(this.state.buffers, frameNumber); } private getFrame(frameNumber: number) {