effect-cf 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
- import { Effect, Schema as S, type Layer } from "effect";
1
+ import { Context, Effect, Schema as S, type Layer } from "effect";
2
2
 
3
+ import * as Binding from "./Binding";
3
4
  import type { WorkerEnvironment } from "./Environment";
4
5
  import type { ExecutionContext, WorkerContext } from "./Worker";
5
6
  import type * as RpcDefinition from "./RpcDefinition";
@@ -24,15 +25,6 @@ export namespace Definition {
24
25
  >;
25
26
  }
26
27
 
27
- declare const BindingServiceTypeId: unique symbol;
28
-
29
- export interface BindingService<Id extends string, Self extends Definition.Any> {
30
- readonly [BindingServiceTypeId]: {
31
- readonly id: Id;
32
- readonly definition: Self;
33
- };
34
- }
35
-
36
28
  export type Handler<ROut, Self extends Definition.Any> = (
37
29
  payload: S.Schema.Type<Self["payload"]>,
38
30
  ) => Effect.Effect<
@@ -45,6 +37,62 @@ export interface Options<ROut, Self extends Definition.Any> {
45
37
  readonly run: Handler<ROut, Self>;
46
38
  }
47
39
 
40
+ export type LayerOptions = {
41
+ readonly binding: string;
42
+ };
43
+
44
+ export interface TagClass<
45
+ Self,
46
+ Id extends string,
47
+ Payload extends RpcDefinition.ServiceFreeSchema,
48
+ Result extends RpcDefinition.ServiceFreeSchema,
49
+ > extends Context.ServiceClass<Self, Id, WorkflowBinding.WorkflowBindingClient<Payload, Result>> {
50
+ readonly id: Id;
51
+ readonly payload: Payload;
52
+ readonly result: Result;
53
+ readonly make: <ROut, LayerError>(
54
+ layer: Layer.Layer<ROut, LayerError, ExecutionContext | WorkerContext | WorkerEnvironment>,
55
+ options: Options<ROut, Definition<Id, Payload, Result>>,
56
+ ) => WorkflowEntrypoint.WorkflowClass<S.Codec.Encoded<Payload>, S.Codec.Encoded<Result>, ROut>;
57
+ readonly layer: (
58
+ options: LayerOptions,
59
+ ) => Layer.Layer<
60
+ Self,
61
+ Binding.BindingNotFoundError | Binding.BindingValidationError,
62
+ WorkerEnvironment
63
+ >;
64
+ readonly create: (
65
+ payload: S.Schema.Type<Payload>,
66
+ options?: WorkflowBinding.WorkflowInstanceCreateOptions<S.Codec.Encoded<Payload>>,
67
+ ) => Effect.Effect<
68
+ WorkflowBinding.WorkflowInstance<S.Schema.Type<Result>>,
69
+ WorkflowBinding.WorkflowOperationError | S.SchemaError,
70
+ Self
71
+ >;
72
+ readonly createBatch: (
73
+ batch: WorkflowBinding.WorkflowInstanceCreateBatchOptions<
74
+ S.Schema.Type<Payload>,
75
+ S.Codec.Encoded<Payload>
76
+ >,
77
+ ) => Effect.Effect<
78
+ ReadonlyArray<WorkflowBinding.WorkflowInstance<S.Schema.Type<Result>>>,
79
+ WorkflowBinding.WorkflowOperationError | S.SchemaError,
80
+ Self
81
+ >;
82
+ readonly get: (
83
+ instanceId: string,
84
+ ) => Effect.Effect<
85
+ WorkflowBinding.WorkflowInstance<S.Schema.Type<Result>>,
86
+ WorkflowBinding.WorkflowOperationError,
87
+ Self
88
+ >;
89
+ readonly unsafeRaw: () => Effect.Effect<
90
+ globalThis.Workflow<S.Codec.Encoded<Payload>>,
91
+ never,
92
+ Self
93
+ >;
94
+ }
95
+
48
96
  const makeDefinition = <
49
97
  Id extends string,
50
98
  Payload extends RpcDefinition.ServiceFreeSchema,
@@ -71,43 +119,23 @@ const makeDefinition = <
71
119
  WorkflowEntrypoint.make(layer, {
72
120
  run: wrapHandler(workflowDefinition, options.run),
73
121
  }),
74
- Binding:
75
- <Self>() =>
76
- <BindingId extends string>(
77
- bindingId: BindingId,
78
- binding: Omit<
79
- WorkflowBinding.WorkflowBindingDefinition<Payload, Result>,
80
- "payload" | "result"
81
- >,
82
- ) =>
83
- WorkflowBinding.Service<Self>()<BindingId, Payload, Result>(bindingId, {
84
- ...binding,
85
- payload: definition.payload,
86
- result: definition.result,
87
- }),
88
- binding: <BindingId extends string>(
89
- bindingId: BindingId,
90
- binding: Omit<
91
- WorkflowBinding.WorkflowBindingDefinition<Payload, Result>,
92
- "payload" | "result"
93
- >,
94
- ) =>
95
- WorkflowBinding.Service<BindingService<BindingId, SelfDefinition>>()<
96
- BindingId,
97
- Payload,
98
- Result
99
- >(bindingId, {
100
- ...binding,
101
- payload: definition.payload,
102
- result: definition.result,
103
- }),
104
122
  });
