@@ -71,18 +71,19 @@ export interface PlaywrightService {
7171 /**
7272 * Connects to a browser instance via Chrome DevTools Protocol (CDP).
7373 *
74- * Unlike {@link launchScoped }, this method does **not** close the browser when the
75- * scope is closed. It is the caller's responsibility to manage the browser 's
74+ * Unlike {@link connectCDPScoped }, this method does **not** close the connection when the
75+ * scope is closed. It is the caller's responsibility to manage the connection 's
7676 * lifecycle.
7777 *
78- * If you want to close the browser using a scope simply add a finalizer:
78+ * If you want to close the connection using a scope simply add a finalizer:
7979 *
8080 * ```ts
8181 * import { Effect } from "effect";
8282 * import { Playwright } from "effect-playwright";
8383 *
8484 * const program = Effect.gen(function* () {
85- * const browser = yield* Playwright.connectCDP(cdpUrl);
85+ * const playwright = yield* Playwright;
86+ * const browser = yield* playwright.connectCDP(cdpUrl);
8687 * yield* Effect.addFinalizer(() => browser.close.pipe(Effect.ignore));
8788 * });
8889 *
@@ -97,6 +98,39 @@ export interface PlaywrightService {
9798 cdpUrl : string ,
9899 options ?: ConnectOverCDPOptions ,
99100 ) => Effect . Effect < typeof PlaywrightBrowser . Service , PlaywrightError > ;
101+ /**
102+ * Connects to a browser instance via Chrome DevTools Protocol (CDP) managed by a Scope.
103+ *
104+ * This method automatically closes the connection when the scope is closed.
105+ *
106+ * Note that closing a CDP connection does **not** close the browser instance itself,
107+ * only the CDP connection.
108+ *
109+ * ```ts
110+ * import { Effect } from "effect";
111+ * import { Playwright } from "effect-playwright";
112+ *
113+ * const program = Effect.gen(function* () {
114+ * const playwright = yield* Playwright;
115+ * const browser = yield* playwright.connectCDPScoped(cdpUrl);
116+ * // Connection will be closed automatically when scope closes
117+ * });
118+ *
119+ * await Effect.runPromise(program);
120+ * ```
121+ *
122+ * @param cdpUrl - The CDP URL to connect to.
123+ * @param options - Optional options for connecting to the CDP URL.
124+ * @since 0.1.1
125+ */
126+ connectCDPScoped : (
127+ cdpUrl : string ,
128+ options ?: ConnectOverCDPOptions ,
129+ ) => Effect . Effect <
130+ typeof PlaywrightBrowser . Service ,
131+ PlaywrightError ,
132+ Scope . Scope
133+ > ;
100134}
101135
102136const launch : (
@@ -114,6 +148,19 @@ const launch: (
114148 return browser ;
115149 } ) ;
116150
151+ const connectCDP : (
152+ cdpUrl : string ,
153+ options ?: ConnectOverCDPOptions ,
154+ ) => Effect . Effect < typeof PlaywrightBrowser . Service , PlaywrightError > =
155+ Effect . fn ( function * ( cdpUrl : string , options ?: ConnectOverCDPOptions ) {
156+ const browser = yield * Effect . tryPromise ( {
157+ try : ( ) => chromium . connectOverCDP ( cdpUrl , options ) ,
158+ catch : wrapError ,
159+ } ) ;
160+
161+ return PlaywrightBrowser . make ( browser ) ;
162+ } ) ;
163+
117164export class Playwright extends Context . Tag (
118165 "effect-playwright/index/Playwright" ,
119166) < Playwright , PlaywrightService > ( ) {
@@ -122,26 +169,14 @@ export class Playwright extends Context.Tag(
122169 */
123170 static readonly layer = Layer . succeed ( Playwright , {
124171 launch,
125- launchScoped : Effect . fn ( function * (
126- browserType : BrowserType ,
127- options ?: LaunchOptions ,
128- ) {
129- const browser = yield * launch ( browserType , options ) ;
130-
131- // cleanup
132- yield * Effect . addFinalizer ( ( ) => browser . close . pipe ( Effect . ignore ) ) ;
133- return browser ;
134- } ) ,
135- connectCDP : Effect . fn ( function * (
136- cdpUrl : string ,
137- options ?: ConnectOverCDPOptions ,
138- ) {
139- const browser = yield * Effect . tryPromise ( {
140- try : ( ) => chromium . connectOverCDP ( cdpUrl , options ) ,
141- catch : wrapError ,
142- } ) ;
143-
144- return PlaywrightBrowser . make ( browser ) ;
145- } ) ,
172+ launchScoped : ( browserType , options ) =>
173+ Effect . acquireRelease ( launch ( browserType , options ) , ( browser ) =>
174+ browser . close . pipe ( Effect . ignore ) ,
175+ ) ,
176+ connectCDP,
177+ connectCDPScoped : ( cdpUrl , options ) =>
178+ Effect . acquireRelease ( connectCDP ( cdpUrl , options ) , ( browser ) =>
179+ browser . close . pipe ( Effect . ignore ) ,
180+ ) ,
146181 } ) ;
147182}
0 commit comments