Skip to content

Commit

Permalink
Dispose fiber on stream controller
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyfran committed Sep 9, 2024
1 parent 3080917 commit 4ea8edc
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
getOrCreateRuntime,
type EchoRuntimeServices,
} from "@echo/services-bootstrap-runtime";
import { Effect, Stream, type SubscriptionRef } from "effect";
import { Effect, Fiber, Stream, type SubscriptionRef } from "effect";
import type { ReactiveController, ReactiveControllerHost } from "lit";

/**
Expand Down Expand Up @@ -54,6 +54,8 @@ type OutputEffect<A, E> = Effect.Effect<
*/
export class StreamConsumer<A, E> implements ReactiveController {
private host: ReactiveControllerHost;

private _fiber: Fiber.RuntimeFiber<void, E> | undefined;
private _status: StreamStatus<A, E> = { _tag: "Initial" };

constructor(
Expand Down Expand Up @@ -86,7 +88,14 @@ export class StreamConsumer<A, E> implements ReactiveController {
}),
);

getOrCreateRuntime().runPromise(consumer$);
this._fiber = getOrCreateRuntime().runFork(consumer$);
}

hostDisconnected(): void {
if (this._fiber) {
getOrCreateRuntime().runFork(Fiber.interrupt(this._fiber));
this._fiber = undefined;
}
}

/**
Expand Down

0 comments on commit 4ea8edc

Please sign in to comment.