Refactoring

This commit is contained in:
Julien Valverdé
2024-07-04 21:26:16 +02:00
parent 7b578c657a
commit 5f15dedfbf
7 changed files with 36 additions and 33 deletions

View File

@@ -2,8 +2,8 @@ import { Layer } from "effect"
import { TodoRepositoryLive } from "./TodoRepository"
export const AppLive = Layer.mergeAll(
export const ServicesLive = Layer.mergeAll(
TodoRepositoryLive
)
export type AppR = Layer.Layer.Success<typeof AppLive>
export type Services = Layer.Layer.Success<typeof ServicesLive>

View File

@@ -4,4 +4,6 @@ import express from "express"
export class ExpressApp extends Context.Tag("Express")<ExpressApp, ReturnType<typeof express>>() {}
export const ExpressAppLive = Layer.sync(ExpressApp, () => express())
export module ExpressApp {
export const Live = Layer.sync(ExpressApp, () => express())
}

View File

@@ -1,8 +1,9 @@
import { Config, Effect, Layer } from "effect"
import { ExpressApp, ExpressAppLive } from "./ExpressApp"
import { ExpressApp } from "./ExpressApp"
export const ExpressHTTPServerLive = Layer.scopedDiscard(Effect.gen(function*() {
export module ExpressHTTPServer {
export const Live = Layer.scopedDiscard(Effect.gen(function*() {
const app = yield* ExpressApp
const port = yield* Config.number("PORT").pipe(Config.withDefault(8080))
@@ -15,6 +16,5 @@ export const ExpressHTTPServerLive = Layer.scopedDiscard(Effect.gen(function*()
server.close()
),
)
})).pipe(
Layer.provide(ExpressAppLive)
)
}))
}

View File

@@ -2,7 +2,7 @@ import { BunRuntime } from "@effect/platform-bun"
import { Todo } from "@todo-tests/common/data"
import { Identifiable } from "@todo-tests/common/traits"
import { Array, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
import { AppLive } from "./App"
import { ServicesLive } from "./Services"
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
import { TRPCEffectRuntimeLive } from "./trpc/TRPCEffectRuntime"
@@ -58,7 +58,7 @@ const main = Effect.gen(function*() {
const runnableMain = main.pipe(
Effect.provide(
Layer.mergeAll(
AppLive,
ServicesLive,
TRPCEffectRuntimeLive,
)
)

View File

@@ -1,6 +1,6 @@
import { createExpressMiddleware } from "@trpc/server/adapters/express"
import { Config, Effect, Layer } from "effect"
import { ExpressApp, ExpressAppLive } from "../express/ExpressApp"
import { ExpressApp } from "../express/ExpressApp"
import { createTRPCContext } from "../trpc/context"
import { router } from "./routers"
@@ -16,6 +16,4 @@ export const RPCServerLive = Layer.effectDiscard(Effect.gen(function*() {
createContext: createTRPCContext,
}),
)
})).pipe(
Layer.provide(ExpressAppLive)
)
}))

View File

@@ -6,4 +6,7 @@ import type { TRPCContext } from "./context"
const createTRPC = () => initTRPC.context<TRPCContext>().create()
export class TRPCBuilder extends Context.Tag("TRPCBuilder")<TRPCBuilder, ReturnType<typeof createTRPC>>() {}
export const TRPCBuilderLive = Layer.sync(TRPCBuilder, createTRPC)
export module TRPCBuilder {
export const Live = Layer.sync(TRPCBuilder, createTRPC)
}

View File

@@ -1,19 +1,19 @@
import { Context, Effect, Layer, Runtime } from "effect"
import type { RuntimeFiber } from "effect/Fiber"
import { AppLive, type AppR } from "../App"
import type { Services } from "../Services"
export class TRPCEffectRuntime extends Context.Tag("TRPCEffectRuntime")<TRPCEffectRuntime,
<A, E>(
self: Effect.Effect<A, E, AppR>,
self: Effect.Effect<A, E, Services>,
options?: Runtime.RunForkOptions,
) => RuntimeFiber<A, E>
>() {}
export const TRPCEffectRuntimeLive = Layer.effect(TRPCEffectRuntime,
Effect.runtime<AppR>().pipe(
export module TRPCEffectRuntime {
export const Live = Layer.effect(TRPCEffectRuntime,
Effect.runtime<Services>().pipe(
Effect.map(Runtime.runFork)
)
).pipe(
Layer.provide(AppLive)
)
}