New structure

This commit is contained in:
2025-11-11 12:59:42 +01:00
parent 088a3a024f
commit 89bc226d6b
46 changed files with 990 additions and 944 deletions

View File

@@ -0,0 +1,60 @@
/**
* Browser Factory Utility
* Eliminates code duplication between Desktop and Mobile flows
*
* Centralized browser instance creation and cleanup logic
*/
import type { BrowserContext } from 'rebrowser-playwright'
import type { MicrosoftRewardsBot } from '../../index'
import type { AccountProxy } from '../../interface/Account'
/**
* Create a browser instance for the given account
* IMPROVEMENT: Extracted from DesktopFlow and MobileFlow to eliminate duplication
*
* @param bot Bot instance
* @param proxy Account proxy configuration
* @param email Account email for session naming
* @returns Browser context ready to use
*
* @example
* const browser = await createBrowserInstance(bot, account.proxy, account.email)
*/
export async function createBrowserInstance(
bot: MicrosoftRewardsBot,
proxy: AccountProxy,
email: string
): Promise<BrowserContext> {
const browserModule = await import('../../browser/Browser')
const Browser = browserModule.default
const browserInstance = new Browser(bot)
return await browserInstance.createBrowser(proxy, email)
}
/**
* Safely close browser context with error handling
* IMPROVEMENT: Extracted from DesktopFlow and MobileFlow to eliminate duplication
*
* @param bot Bot instance
* @param browser Browser context to close
* @param email Account email for logging
* @param isMobile Whether this is a mobile browser context
*
* @example
* await closeBrowserSafely(bot, browser, account.email, false)
*/
export async function closeBrowserSafely(
bot: MicrosoftRewardsBot,
browser: BrowserContext,
email: string,
isMobile: boolean
): Promise<void> {
try {
await bot.browser.func.closeBrowser(browser, email)
} catch (closeError) {
const message = closeError instanceof Error ? closeError.message : String(closeError)
const platform = isMobile ? 'mobile' : 'desktop'
bot.log(isMobile, `${platform.toUpperCase()}-FLOW`, `Failed to close ${platform} context: ${message}`, 'warn')
}
}