TodoRepository work
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { Todo } from "@todo-tests/common/data"
|
||||
import { Array, Context, Data, Effect, Option, Ref, SubscriptionRef } from "effect"
|
||||
import { Array, Context, Data, Effect, Layer, Option, Ref, SubscriptionRef } from "effect"
|
||||
|
||||
|
||||
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, Ref.Ref<Todo[]>>() {}
|
||||
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, TodoRepositoryService>() {}
|
||||
|
||||
export const TodoRepositoryLive = Layer.effect(TodoRepository,
|
||||
SubscriptionRef.make(Array.empty<Todo & { id: Option.Some<string> }>()).pipe(
|
||||
Effect.map(ref => new TodoRepositoryService(ref))
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
export class TodoRepositoryService {
|
||||
@@ -60,27 +66,20 @@ export class TodoRepositoryService {
|
||||
}
|
||||
}
|
||||
|
||||
export class TodoHasID extends Data.TaggedError("TodoHasID")<{
|
||||
todo: Todo
|
||||
}> {}
|
||||
|
||||
export class TodoHasNoID extends Data.TaggedError("TodoHasNoID")<{
|
||||
todo: Todo
|
||||
}> {}
|
||||
export class TodoHasID extends Data.TaggedError("TodoHasID")<{ todo: Todo }> {}
|
||||
export class TodoHasNoID extends Data.TaggedError("TodoHasNoID")<{ todo: Todo }> {}
|
||||
|
||||
|
||||
export const createDefaultTodos = TodoRepository.pipe(Effect.flatMap(repo =>
|
||||
Ref.update(repo, todos =>
|
||||
Array.appendAll(todos, [
|
||||
new Todo({
|
||||
id: Option.some("1"),
|
||||
export const createDefaultTodos = Effect.gen(function*() {
|
||||
const todos = yield* TodoRepository
|
||||
|
||||
yield* todos.add(new Todo({
|
||||
id: Option.none(),
|
||||
title: "A test todo",
|
||||
content: "Lorem ipsum",
|
||||
due: Option.none(),
|
||||
completed: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
}))
|
||||
})
|
||||
])
|
||||
)
|
||||
))
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { BunRuntime } from "@effect/platform-bun"
|
||||
import { Array, Effect, Ref } from "effect"
|
||||
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
|
||||
import { Effect } from "effect"
|
||||
import { TodoRepository, TodoRepositoryLive, createDefaultTodos } from "./TodoRepository"
|
||||
|
||||
|
||||
const main = Effect.gen(function*() {
|
||||
yield* createDefaultTodos
|
||||
|
||||
const todos = yield* TodoRepository
|
||||
console.log(yield* Ref.get(todos))
|
||||
console.log(yield* todos.ref.get)
|
||||
})
|
||||
|
||||
const runnableMain = main.pipe(
|
||||
Effect.provideServiceEffect(TodoRepository, Ref.make(Array.empty()))
|
||||
Effect.provide(TodoRepositoryLive)
|
||||
)
|
||||
|
||||
BunRuntime.runMain(runnableMain)
|
||||
|
||||
Reference in New Issue
Block a user