Skip to content

Commit ada7cba

Browse files
committed
fix: remove Resource from Binding tag construction and implement layer builders
1 parent b154d4f commit ada7cba

File tree

8 files changed

+38
-61
lines changed

8 files changed

+38
-61
lines changed

alchemy-effect-aws/src/sqs/queue.consume.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const QueueEventSource = Binding<
2525
queue: Q,
2626
props?: Props,
2727
) => Binding<Function, Consume<From<Q>>, Props, "QueueEventSource">
28-
>(Function, Queue, "AWS.SQS.Consume", "QueueEventSource");
28+
>(Function, "AWS.SQS.Consume", "QueueEventSource");
2929

3030
export const consumeFromLambdaFunction = () =>
3131
QueueEventSource.provider.succeed({

alchemy-effect-aws/src/sqs/queue.send-message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface SendMessage<Q = Queue>
1515

1616
export const SendMessage = Binding<
1717
<Q extends Queue>(queue: Q) => Binding<Function, SendMessage<To<Q>>>
18-
>(Function, Queue, "AWS.SQS.SendMessage");
18+
>(Function, "AWS.SQS.SendMessage");
1919

2020
export const sendMessage = <Q extends Queue>(
2121
queue: Q,

alchemy-effect-cloudflare/src/binding.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

alchemy-effect-cloudflare/src/kv.binding.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

alchemy-effect/src/binding.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Context from "effect/Context";
22
import type { Effect } from "effect/Effect";
3-
import type { Layer } from "effect/Layer";
3+
import * as Layer from "effect/Layer";
44
import type { Capability } from "./capability.ts";
55
import type { Resource } from "./resource.ts";
66
import type { Runtime } from "./runtime.ts";
@@ -64,31 +64,29 @@ export interface Bind<
6464
export const Binding: {
6565
<F extends (resource: any, props?: any) => AnyBinding & { isCustom: true }>(
6666
runtime: ReturnType<F>["runtime"],
67-
resource: new () => ReturnType<F>["capability"]["resource"],
67+
// resource: new () => ReturnType<F>["capability"]["resource"],
6868
type: ReturnType<F>["capability"]["type"],
6969
tag: ReturnType<F>["tag"],
7070
): F & BindingDeclaration<ReturnType<F>["runtime"], F>;
7171
<F extends (resource: any, props?: any) => AnyBinding & { isCustom: false }>(
7272
runtime: ReturnType<F>["runtime"],
73-
resource: new () => ReturnType<F>["capability"]["resource"],
73+
// resource: new () => ReturnType<F>["capability"]["resource"],
7474
type: ReturnType<F>["capability"]["type"],
7575
): F & BindingDeclaration<ReturnType<F>["runtime"], F>;
76-
} = (runtime: any, resource: any, type: string, tag?: string) =>
77-
Object.assign(
76+
} = (runtime: any, cap: string, tag?: string) => {
77+
const Tag = Context.Tag(`${runtime.type}(${cap}, ${tag ?? cap})`)();
78+
return Object.assign(
7879
() => {
79-
throw new Error(`Should never be called`);
80+
throw new Error(`Not implemented`);
8081
},
8182
{
8283
provider: {
83-
effect: () => {
84-
throw new Error(`Not implemented`);
85-
},
86-
succeed: () => {
87-
throw new Error(`Not implemented`);
88-
},
84+
effect: (eff) => Layer.effect(Tag, eff),
85+
succeed: (service) => Layer.succeed(Tag, service),
8986
},
9087
} satisfies BindingDeclaration<Runtime, any>,
9188
);
89+
};
9290

9391
export interface BindingDeclaration<
9492
Run extends Runtime,
@@ -103,10 +101,10 @@ export interface BindingDeclaration<
103101
Err,
104102
Req
105103
>,
106-
): Layer<Bind<Run, Cap, Tag>, Err, Req>;
104+
): Layer.Layer<Bind<Run, Cap, Tag>, Err, Req>;
107105
succeed(
108106
service: BindingService<Run, Parameters<F>[0], Parameters<F>[1]>,
109-
): Layer<Bind<Run, Cap, Tag>>;
107+
): Layer.Layer<Bind<Run, Cap, Tag>>;
110108
};
111109
}
112110

