68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
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 { MainLive } from "./Main"
|
|
import { TRPCEffectRuntimeLive } from "./TRPCEffectRuntime"
|
|
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
|
|
|
|
|
|
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)
|
|
})
|
|
|
|
const runnableMain = main.pipe(
|
|
Effect.provide(
|
|
Layer.mergeAll(
|
|
TRPCEffectRuntimeLive,
|
|
MainLive,
|
|
)
|
|
)
|
|
)
|
|
|
|
BunRuntime.runMain(runnableMain)
|