1- // @effect -diagnostics nodeBuiltinImport:off - Owns Playwright resources outside the Effect runtime.
1+ // @effect -diagnostics nodeBuiltinImport:off globalTimers:off - Owns Playwright resources outside the Effect runtime.
22import { INCOGNITO_BROWSER_PROFILE_ID } from "@t3tools/contracts" ;
33import { constVoid } from "effect/Function" ;
44import * as NodeFSP from "node:fs/promises" ;
@@ -26,6 +26,8 @@ interface Options {
2626 ) => Promise < { readonly path : string ; readonly release : ( ) => Promise < void > } | undefined > ;
2727 readonly diagnose ?: ( executable : string , cause : unknown ) => Promise < unknown > ;
2828 readonly env ?: NodeJS . ProcessEnv ;
29+ /** Closes the shared browser once it has held no context for this long. */
30+ readonly idleBrowserMs ?: number ;
2931 readonly onContextClose ?: ( context : BrowserContext ) => void ;
3032}
3133
@@ -80,6 +82,8 @@ export class ServerBrowserContexts {
8082 private readonly clearing = new Map < string , Promise < void > > ( ) ;
8183 private browser : Promise < Browser > | undefined ;
8284 private closing : Promise < void > | undefined ;
85+ private idleTimer : ReturnType < typeof setTimeout > | undefined ;
86+ private pendingSharedContexts = 0 ;
8387
8488 constructor ( options : Options ) {
8589 this . options = options ;
@@ -136,6 +140,37 @@ export class ServerBrowserContexts {
136140 return this . browser ;
137141 }
138142
143+ private async sharedContext ( options ?: Parameters < Browser [ "newContext" ] > [ 0 ] ) {
144+ clearTimeout ( this . idleTimer ) ;
145+ this . pendingSharedContexts ++ ;
146+ const launched = this . sharedBrowser ( ) ;
147+ try {
148+ const context = await ( await launched ) . newContext ( options ) ;
149+ context . on ( "close" , ( ) => this . retireWhenIdle ( launched ) ) ;
150+ return context ;
151+ } finally {
152+ this . pendingSharedContexts -- ;
153+ this . retireWhenIdle ( launched ) ;
154+ }
155+ }
156+
157+ private retireWhenIdle ( launched : Promise < Browser > ) {
158+ const delay = this . options . idleBrowserMs ;
159+ if ( delay === undefined || this . closing || this . pendingSharedContexts > 0 ) return ;
160+ clearTimeout ( this . idleTimer ) ;
161+ this . idleTimer = setTimeout ( ( ) => void this . retire ( launched ) , delay ) ;
162+ this . idleTimer . unref ( ) ;
163+ }
164+
165+ private async retire ( launched : Promise < Browser > ) {
166+ const browser = await launched . catch ( ( ) => undefined ) ;
167+ if ( ! browser || this . browser !== launched || this . pendingSharedContexts > 0 ) return ;
168+ if ( browser . contexts ( ) . length > 0 ) return ;
169+ // Cleared first, so a request during the close launches a fresh browser.
170+ this . browser = undefined ;
171+ await browser . close ( ) . catch ( constVoid ) ;
172+ }
173+
139174 contextFor ( profileId : string , isolationKey ?: string ) : Promise < BrowserContext > {
140175 if ( this . closing ) return Promise . reject ( new Error ( "The preview browser is closed." ) ) ;
141176 const key = JSON . stringify ( [ profileId , isolationKey ?? null ] ) ;
@@ -166,8 +201,7 @@ export class ServerBrowserContexts {
166201 private async createContext ( profileId : string , isolationKey ?: string ) {
167202 const contextOptions = { viewport : { width : 1280 , height : 800 } , deviceScaleFactor : 2 } ;
168203 if ( isolationKey !== undefined || profileId === INCOGNITO_BROWSER_PROFILE_ID ) {
169- const browser = await this . sharedBrowser ( ) ;
170- return browser . newContext ( contextOptions ) ;
204+ return this . sharedContext ( contextOptions ) ;
171205 }
172206 const directory = this . profileDirectory ( profileId ) ;
173207 await NodeFSP . mkdir ( directory , { recursive : true } ) ;
@@ -192,7 +226,7 @@ export class ServerBrowserContexts {
192226
193227 async scratchPage ( ) {
194228 if ( this . closing ) throw new Error ( "The preview browser is closed." ) ;
195- const context = await ( await this . sharedBrowser ( ) ) . newContext ( ) ;
229+ const context = await this . sharedContext ( ) ;
196230 const page = await context . newPage ( ) ;
197231 page . once ( "close" , ( ) => void context . close ( ) . catch ( constVoid ) ) ;
198232 return page ;
@@ -237,6 +271,7 @@ export class ServerBrowserContexts {
237271 }
238272
239273 private async dispose ( ) {
274+ clearTimeout ( this . idleTimer ) ;
240275 await Promise . allSettled (
241276 [ ...this . contexts . values ( ) ] . map ( async ( pending ) => ( await pending ) . close ( ) ) ,
242277 ) ;
0 commit comments