Chunk instead of Array

This commit is contained in:
Julien Valverdé
2024-07-09 00:12:22 +02:00
parent e88a6f7b1c
commit fb9ce1bb67
2 changed files with 21 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
import { Todo, type IdentifiedTodo } from "@todo-tests/common/data"
import { Array, Context, Data, Effect, Equal, Layer, Option, Order, Ref, SubscriptionRef, flow } from "effect"
import { Chunk, Context, Data, Effect, Equal, Layer, Option, Order, Ref, SubscriptionRef, flow } from "effect"
import crypto from "node:crypto"
@@ -7,7 +7,7 @@ export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository
export module TodoRepository {
export const Live = Layer.effect(TodoRepository,
SubscriptionRef.make(Array.empty<IdentifiedTodo>()).pipe(
SubscriptionRef.make(Chunk.empty<IdentifiedTodo>()).pipe(
Effect.map(ref => new TodoRepositoryService(ref))
)
)
@@ -17,19 +17,19 @@ export module TodoRepository {
export class TodoRepositoryService {
constructor(
readonly todos: SubscriptionRef.SubscriptionRef<IdentifiedTodo[]>
readonly todos: SubscriptionRef.SubscriptionRef<Chunk.Chunk<IdentifiedTodo>>
) {}
getByID(id: string) {
return this.todos.get.pipe(
Effect.map(Array.findFirst(todo => Equal.equals(todo.id.value, id)))
Effect.map(Chunk.findFirst(todo => Equal.equals(todo.id.value, id)))
)
}
getIndexByID(id: string) {
return this.todos.get.pipe(
Effect.map(Array.findFirstIndex(todo => Equal.equals(todo.id.value, id)))
Effect.map(Chunk.findFirstIndex(todo => Equal.equals(todo.id.value, id)))
)
}
@@ -42,7 +42,7 @@ export class TodoRepositoryService {
const id: string = crypto.randomUUID()
yield* Ref.update(this.todos, flow(
Array.append(new Todo({
Chunk.append(new Todo({
...todo,
id: Option.some(id),
}) as IdentifiedTodo),
@@ -61,7 +61,7 @@ export class TodoRepositoryService {
return yield* Effect.fail(new TodoHasNoID({ todo }))
yield* Ref.update(this.todos, flow(
Array.replace(
Chunk.replace(
yield* yield* this.getIndexByID(todo.id.value),
todo as IdentifiedTodo,
),
@@ -78,7 +78,7 @@ export class TodoRepositoryService {
return yield* Effect.fail(new TodoHasNoID({ todo }))
yield* Ref.update(this.todos, flow(
Array.remove(
Chunk.remove(
yield* yield* this.getIndexByID(todo.id.value)
),
updateTodosOrder,
@@ -88,11 +88,14 @@ export class TodoRepositoryService {
}
const sortTodos = Array.sortBy<IdentifiedTodo[]>(
const sortTodos = Chunk.sort<IdentifiedTodo>(
Order.mapInput(Order.number, todo => todo.order)
)
const updateTodosOrder = Array.map<IdentifiedTodo[], IdentifiedTodo>((todo, order) =>
const updateTodosOrder = Chunk.map<
Chunk.Chunk<IdentifiedTodo>,
IdentifiedTodo
>((todo, order) =>
new Todo({ ...todo, order }, { disableValidation: true }) as IdentifiedTodo
)