Files
parcer/server/src/browser-management/classes/BrowserPool.ts

75 lines
2.3 KiB
TypeScript
Raw Normal View History

import { RemoteBrowser } from "./RemoteBrowser";
import logger from "../../logger";
2024-06-01 08:56:52 +05:30
/**
* @category Types
*/
interface BrowserPoolInfo {
2024-06-01 08:56:52 +05:30
/**
* The instance of remote browser.
*/
browser: RemoteBrowser,
2024-06-01 08:56:52 +05:30
/**
* States if the browser's instance is being actively used.
* Helps to persist the progress on the frontend when the application has been reloaded.
* @default false
*/
active: boolean,
}
2024-06-01 08:57:10 +05:30
/**
* Dictionary of all the active remote browser's instances indexed by their id.
* The value in this dictionary is of type BrowserPoolInfo,
* which provides additional information about the browser's usage.
* @category Types
*/
interface PoolDictionary {
[key: string]: BrowserPoolInfo,
}
2024-06-01 10:24:20 +05:30
/**
* A browser pool is a collection of remote browsers that are initialized and ready to be used.
* Adds the possibility to add, remove and retrieve remote browsers from the pool.
* It is possible to manage multiple browsers for creating or running a recording.
* @category BrowserManagement
*/
export class BrowserPool {
/**
* Holds all the instances of remote browsers.
*/
private pool : PoolDictionary = {};
2024-06-01 10:25:04 +05:30
/**
* Adds a remote browser instance to the pool indexed by the id.
* @param id remote browser instance's id
* @param browser remote browser instance
* @param active states if the browser's instance is being actively used
*/
public addRemoteBrowser = (id: string, browser: RemoteBrowser, active: boolean = false): void => {
this.pool = {
...this.pool,
[id]: {
browser,
active,
},
}
logger.log('debug', `Remote browser with id: ${id} added to the pool`);
};
2024-06-01 10:27:51 +05:30
/**
* Removes the remote browser instance from the pool.
* @param id remote browser instance's id
* @returns true if the browser was removed successfully, false otherwise
*/
public deleteRemoteBrowser = (id: string) : boolean => {
if (!this.pool[id]) {
logger.log('warn', `Remote browser with id: ${id} does not exist in the pool`);
return false;
}
delete(this.pool[id]);
logger.log('debug', `Remote browser with id: ${id} deleted from the pool`);
return true;
};
2024-06-01 10:24:20 +05:30
}