Todos service

This commit is contained in:
Julien Valverdé
2024-06-27 10:35:52 +02:00
parent d6517365a7
commit 3dbb465500
10 changed files with 74 additions and 1 deletions

View File

@@ -6,6 +6,8 @@
"@effect/platform": "^0.58.12",
"@effect/platform-bun": "^0.38.11",
"@effect/schema": "^0.68.11",
"@thilawyn/thilalib": "^0.1.3",
"@todo-tests/common": "workspace:*",
"effect": "^3.4.4"
}
}

View File

@@ -0,0 +1,5 @@
import type { Todo } from "@todo-tests/common/data"
import { Context, Ref } from "effect"
export class Todos extends Context.Tag("Todos")<Todos, Ref.Ref<Todo[]>>() {}

View File

@@ -0,0 +1,36 @@
import { BunRuntime } from "@effect/platform-bun"
import { Todo } from "@todo-tests/common/data"
import { Array, Effect, Option, Ref } from "effect"
import { Todos } from "./Todos"
const createDefaultTodos = Todos.pipe(
Effect.flatMap(ref =>
Ref.update(ref, todos =>
Array.appendAll(todos, [
new Todo({
id: "1",
title: "A test todo",
content: "Lorem ipsum",
due: Option.none(),
createdAt: new Date(),
updatedAt: new Date(),
})
])
)
)
)
const main = Effect.gen(function*() {
yield* createDefaultTodos
const todos = yield* Todos
console.log(yield* Ref.get(todos))
})
const runnableMain = main.pipe(
Effect.provideServiceEffect(Todos, Ref.make(Array.empty()))
)
BunRuntime.runMain(runnableMain)