@@ -24,13 +24,11 @@ import { logCause } from "liminal/_util/logCause"
2424import * as Mutex from "liminal/_util/Mutex"
2525import { type TopFromString , encodeJsonString , decodeJsonString } from "liminal/_util/schema"
2626
27- import { Binding , DurableObjectState , NativeRequest } from "./bindings/index.ts"
27+ import { DoState , NativeRequest , Binding } from "./bindings/index.ts"
2828import { close } from "./close.ts"
2929
3030const { debug, span } = Diagnostic . module ( "cloudflare.ActorRegistry" )
3131
32- const TypeId = "~liminal/cloudflare/ActorRegistry" as const
33-
3432export interface ActorRegistryDefinition <
3533 ActorSelf ,
3634 ActorId extends string ,
@@ -91,11 +89,9 @@ export interface ActorRegistry<
9189 PreludeE ,
9290 RunROut ,
9391 RunE ,
94- > extends Binding < RegistrySelf , RegistryId , DurableObjectNamespace , never , never , never > {
92+ > extends Context . Service < RegistrySelf , DurableObjectNamespace > {
9593 new ( state : globalThis . DurableObjectState < { } > ) : Context . ServiceClass . Shape < RegistryId , DurableObjectNamespace >
9694
97- readonly [ TypeId ] : typeof TypeId
98-
9995 readonly definition : ActorRegistryDefinition <
10096 ActorSelf ,
10197 ActorId ,
@@ -113,7 +109,9 @@ export interface ActorRegistry<
113109 readonly upgrade : (
114110 name : Name [ "Type" ] ,
115111 attachments : S . Struct < AttachmentFields > [ "Type" ] ,
116- ) => Effect . Effect < HttpServerResponse . HttpServerResponse , S . SchemaError , RegistrySelf | NativeRequest >
112+ ) => Effect . Effect < HttpServerResponse . HttpServerResponse , S . SchemaError , RegistrySelf | NativeRequest . NativeRequest >
113+
114+ readonly layer : ( binding : string ) => Layer . Layer < RegistrySelf , S . SchemaError , never >
117115}
118116
119117export const Service =
@@ -194,13 +192,13 @@ export const Service =
194192 encodeAttachments ( attachments ) . pipe ( Effect . andThen ( ( v ) => Effect . sync ( ( ) => socket . serializeAttachment ( v ) ) ) ) ,
195193 }
196194
197- class tag extends Binding < RegistrySelf > ( ) ( id , ( value ) : value is DurableObjectNamespace => "getByName" in value ) {
195+ const tag = class tag extends Context . Service < RegistrySelf , DurableObjectNamespace > ( ) ( id ) {
198196 readonly runtime
199197 readonly directory = ClientDirectory . make ( actor , transport )
200198
201- constructor ( state : globalThis . DurableObjectState < { } > , env : unknown ) {
202- // @ts -ignore
203- super ( state , env )
199+ constructor ( ... args : [ never ] ) {
200+ super ( ... args )
201+ const [ state , env ] = args as never as [ state : globalThis . DurableObjectState < { } > , env : unknown ]
204202
205203 if ( hibernation ) {
206204 Option . andThen (
@@ -212,7 +210,7 @@ export const Service =
212210 const baseLayer = Layer . mergeAll (
213211 prelude . pipe ( Layer . provideMerge ( ConfigProvider . layer ( ConfigProvider . fromUnknown ( env ) ) ) ) ,
214212 FetchHttpClient . layer ,
215- Layer . succeed ( DurableObjectState , state ) ,
213+ Layer . succeed ( DoState . DoState , state ) ,
216214 Mutex . layer ,
217215 )
218216
@@ -242,12 +240,12 @@ export const Service =
242240 const attachments = yield * decodeAttachmentsString ( url . searchParams . get ( "__liminal_attachments" ) )
243241 if ( ! this . #name) {
244242 this . #name = name
245- const state = yield * DurableObjectState
243+ const state = yield * DoState . DoState
246244 const encoded = yield * S . encodeEffect ( Name ) ( name )
247245 yield * Effect . promise ( ( ) => state . storage . put ( "__liminal_name" , encoded ) )
248246 }
249247 const { 0 : webSocket , 1 : server } = new WebSocketPair ( )
250- const state = yield * DurableObjectState
248+ const state = yield * DoState . DoState
251249 state . acceptWebSocket ( server )
252250 server . send ( yield * encodeAuditionSuccess ( { _tag : "Audition.Success" } ) )
253251 const currentClient = yield * this . directory . register ( server , attachments )
@@ -330,7 +328,7 @@ export const Service =
330328 const namespace = yield * tag
331329 const nameEncoded = yield * encodeName ( name )
332330 const stub = namespace . getByName ( nameEncoded )
333- const request = yield * NativeRequest
331+ const request = yield * NativeRequest . NativeRequest
334332 const protocols = yield * Effect . fromNullishOr ( request . headers . get ( SecWebSocketProtocol ) ) . pipe (
335333 Effect . map ( flow ( String . split ( "," ) , Array . map ( String . trim ) ) ) ,
336334 )
@@ -353,5 +351,7 @@ export const Service =
353351 return yield * Effect . promise ( ( ) => stub . fetch ( new Request ( url , request ) ) ) . pipe ( Effect . map ( HttpServerResponse . raw ) )
354352 } , span ( "upgrade" ) )
355353
356- return Object . assign ( tag , { [ TypeId ] : TypeId , definition, upgrade } ) as never
354+ const layer = Binding . layer ( tag , [ "getByName" ] )
355+
356+ return Object . assign ( tag , { definition, upgrade, layer } ) as never
357357 }
0 commit comments