Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/event-stream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { layer } from "@effect/vitest";
import { Effect, Stream } from "effect";
import { chromium } from "playwright-core";
import { PlaywrightBrowser } from "./browser";
import { PlaywrightEnvironment } from "./experimental";

layer(PlaywrightEnvironment.layer(chromium))("eventStream", (it) => {
it.scoped("should complete when the page closes", () =>
Effect.gen(function* () {
const browser = yield* PlaywrightBrowser;
const page = yield* browser.newPage();

// Subscribe to an event stream
const stream = page.eventStream("console");

// Run the stream in the background
const fiber = yield* Stream.runCollect(stream).pipe(Effect.fork);

// Close the page
yield* page.close;

// Wait for the stream to complete
yield* fiber.await;

// test will timeout if the stream does not complete
}).pipe(PlaywrightEnvironment.withBrowser),
);

it.scoped("should complete when the browser closes", () =>
Effect.gen(function* () {
const browser = yield* PlaywrightBrowser;
const page = yield* browser.newPage();

// Subscribe to an event stream
const stream = page.eventStream("console");

// Run the stream in the background
const fiber = yield* Stream.runCollect(stream).pipe(Effect.fork);

// Close the browser
yield* browser.close;

// Wait for the stream to complete
yield* fiber.await;

// test will timeout if the stream does not complete
}).pipe(PlaywrightEnvironment.withBrowser),
);
});
10 changes: 8 additions & 2 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,17 @@ export class PlaywrightPage extends Context.Tag(
Effect.acquireRelease(
Effect.sync(() => {
const callback = emit.single;
const closeCallback = emit.end;
page.on(event, callback);
page.once("close", closeCallback);

return callback;
return { callback, closeCallback };
}),
(callback) => Effect.sync(() => page.off(event, callback)),
({ callback, closeCallback }) =>
Effect.sync(() => {
page.off(event, callback);
page.off("close", closeCallback);
}),
),
).pipe(
Stream.map((e) => {
Expand Down