address comments
This commit is contained in:
+13
-3
@@ -84,6 +84,7 @@ export class BrowserManager {
|
||||
private screencastActive: boolean = false;
|
||||
private screencastSessionId: number = 0;
|
||||
private frameCallback: ((frame: ScreencastFrame) => void) | null = null;
|
||||
private screencastFrameHandler: ((params: any) => void) | null = null;
|
||||
|
||||
/**
|
||||
* Check if browser is launched
|
||||
@@ -952,8 +953,8 @@ export class BrowserManager {
|
||||
this.frameCallback = callback;
|
||||
this.screencastActive = true;
|
||||
|
||||
// Listen for screencast frames
|
||||
cdp.on('Page.screencastFrame', async (params: any) => {
|
||||
// Create and store the frame handler so we can remove it later
|
||||
this.screencastFrameHandler = async (params: any) => {
|
||||
const frame: ScreencastFrame = {
|
||||
data: params.data,
|
||||
metadata: params.metadata,
|
||||
@@ -967,7 +968,10 @@ export class BrowserManager {
|
||||
if (this.frameCallback) {
|
||||
this.frameCallback(frame);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Listen for screencast frames
|
||||
cdp.on('Page.screencastFrame', this.screencastFrameHandler);
|
||||
|
||||
// Start the screencast
|
||||
await cdp.send('Page.startScreencast', {
|
||||
@@ -990,12 +994,18 @@ export class BrowserManager {
|
||||
try {
|
||||
const cdp = await this.getCDPSession();
|
||||
await cdp.send('Page.stopScreencast');
|
||||
|
||||
// Remove the event listener to prevent accumulation
|
||||
if (this.screencastFrameHandler) {
|
||||
cdp.off('Page.screencastFrame', this.screencastFrameHandler);
|
||||
}
|
||||
} catch {
|
||||
// Ignore errors when stopping
|
||||
}
|
||||
|
||||
this.screencastActive = false;
|
||||
this.frameCallback = null;
|
||||
this.screencastFrameHandler = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -304,8 +304,11 @@ export class StreamServer {
|
||||
* Start screencasting
|
||||
*/
|
||||
private async startScreencast(): Promise<void> {
|
||||
// Set flag immediately to prevent race conditions with concurrent calls
|
||||
if (this.isScreencasting) return;
|
||||
this.isScreencasting = true;
|
||||
|
||||
try {
|
||||
// Check if browser is launched
|
||||
if (!this.browser.isLaunched()) {
|
||||
throw new Error('Browser not launched');
|
||||
@@ -319,12 +322,15 @@ export class StreamServer {
|
||||
everyNthFrame: 1,
|
||||
});
|
||||
|
||||
this.isScreencasting = true;
|
||||
|
||||
// Notify all clients
|
||||
for (const client of this.clients) {
|
||||
this.sendStatus(client);
|
||||
}
|
||||
} catch (error) {
|
||||
// Reset flag on failure so caller can retry
|
||||
this.isScreencasting = false;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user