Files
todo-tests/packages/server/src/index.ts
Julien Valverdé 73bee895b1 WebUI setup
2024-07-06 21:33:31 +02:00

79 lines
2.6 KiB
TypeScript

import { BunRuntime } from "@effect/platform-bun"
import { Todo } from "@todo-tests/common/data"
import { Identifiable } from "@todo-tests/common/traits"
import { Array, Effect, Layer, Option, Stream } from "effect"
import { ServicesLive } from "./Services"
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
import { ExpressApp } from "./express/ExpressApp"
import { ExpressHTTPServer } from "./express/ExpressHTTPServer"
import { RPCPlayground } from "./rpc/RPCPlayground"
import { RPCRouter } from "./rpc/RPCRouter"
import { RPCServer } from "./rpc/RPCServer"
import { RPCProcedureBuilder } from "./rpc/procedures/RPCProcedureBuilder"
import { TRPCBuilder } from "./trpc/TRPCBuilder"
import { TRPCContextCreator } from "./trpc/TRPCContextCreator"
const ServerDev = ExpressHTTPServer.Live.pipe(
Layer.provide(RPCServer.Live),
Layer.provide(RPCPlayground.Dev),
Layer.provide(RPCRouter.Live),
Layer.provide(RPCProcedureBuilder.Live),
Layer.provide(TRPCBuilder.Live),
Layer.provide(TRPCContextCreator.Live),
Layer.provide(ExpressApp.Live),
)
const watchTodoChanges = Effect.gen(function*() {
const todos = yield* TodoRepository
// yield* Stream.runForEach(todos.todos.changes, todos => Console.log(`Todos changed: ${ todos }`))
yield* todos.todos.changes.pipe(
Stream.runFold(
Array.empty<Todo & { id: Option.Some<string> }>(),
(prev, curr) => {
console.log(`Added todos: ${ Array.differenceWith<Todo>(Identifiable.equals)(curr, prev) }`)
console.log(`Removed todos: ${ Array.differenceWith<Todo>(Identifiable.equals)(prev, curr) }`)
return curr
},
)
)
})
const main = Effect.gen(function*() {
// const watcher = yield* Effect.fork(watchTodoChanges)
yield* createDefaultTodos
// const todos = yield* TodoRepository
// const secondTodo = yield* yield* todos.todos.get.pipe(
// Effect.map(Array.get(1))
// )
// const secondTodoModified = new Todo({
// ...secondTodo,
// completed: true,
// })
// yield* todos.update(secondTodoModified)
// yield* todos.add(new Todo({
// id: Option.none(),
// title: "Put the dishes in the dishwasher",
// content: "Lorem ipsum",
// due: Option.none(),
// completed: false,
// createdAt: new Date(),
// updatedAt: new Date(),
// })).pipe(
// Effect.delay(Duration.seconds(1))
// )
// yield* Fiber.join(watcher)
yield* Layer.launch(ServerDev)
})
BunRuntime.runMain(main.pipe(
Effect.provide(ServicesLive)
))