|
4 | 4 | * This service manages cancellation tokens for long-running RPC operations. |
5 | 5 | */ |
6 | 6 |
|
7 | | -import { Context, type Effect } from "effect"; |
| 7 | +import { Context, Scope, type Effect } from "effect"; |
8 | 8 |
|
9 | 9 | import type { InvalidTokenIDError } from "./Error.js"; |
10 | 10 | import type { TokenAndScope } from "./Type.js"; |
11 | 11 |
|
12 | | -/** |
13 | | - * The service interface for the cancellation token provider. |
14 | | - */ |
15 | | -export interface Interface { |
16 | | - /** |
17 | | - * Acquires a CancellationToken for a given operation ID. |
18 | | - * This returns a scoped Effect. When the scope is closed, the token |
19 | | - * source is automatically disposed and cleaned up. |
20 | | - * @param TokenID The numeric ID for the operation. |
21 | | - * @returns A scoped `Effect` that resolves to a `TokenAndScope` object. |
22 | | - */ |
23 | | - readonly ObtainToken: ( |
24 | | - TokenID: number, |
25 | | - ) => Effect.Effect<TokenAndScope, InvalidTokenIDError, Effect.Scope>; |
26 | | - |
27 | | - /** |
28 | | - * Signals cancellation for a specific token ID. |
29 | | - * @param TokenID The numeric ID of the operation to cancel. |
30 | | - */ |
31 | | - readonly CancelToken: (TokenID: number) => Effect.Effect<void, never>; |
| 12 | +export class Cancellation extends Context.Tag( |
| 13 | + "Service/CancellationTokenProvider", |
| 14 | +)< |
| 15 | + Cancellation, |
| 16 | + { |
| 17 | + /** |
| 18 | + * Acquires a CancellationToken for a given operation ID. |
| 19 | + * This returns a scoped Effect. When the scope is closed, the token |
| 20 | + * source is automatically disposed and cleaned up. |
| 21 | + * @param TokenID The numeric ID for the operation. |
| 22 | + * @returns A scoped `Effect` that resolves to a `TokenAndScope` object. |
| 23 | + */ |
| 24 | + readonly ObtainToken: ( |
| 25 | + TokenID: number, |
| 26 | + ) => Effect.Effect<TokenAndScope, InvalidTokenIDError, Scope.Scope>; |
32 | 27 |
|
33 | | - /** |
34 | | - * Disposes of all currently managed cancellation token sources. |
35 | | - * This is typically called during shutdown. |
36 | | - */ |
37 | | - readonly DisposeAll: () => Effect.Effect<void, never>; |
38 | | -} |
| 28 | + /** |
| 29 | + * Signals cancellation for a specific token ID. |
| 30 | + * @param TokenID The numeric ID of the operation to cancel. |
| 31 | + */ |
| 32 | + readonly CancelToken: (TokenID: number) => Effect.Effect<void, never>; |
39 | 33 |
|
40 | | -/** |
41 | | - * The Context.Tag for the CancellationTokenProvider service. |
42 | | - */ |
43 | | -export const Tag = Context.Tag<Interface>("Service/CancellationTokenProvider"); |
| 34 | + /** |
| 35 | + * Disposes of all currently managed cancellation token sources. |
| 36 | + * This is typically called during shutdown. |
| 37 | + */ |
| 38 | + readonly DisposeAll: () => Effect.Effect<void, never>; |
| 39 | + } |
| 40 | +>() {} |
0 commit comments