Skip to content

Commit

Permalink
docs(ScreenshotStream API): change isRecording to canCapture for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Lanza committed May 21, 2024
1 parent 7240790 commit 31a7795
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
23 changes: 13 additions & 10 deletions packages/core/src/provenance/screenshot-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ export function intitializeScreenshotStream(): ScreenshotStream {
}
}

/**
* False if the MediaStream has not been initialized via start(), or has been stopped.
* True if we have a MediaStream and can capture screenshots.
* @returns Whether a screenshot can be captured.
*/
function canCapture(): boolean {
return video !== null;
}

/**
* Captures a screenshot and stores it in the screenshots array.
* Also pushes the screenshot to the newScreenshotCallback if available.
Expand All @@ -118,7 +127,9 @@ export function intitializeScreenshotStream(): ScreenshotStream {
* @returns The captured screenshot.
*/
function capture(): ImageData {
if (!video) {
// We need the null check for ts, but canCapture() does that check already.
// We include canCapture() in case the implementation changes.
if (!canCapture() || !video) {
throw new Error('Recording not started');
}

Expand Down Expand Up @@ -197,14 +208,6 @@ export function intitializeScreenshotStream(): ScreenshotStream {
return [...screenshots];
}

/**
* Returns whether the screenshot stream is currently recording.
* @returns Whether the screenshot stream is currently recording.
*/
function isRecording(): boolean {
return video !== null;
}

/**
* Registers a listener to be called when a new screenshot is captured.
* @param listener - The listener to be called when a new screenshot is captured.
Expand All @@ -230,7 +233,7 @@ export function intitializeScreenshotStream(): ScreenshotStream {
getNth,
count,
getAll,
isRecording,
canCapture: canCapture,
registerScreenshotListener,
};
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/provenance/trrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export function initializeTrrack<State = any, Event extends string = string>({
});
}

if (screenshots.isRecording() && action.triggersScreenshot)
if (screenshots.canCapture() && action.triggersScreenshot)
screenshots.delayCapture(action.transitionTime);
},
async to(node: NodeId) {
Expand All @@ -327,7 +327,7 @@ export function initializeTrrack<State = any, Event extends string = string>({
for (let i = 0; i < path.length - 1; ++i) {
const currentNode = getNode(path[i]);

if (screenshots.isRecording()) {
if (screenshots.canCapture()) {
const action = registry.get(currentNode.event);
if (
action.triggersScreenshot &&
Expand Down Expand Up @@ -359,7 +359,7 @@ export function initializeTrrack<State = any, Event extends string = string>({

graph.update(graph.changeCurrent(node));

if (screenshots.isRecording() && maxTimer >= 0) {
if (screenshots.canCapture() && maxTimer >= 0) {
screenshots.delayCapture(maxTimer);
}

Expand Down Expand Up @@ -396,7 +396,7 @@ export function initializeTrrack<State = any, Event extends string = string>({
});
},
done() {
if (screenshots.isRecording()) screenshots.stop();
if (screenshots.canCapture()) screenshots.stop();
console.log('Setup later for URL sharing.');
},
tree() {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/provenance/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export interface ScreenshotStream {
*/
getAll(): ImageData[];
/**
* Returns whether the screenshot stream is currently recording.
* @returns whether capturing is allowed. Generally is false before start() is called and after stop() is called.
*/
isRecording(): boolean;
canCapture(): boolean;
/**
* Registers a listener to be called when a screenshot is captured.
* @param listener - The listener to register.
Expand Down

0 comments on commit 31a7795

Please sign in to comment.