Skip to content

Commit

Permalink
fix(screenshot-stream.ts): capture initial state & handle when no scr…
Browse files Browse the repository at this point in the history
…eenshots available

Also updates react-trrack-example to match
  • Loading branch information
Nate Lanza committed May 13, 2024
1 parent 5394e31 commit c00bbff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 5 additions & 1 deletion apps/react-trrack-example/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ function App() {
return (
<Box sx={{ height: '100vh', width: '100vw' }}>
<Navbar t={trrackManager} />
<Button onClick={() => downloadScreenshot(ss.getNth(0), 'screenshot')}>
<Button
onClick={() =>
ss.getNth(0) ? downloadScreenshot(ss.getNth(0)!, 'screenshot') : null
}
>
Download Latest Screenshot
</Button>
<Button
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/provenance/screenshot-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export function intitializeScreenshotStream(
// I honestly don't know how we'd get here
throw new Error('Unable to start recording; no stream available');
}

// We should capture initial state
capture();
}

/**
Expand Down Expand Up @@ -169,7 +172,11 @@ export function intitializeScreenshotStream(
* @param n - The index of the screenshot to retrieve. 0 is the most recent.
* @returns The nth screenshot.
*/
function getNth(n: number): ImageData {
function getNth(n: number): ImageData | null {
if (screenshots.length === 0) {
return null;
}

if (n < 0 || n >= screenshots.length) {
throw new Error(`Screenshot index out of bounds: ${n}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/provenance/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface ScreenshotStream {
capture(): ImageData;
delayCapture(timeout: number): void;
stop(): void;
getNth(n: number): ImageData;
getNth(n: number): ImageData | null;
count(): number;
getAll(): ImageData[];
isRecording(): boolean;
Expand Down

0 comments on commit c00bbff

Please sign in to comment.