|
| 1 | +/** |
| 2 | + * @since 1.0.0 |
| 3 | + */ |
| 4 | +import * as Duration from "effect/Duration" |
| 5 | +import * as Effect from "effect/Effect" |
| 6 | +import { identity } from "effect/Function" |
| 7 | +import * as RcRef from "effect/RcRef" |
| 8 | +import * as Scope from "effect/Scope" |
| 9 | +import * as Entity from "./Entity.js" |
| 10 | +import type { Sharding } from "./Sharding.js" |
| 11 | + |
| 12 | +/** |
| 13 | + * @since 1.0.0 |
| 14 | + * @category Type ids |
| 15 | + */ |
| 16 | +export const TypeId: TypeId = "~@effect/cluster/EntityResource" |
| 17 | + |
| 18 | +/** |
| 19 | + * @since 1.0.0 |
| 20 | + * @category Type ids |
| 21 | + */ |
| 22 | +export type TypeId = "~@effect/cluster/EntityResource" |
| 23 | + |
| 24 | +/** |
| 25 | + * @since 1.0.0 |
| 26 | + * @category Models |
| 27 | + */ |
| 28 | +export interface EntityResource<out A, out E = never> { |
| 29 | + readonly [TypeId]: TypeId |
| 30 | + readonly get: Effect.Effect<A, E, Scope.Scope> |
| 31 | + readonly close: Effect.Effect<void> |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A `EntityResource` is a resource that can be acquired inside a cluster |
| 36 | + * entity, which will keep the entity alive even across restarts. |
| 37 | + * |
| 38 | + * The resource will only be fully released when the idle time to live is |
| 39 | + * reached, or when the `close` effect is called. |
| 40 | + * |
| 41 | + * By default, the `idleTimeToLive` is infinite, meaning the resource will only |
| 42 | + * be released when `close` is called. |
| 43 | + * |
| 44 | + * @since 1.0.0 |
| 45 | + * @category Constructors |
| 46 | + */ |
| 47 | +export const make: <A, E, R>(options: { |
| 48 | + readonly acquire: Effect.Effect<A, E, R> |
| 49 | + readonly idleTimeToLive?: Duration.DurationInput | undefined |
| 50 | + /** |
| 51 | + * When to close the resource Scope. |
| 52 | + * |
| 53 | + * If set to "explicit", the resource will only be cleaned up when either the |
| 54 | + * `idleTimeToLive` is reached, or the .close effect is called. |
| 55 | + * |
| 56 | + * Defaults to "always", which means the resource will be cleaned up when the |
| 57 | + * the parent Scope is closed. |
| 58 | + */ |
| 59 | + readonly shutdownMode?: "explicit" | "always" | undefined |
| 60 | +}) => Effect.Effect< |
| 61 | + EntityResource<A, E>, |
| 62 | + E, |
| 63 | + Scope.Scope | R | Sharding | Entity.CurrentAddress |
| 64 | +> = Effect.fnUntraced(function*<A, E, R>(options: { |
| 65 | + readonly acquire: Effect.Effect<A, E, R> |
| 66 | + readonly idleTimeToLive?: Duration.DurationInput | undefined |
| 67 | + readonly shutdownMode?: "explicit" | "always" | undefined |
| 68 | +}) { |
| 69 | + const shutdownMode = options.shutdownMode ?? "always" |
| 70 | + let shuttingDown = false |
| 71 | + |
| 72 | + const ref = yield* RcRef.make({ |
| 73 | + acquire: Effect.gen(function*() { |
| 74 | + let scope = yield* Effect.scope |
| 75 | + |
| 76 | + if (shutdownMode === "explicit") { |
| 77 | + const closeable = yield* Scope.make() |
| 78 | + const context = yield* Effect.context<Sharding | Entity.CurrentAddress>() |
| 79 | + yield* Scope.addFinalizerExit( |
| 80 | + scope, |
| 81 | + Effect.fnUntraced(function*(exit) { |
| 82 | + if (shuttingDown) return |
| 83 | + yield* Scope.close(closeable, exit) |
| 84 | + yield* Entity.keepAlive(false) |
| 85 | + }, Effect.provide(context)) |
| 86 | + ) |
| 87 | + scope = closeable |
| 88 | + } else { |
| 89 | + yield* Effect.addFinalizer(() => { |
| 90 | + if (shuttingDown) return Effect.void |
| 91 | + return Entity.keepAlive(false) |
| 92 | + }) |
| 93 | + } |
| 94 | + |
| 95 | + yield* Entity.keepAlive(true) |
| 96 | + |
| 97 | + return yield* options.acquire.pipe( |
| 98 | + Scope.extend(scope) |
| 99 | + ) |
| 100 | + }), |
| 101 | + idleTimeToLive: options.idleTimeToLive ?? Duration.infinity |
| 102 | + }) |
| 103 | + |
| 104 | + yield* Effect.addFinalizer(() => { |
| 105 | + shuttingDown = true |
| 106 | + return Effect.void |
| 107 | + }) |
| 108 | + |
| 109 | + // Initialize the resource |
| 110 | + yield* Effect.scoped(RcRef.get(ref)) |
| 111 | + |
| 112 | + return identity<EntityResource<A, E>>({ |
| 113 | + [TypeId]: TypeId, |
| 114 | + get: RcRef.get(ref), |
| 115 | + close: RcRef.invalidate(ref) |
| 116 | + }) |
| 117 | +}) |
0 commit comments