105
123
  };
106
124
 
107
- export const make = makeDefinition;
125
+ export const make = <
126
+ Id extends string,
127
+ Payload extends RpcDefinition.ServiceFreeSchema,
128
+ Result extends RpcDefinition.ServiceFreeSchema,
129
+ >(
130
+ id: Id,
131
+ definition: {
132
+ readonly payload: Payload;
133
+ readonly result: Result;
134
+ },
135
+ ) => Tag<Definition<Id, Payload, Result>>()(id, definition);
108
136
 
109
137
  export const Tag =
110
- <_Self>() =>
138
+ <Self>() =>
111
139
  <
112
140
  Id extends string,
113
141
  Payload extends RpcDefinition.ServiceFreeSchema,
@@ -120,17 +148,54 @@ export const Tag =
120
148
  },
121
149
  ) => {
122
150
  const workflowDefinition = makeDefinition(id, definition);
151
+ const tag = Context.Service<Self, WorkflowBinding.WorkflowBindingClient<Payload, Result>>()(id);
123
152
 
124
- abstract class WorkflowDefinitionClass {
125
- static readonly id = workflowDefinition.id;
126
- static readonly payload = workflowDefinition.payload;
127
- static readonly result = workflowDefinition.result;
128
- static readonly make = workflowDefinition.make;
129
- static readonly Binding = workflowDefinition.Binding;
130
- static readonly binding = workflowDefinition.binding;
131
- }
153
+ const layer = (binding: LayerOptions) =>
154
+ WorkflowBinding.layer(tag, {
155
+ ...binding,
156
+ payload: definition.payload,
157
+ result: definition.result,
158
+ });
159
+
160
+ const create = Effect.fnUntraced(function* (
161
+ payload: S.Schema.Type<Payload>,
162
+ options?: WorkflowBinding.WorkflowInstanceCreateOptions<S.Codec.Encoded<Payload>>,
163
+ ) {
164
+ const workflow = yield* tag;
165
+ return yield* workflow.create(payload, options);
166
+ });
167
+
168
+ const createBatch = Effect.fnUntraced(function* (
169
+ batch: WorkflowBinding.WorkflowInstanceCreateBatchOptions<
170
+ S.Schema.Type<Payload>,
171
+ S.Codec.Encoded<Payload>
172
+ >,
173
+ ) {
174
+ const workflow = yield* tag;
175
+ return yield* workflow.createBatch(batch);
176
+ });
177
+
178
+ const get = Effect.fnUntraced(function* (instanceId: string) {
179
+ const workflow = yield* tag;
180
+ return yield* workflow.get(instanceId);
181
+ });
182
+
183
+ const unsafeRaw = Effect.fnUntraced(function* () {
184
+ const workflow = yield* tag;
185
+ return yield* workflow.unsafeRaw;
186
+ });
132
187
 
133
- return WorkflowDefinitionClass as (abstract new () => object) & typeof workflowDefinition;
188
+ return Object.assign(tag, {
189
+ id: workflowDefinition.id,
190
+ payload: workflowDefinition.payload,
191
+ result: workflowDefinition.result,
192
+ make: workflowDefinition.make,
193
+ layer,
194
+ create,
195
+ createBatch,
196
+ get,
197
+ unsafeRaw,
198
+ }) as TagClass<Self, Id, Payload, Result>;
134
199
  };
135
200
 
136
201
  export const Workflow = Tag;
package/src/index.ts CHANGED
@@ -8,10 +8,13 @@ export * as DurableObjectRpcWebSocket from "./DurableObjectRpcWebSocket";
8
8
  export * as DurableObjectState from "./DurableObjectState";
9
9
  export * as DurableObjectWebSocket from "./DurableObjectWebSocket";
10
10
  export * as DurableObjectStorage from "./DurableObjectStorage";
11
+ export * as Hyperdrive from "./Hyperdrive";
12
+ export * as Images from "./Images";
11
13
  export * as Kv from "./Kv";
12
14
  export * as Queue from "./Queue";
13
15
  export * as QueueBinding from "./QueueBinding";
14
16
  export * as QueueDefinition from "./QueueDefinition";
17
+ export * as R2 from "./R2";
15
18
  export * as Rpc from "./Rpc";
16
19
  export * as RpcDefinition from "./RpcDefinition";
17
20
  export * as ServiceBinding from "./ServiceBinding";