Working TodoRepository
This commit is contained in:
@@ -9,5 +9,8 @@
|
||||
"@thilawyn/thilalib": "^0.1.4",
|
||||
"@todo-tests/common": "workspace:*",
|
||||
"effect": "^3.4.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bun-types": "^1.1.17"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Todo } from "@todo-tests/common/data"
|
||||
import { Array, Context, Data, Effect, Layer, Option, Ref, SubscriptionRef } from "effect"
|
||||
import crypto from "node:crypto"
|
||||
|
||||
|
||||
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, TodoRepositoryService>() {}
|
||||
@@ -13,17 +14,17 @@ export const TodoRepositoryLive = Layer.effect(TodoRepository,
|
||||
|
||||
export class TodoRepositoryService {
|
||||
constructor(
|
||||
readonly ref: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
||||
readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
||||
) {}
|
||||
|
||||
get(id: string) {
|
||||
return this.ref.get.pipe(
|
||||
return this.todos.get.pipe(
|
||||
Effect.map(Array.findFirst(todo => todo.id.value === id))
|
||||
)
|
||||
}
|
||||
|
||||
getIndex(id: string) {
|
||||
return this.ref.get.pipe(
|
||||
return this.todos.get.pipe(
|
||||
Effect.map(Array.findFirstIndex(todo => todo.id.value === id))
|
||||
)
|
||||
}
|
||||
@@ -33,10 +34,10 @@ export class TodoRepositoryService {
|
||||
if (Option.isSome(todo.id))
|
||||
return yield* Effect.fail(new TodoHasID({ todo }))
|
||||
|
||||
yield* Ref.update(this.ref, todos =>
|
||||
yield* Ref.update(this.todos, todos =>
|
||||
Array.append(todos, new Todo({
|
||||
...todo,
|
||||
id: Option.some(""),
|
||||
id: Option.some(crypto.randomUUID()),
|
||||
}) as Todo & { id: Option.Some<string> })
|
||||
)
|
||||
})
|
||||
@@ -47,7 +48,7 @@ export class TodoRepositoryService {
|
||||
if (Option.isNone(todo.id))
|
||||
return yield* Effect.fail(new TodoHasNoID({ todo }))
|
||||
|
||||
yield* Ref.update(this.ref, Array.replace(
|
||||
yield* Ref.update(this.todos, Array.replace(
|
||||
yield* yield* this.getIndex(todo.id.value),
|
||||
todo as Todo & { id: Option.Some<string> },
|
||||
))
|
||||
@@ -59,7 +60,7 @@ export class TodoRepositoryService {
|
||||
if (Option.isNone(todo.id))
|
||||
return yield* Effect.fail(new TodoHasNoID({ todo }))
|
||||
|
||||
yield* Ref.update(this.ref, Array.remove(
|
||||
yield* Ref.update(this.todos, Array.remove(
|
||||
yield* yield* this.getIndex(todo.id.value)
|
||||
))
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ const main = Effect.gen(function*() {
|
||||
yield* createDefaultTodos
|
||||
|
||||
const todos = yield* TodoRepository
|
||||
console.log(yield* todos.ref.get)
|
||||
console.log(yield* todos.todos.get)
|
||||
})
|
||||
|
||||
const runnableMain = main.pipe(
|
||||
|
||||
Reference in New Issue
Block a user