-
Notifications
You must be signed in to change notification settings - Fork 3
Add lazy actors owned by parent state scopes #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "effect-machine": minor | ||
| --- | ||
|
|
||
| Add ActorHost for lazy actors owned by a parent state scope. Consumers share startup without owning cancellation or shutdown. The factory captures service dependencies, uses the first matching request input, and releases the actor on state exit or host service shutdown. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import { Deferred, Effect, Exit, Option, Scope, Stream, SubscriptionRef } from "effect"; | ||
|
|
||
| import type { ActorRef } from "./actor.js"; | ||
| import * as Machine from "./machine.js"; | ||
|
|
||
| import { ActorHostClosedError, ActorHostOccupiedError } from "./errors.js"; | ||
|
|
||
| export { ActorHostClosedError, ActorHostOccupiedError } from "./errors.js"; | ||
|
|
||
| export interface ActorHost<Input, S extends { readonly _tag: string }, E, Output, Failure> { | ||
| /** Register in the current scope. Wait for acquisition; interrupt if this generation closes. */ | ||
| readonly host: ( | ||
| input: Input, | ||
| ) => Effect.Effect< | ||
| ActorRef<S, E, Output>, | ||
| Failure | ActorHostClosedError | ActorHostOccupiedError, | ||
| Scope.Scope | ||
| >; | ||
| /** Wait for a matching generation. Caller cancellation does not cancel actor startup. */ | ||
| readonly acquire: ( | ||
| input: Input, | ||
| ) => Effect.Effect<ActorRef<S, E, Output>, Failure | ActorHostClosedError>; | ||
| } | ||
|
|
||
| /** | ||
| * A lazy actor whose lifetime belongs to its host scope, not its consumers. | ||
| * | ||
| * Call `host` from a machine's state-scoped spawn handler. Call `acquire` from | ||
| * consumers. The first matching consumer supplies the spawn input. Identity | ||
| * values match with Object.is. The factory captures the services at make time; | ||
| * its Scope and ActorScope always belong to the hosting generation. The actor | ||
| * starts before publication, including factories that use Machine.spawn. | ||
| */ | ||
| export const make = Effect.fn("effect-machine.actorHost.make")(function* < | ||
| Input, | ||
| S extends { readonly _tag: string }, | ||
| E, | ||
| Output, | ||
| Failure, | ||
| R, | ||
| >(options: { | ||
| readonly identity: (input: Input) => unknown; | ||
| readonly spawn: (input: Input) => Effect.Effect<ActorRef<S, E, Output>, Failure, R>; | ||
| }) { | ||
| type Actor = ActorRef<S, E, Output>; | ||
| interface Entry { | ||
| readonly identity: unknown; | ||
| readonly scope: Scope.Closeable; | ||
| readonly requested: Deferred.Deferred<Input>; | ||
| readonly actor: Deferred.Deferred<Actor, Failure | ActorHostClosedError>; | ||
| readonly closed: Deferred.Deferred<void>; | ||
| } | ||
| const services = yield* Effect.context<Exclude<R, Scope.Scope>>(); | ||
| const current = yield* SubscriptionRef.make(Option.none<Entry>()); | ||
| const closed = yield* Deferred.make<void>(); | ||
| const failClosed = Effect.fail(ActorHostClosedError.make({})); | ||
| yield* Effect.addFinalizer(() => | ||
| Effect.gen(function* () { | ||
| yield* Deferred.succeed(closed, undefined); | ||
| const entry = yield* SubscriptionRef.get(current); | ||
| if (Option.isSome(entry)) yield* Scope.close(entry.value.scope, Exit.void); | ||
| }), | ||
| ); | ||
|
|
||
| const awaitActor = (entry: Entry): Effect.Effect<Actor, Failure | ActorHostClosedError> => | ||
| Effect.raceFirst( | ||
| Deferred.await(entry.actor), | ||
| Deferred.await(entry.closed).pipe(Effect.andThen(failClosed)), | ||
| ).pipe( | ||
| Effect.flatMap((actor) => | ||
| Deferred.isDone(entry.closed).pipe( | ||
| Effect.flatMap((ended) => { | ||
| if (ended) return failClosed; | ||
| return Effect.succeed(actor); | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const host: ActorHost<Input, S, E, Output, Failure>["host"] = Effect.fn( | ||
| "effect-machine.actorHost.host", | ||
| )((input) => | ||
| Effect.uninterruptibleMask((restore) => | ||
| Effect.gen(function* () { | ||
| if (yield* Deferred.isDone(closed)) return yield* failClosed; | ||
| const generation = yield* Scope.fork(yield* Scope.Scope); | ||
| const entry: Entry = { | ||
| identity: options.identity(input), | ||
| scope: generation, | ||
| requested: yield* Deferred.make<Input>(), | ||
| actor: yield* Deferred.make<Actor, Failure | ActorHostClosedError>(), | ||
| closed: yield* Deferred.make<void>(), | ||
| }; | ||
| yield* Scope.addFinalizer( | ||
| generation, | ||
| SubscriptionRef.update(current, (value) => { | ||
| if (Option.isSome(value) && value.value === entry) return Option.none(); | ||
| return value; | ||
| }), | ||
| ); | ||
| const scope = yield* Scope.fork(generation); | ||
| // Publish closure before startup cancellation; retire the entry after actor cleanup. | ||
| yield* Scope.addFinalizer( | ||
| generation, | ||
| Deferred.succeed(entry.closed, undefined).pipe( | ||
| Effect.andThen(Deferred.fail(entry.actor, ActorHostClosedError.make({}))), | ||
| ), | ||
| ); | ||
| const registered = yield* SubscriptionRef.modify(current, (value) => { | ||
| if ( | ||
| Option.isSome(value) || | ||
| Deferred.isDoneUnsafe(closed) || | ||
| Deferred.isDoneUnsafe(entry.closed) | ||
| ) { | ||
| return [false, value]; | ||
| } | ||
| return [true, Option.some(entry)]; | ||
| }); | ||
| if (!registered) { | ||
| const ended = (yield* Deferred.isDone(closed)) || (yield* Deferred.isDone(entry.closed)); | ||
| yield* Scope.close(generation, Exit.void); | ||
| if (ended) return yield* failClosed; | ||
| return yield* ActorHostOccupiedError.make({}); | ||
| } | ||
| yield* Effect.forkIn( | ||
| Deferred.complete( | ||
| entry.actor, | ||
| Deferred.await(entry.requested).pipe( | ||
| Effect.flatMap((requested) => | ||
| Machine.scoped(options.spawn(requested).pipe(Effect.tap((actor) => actor.start))), | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
+ acquire(input)
+ select matching generation
+ supply the first request input
+ await shared actor
+ host startup fiber
+ Machine.scoped(factory(input))
+ actor.start
+ publish shared resultConsumer cancellation only ends that consumer's wait. The generation scope owns startup. Tests cover real recovery cancellation, direct Machine.spawn startup, and typed failures from a nested host. This trace comes from the source; the calldiff entry point was not resolved. |
||
| ), | ||
| Scope.provide(scope), | ||
| Effect.provideContext(services), | ||
| ), | ||
| ), | ||
| scope, | ||
| ); | ||
| return yield* restore( | ||
| awaitActor(entry).pipe( | ||
| Effect.catchTag("ActorHostClosedError", (error) => | ||
| Deferred.isDone(entry.closed).pipe( | ||
| Effect.flatMap((ownGenerationClosed) => { | ||
| if (ownGenerationClosed) return Effect.interrupt; | ||
| return Effect.fail(error); | ||
| }), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| }), | ||
| ), | ||
| ); | ||
| const acquire: ActorHost<Input, S, E, Output, Failure>["acquire"] = Effect.fn( | ||
| "effect-machine.actorHost.acquire", | ||
| )((input) => | ||
| Effect.raceFirst( | ||
| Effect.gen(function* () { | ||
| const identity = options.identity(input); | ||
| const entry = yield* SubscriptionRef.changes(current).pipe( | ||
| Stream.filter(Option.isSome), | ||
| Stream.map((value) => value.value), | ||
| Stream.filter((value) => Object.is(value.identity, identity)), | ||
| Stream.runHead, | ||
| Effect.flatMap(Option.match({ onNone: () => failClosed, onSome: Effect.succeed })), | ||
| ); | ||
| yield* Deferred.succeed(entry.requested, input); | ||
| return yield* awaitActor(entry); | ||
| }), | ||
| Deferred.await(closed).pipe(Effect.andThen(failClosed)), | ||
| ), | ||
| ); | ||
| return { host, acquire } satisfies ActorHost<Input, S, E, Output, Failure>; | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finalizers run in reverse registration order. The slow-cleanup test verifies that consumers fail closed and a new host remains rejected until cleanup ends. This trace comes from the source. calldiff could not resolve the Effect.fn generator entry point.