Skip to content

Commit

Permalink
remove RpcRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Jan 29, 2025
1 parent f06e23c commit af07ea9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 101 deletions.
14 changes: 13 additions & 1 deletion packages/rpc/src/Rpc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/**
* @since 1.0.0
*/
import type { Headers } from "@effect/platform/Headers"
import * as Context_ from "effect/Context"
import type { Effect } from "effect/Effect"
import { type Pipeable, pipeArguments } from "effect/Pipeable"
import * as Predicate from "effect/Predicate"
import * as Schema from "effect/Schema"
import type * as AST from "effect/SchemaAST"
import type { Stream } from "effect/Stream"
import type * as RpcMiddleware from "./RpcMiddleware.js"

/**
Expand Down Expand Up @@ -42,6 +45,7 @@ export interface Rpc<
> extends Pipeable {
readonly [TypeId]: TypeId
readonly _tag: Tag
readonly key: string
readonly payloadSchema: Payload
readonly successSchema: Success
readonly errorSchema: Error
Expand Down Expand Up @@ -119,6 +123,8 @@ export interface Rpc<
export interface Handler<Tag extends string> {
readonly _: unique symbol
readonly tag: Tag
readonly handler: (request: any, headers: Headers) => Effect<any, any> | Stream<any, any>
readonly context: Context<never>
}

/**
Expand All @@ -128,6 +134,7 @@ export interface Handler<Tag extends string> {
export interface Any extends Pipeable {
readonly [TypeId]: TypeId
readonly _tag: string
readonly key: string
}

/**
Expand All @@ -137,6 +144,7 @@ export interface Any extends Pipeable {
export interface AnyWithProps {
readonly [TypeId]: TypeId
readonly _tag: string
readonly key: string
readonly payloadSchema: AnyStructSchema
readonly successSchema: Schema.Schema.Any
readonly errorSchema: Schema.Schema.All
Expand Down Expand Up @@ -385,7 +393,11 @@ const makeProto = <
readonly errorSchema: Error
readonly annotations: Context_.Context<never>
readonly middlewares: ReadonlySet<Middleware>
}): Rpc<Tag, Payload, Success, Error, Middleware> => Object.assign(Object.create(Proto), options)
}): Rpc<Tag, Payload, Success, Error, Middleware> => {
const self = Object.assign(Object.create(Proto), options)
self.key = `@effect/rpc/Rpc/${options._tag}`
return self
}

const constEmptyStruct = Schema.Struct({})

Expand Down
37 changes: 6 additions & 31 deletions packages/rpc/src/RpcGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ export interface RpcGroup<Rpcs extends Rpc.Any> extends Pipeable {
RX
>
): Layer.Layer<
Rpc.ToHandler<Rpcs> | RpcRegistry,
Rpc.ToHandler<Rpcs>,
EX,
| Rpc.Middleware<Rpcs>
| Exclude<RX, Scope>
| (
keyof Handlers extends infer K ? K extends keyof Handlers & string ? Handlers[K] extends (...args: any) =>
Expand Down Expand Up @@ -155,19 +154,19 @@ const RpcGroupProto = {
})
},
toLayer(this: RpcGroup<any>, build: Effect.Effect<Record<string, (request: any) => any>>) {
return Layer.scopedDiscard(Effect.gen(this, function*() {
return Layer.scopedContext(Effect.gen(this, function*() {
const context = yield* Effect.context<never>()
const registry = yield* RpcRegistry
const handlers = Effect.isEffect(build) ? yield* build : build
const contextMap = new Map<string, unknown>()
for (const [tag, handler] of Object.entries(handlers)) {
const rpc = this.requests.get(tag)!
registry.set(tag, {
rpc,
contextMap.set(rpc.key, {
handler,
context
})
}
})).pipe(Layer.provideMerge(RpcRegistry.layer))
return Context.unsafeMake(contextMap)
}))
},
annotate(this: RpcGroup<any>, tag: Context.Tag<any, any>, value: any) {
return makeProto({
Expand Down Expand Up @@ -213,27 +212,3 @@ export const make = <const Rpcs extends ReadonlyArray<Rpc.Any | Rpc.AnyTaggedReq
requests: resolveInput(...rpcs),
annotations: Context.empty()
})

/**
* @since 1.0.0
* @category registry
*/
export class RpcRegistry extends Context.Tag("@effect/rpc/Rpc/RpcRegistry")<
RpcRegistry,
Map<string, RpcRegistryEntry>
>() {
/**
* @since 1.0.0
*/
static readonly layer = Layer.sync(RpcRegistry, () => new Map())
}

/**
* @since 1.0.0
* @category registry
*/
export interface RpcRegistryEntry {
readonly rpc: Rpc.AnyWithProps
readonly handler: (request: any, headers: Headers) => Effect.Effect<any, any> | Stream.Stream<any, any>
readonly context: Context.Context<never>
}
Loading

0 comments on commit af07ea9

Please sign in to comment.