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"),
|
||||
title: "A test todo",
|
||||
content: "Lorem ipsum",
|
||||
due: Option.none(),
|
||||
completed: false,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
])
|
||||
)
|
||||
))
|
||||
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(),
|
||||
}))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user