Skip to content

Commit c59b76c

Browse files
committed
feat: introduce BindingTag to map Binding -> BindingService
1 parent 9e8fc77 commit c59b76c

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

@alchemy.run/effect/src/binding.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Context from "effect/Context";
12
import type { Effect } from "effect/Effect";
23
import type { Layer } from "effect/Layer";
34
import type { Capability } from "./capability.ts";
@@ -13,20 +14,37 @@ export const isBinding = (b: any): b is Binding<any, any, any> =>
1314

1415
export type AnyBinding<F extends Runtime = any> = Binding<F, any, any>;
1516

16-
export interface Binding<
17+
export type Binding<
1718
Run extends Runtime,
1819
Cap extends Capability = Capability,
19-
Tag = Cap["type"],
20-
> {
20+
Tag extends string = Cap["type"],
21+
> = {
2122
runtime: Run;
2223
capability: Cap;
23-
tag: Tag;
24+
tag: BindingTag<Run, Cap, Tag>;
25+
};
26+
27+
export interface BindingTag<
28+
Run extends Runtime,
29+
Cap extends Capability,
30+
Tag extends string,
31+
> extends Context.Tag<
32+
`${Run["type"]}(${Cap["type"]}, ${Tag})`,
33+
BindingService<
34+
Run,
35+
Extract<Extract<Cap["resource"], Resource>["parent"], Resource>,
36+
Run["props"]
37+
>
38+
> {
39+
/** @internal phantom */
40+
tagName: Tag;
2441
}
2542

2643
export const Binding = <F extends (resource: any, props?: any) => AnyBinding>(
2744
runtime: ReturnType<F>["runtime"],
2845
resource: new () => ReturnType<F>["capability"]["resource"],
29-
tag: ReturnType<F>["tag"],
46+
tag: ReturnType<F>["tag"]["tagName"],
47+
// _tag: ReturnType<F>,
3048
): F & BindingDeclaration<ReturnType<F>["runtime"], F> => {
3149
type Runtime = ReturnType<F>["runtime"];
3250
type Tag = ReturnType<F>["tag"];
@@ -93,13 +111,13 @@ export type BindingService<
93111
},
94112
to: Props,
95113
target: Target,
96-
) => Effect.Effect<Partial<Props> | void, never, AttachReq>;
114+
) => Effect<Partial<Props> | void, never, AttachReq>;
97115
detach?: (
98116
resource: {
99117
id: string;
100118
attr: R["attr"];
101119
props: R["props"];
102120
},
103121
from: Props,
104-
) => Effect.Effect<void, never, DetachReq>;
122+
) => Effect<void, never, DetachReq>;
105123
};

@alchemy.run/effect/src/resource.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface Resource<
1010
Props = unknown,
1111
Attrs = unknown,
1212
> extends IResource<Type, ID, Props, Attrs> {
13+
parent: unknown;
1314
// oxlint-disable-next-line no-misused-new
1415
new (): Resource<Type, ID, Props, Attrs>;
1516
}
@@ -34,5 +35,7 @@ export const Resource = <Ctor extends (id: string, props: any) => Resource>(
3435
}
3536
} as unknown as Ctor & {
3637
type: ReturnType<Ctor>["type"];
37-
new (): ReturnType<Ctor>;
38+
new (): ReturnType<Ctor> & {
39+
parent: ReturnType<Ctor>;
40+
};
3841
};

0 commit comments

Comments
 (0)