alchemy-effect/src/provider.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as Context from "effect/Context";
22
import type * as Effect from "effect/Effect";
33
import type { ScopedPlanStatusSession } from "./apply.ts";
4-
import type { Instance } from "./policy.ts";
54
import type { Resource } from "./resource.ts";
65
import type { Runtime } from "./runtime.ts";
76

@@ -10,9 +9,6 @@ export type Provider<R extends Resource> = Context.TagClass<
109
R["type"],
1110
ProviderService<R>
1211
>;
13-
export const Provider = <R extends Resource>(R: R) => {
14-
return Context.Tag(R.type)() as Provider<Instance<R>>;
15-
};
1612

1713
export type Diff =
1814
| {

alchemy-effect/src/resource.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import * as Context from "effect/Context";
12
import type { Effect } from "effect/Effect";
2-
import type { Layer } from "effect/Layer";
3+
import * as Layer from "effect/Layer";
34
import type { Provider, ProviderService } from "./provider.ts";
45

56
export interface IResource<
@@ -26,8 +27,9 @@ export interface Resource<
2627

2728
export const Resource = <Ctor extends (id: string, props: any) => Resource>(
2829
type: ReturnType<Ctor>["type"],
29-
) =>
30-
class {
30+
) => {
31+
const Tag = Context.Tag(type)();
32+
return class {
3133
static readonly type = type;
3234
constructor(id: string, props: any) {
3335
if (!new.target) {
@@ -42,6 +44,12 @@ export const Resource = <Ctor extends (id: string, props: any) => Resource>(
4244
};
4345
}
4446
}
47+
static provider = {
48+
effect: (eff: Effect<ProviderService<ReturnType<Ctor>>, any, any>) =>
49+
Layer.effect(Tag, eff),
50+
succeed: (service: ProviderService<ReturnType<Ctor>>) =>
51+
Layer.succeed(Tag, service),
52+
};
4553
} as unknown as Ctor & {
4654
type: ReturnType<Ctor>["type"];
4755
new (): ReturnType<Ctor> & {
@@ -50,9 +58,10 @@ export const Resource = <Ctor extends (id: string, props: any) => Resource>(
5058
provider: {
5159
effect<Err, Req>(
5260
eff: Effect<ProviderService<ReturnType<Ctor>>, Err, Req>,
53-
): Layer<Provider<ReturnType<Ctor>>, Err, Req>;
61+
): Layer.Layer<Provider<ReturnType<Ctor>>, Err, Req>;
5462
succeed(
5563
service: ProviderService<ReturnType<Ctor>>,
56-
): Layer<Provider<ReturnType<Ctor>>>;
64+
): Layer.Layer<Provider<ReturnType<Ctor>>>;
5765
};
5866
};
67+
};

alchemy-effect/src/runtime.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Types } from "effect";
22
import * as Context from "effect/Context";
33
import type { Effect } from "effect/Effect";
4-
import type { Layer } from "effect/Layer";
4+
import * as Layer from "effect/Layer";
55
import type { Capability } from "./capability.ts";
66
import type { Policy } from "./policy.ts";
77
import type { ProviderService } from "./provider.ts";
@@ -75,10 +75,11 @@ export const Runtime =
7575
provider: {
7676
effect<Err, Req>(
7777
eff: Effect<ProviderService<Self>, Err, Req>,
78-
): Layer<Self, Err, Req>;
79-
succeed(service: ProviderService<Self>): Layer<Self>;
78+
): Layer.Layer<Self, Err, Req>;
79+
succeed(service: ProviderService<Self>): Layer.Layer<Self>;
8080
};
8181
} => {
82+
const Tag = Context.Tag(type)();
8283
const self = Object.assign(
8384
(
8485
...args:
@@ -122,6 +123,12 @@ export const Runtime =
122123
type: type,
123124
id: undefined! as string,
124125
capability: undefined! as Capability[],
126+
provider: {
127+
effect: (eff: Effect<ProviderService<Self>, any, any>) =>
128+
Layer.effect(Tag, eff),
129+
succeed: (service: ProviderService<Self>) =>
130+
Layer.succeed(Tag, service),
131+
},
125132
toString() {
126133
return `${this.type}(${this.id}${
127134
this.capability?.length

0 commit comments

Comments
 (0)