Skip to content

Commit c60f6c6

Browse files
committed
feat: TodoMutationEvent 방출
1 parent 6040a6a commit c60f6c6

3 files changed

Lines changed: 50 additions & 11 deletions

File tree

Application/DevLogData/Sources/DataAssembler.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ public final class DataAssembler: Assembler {
3232
)
3333
}
3434

35+
container.register(TodoMutationEventBus.self) {
36+
TodoMutationEventBusImpl()
37+
}
38+
3539
container.register(TodoRepository.self) {
3640
TodoRepositoryImpl(
3741
todoService: container.resolve(TodoService.self),
3842
todoCategoryService: container.resolve(TodoCategoryService.self),
39-
widgetSyncEventBus: container.resolve(WidgetSyncEventBus.self)
43+
widgetSyncEventBus: container.resolve(WidgetSyncEventBus.self),
44+
todoMutationEventBus: container.resolve(TodoMutationEventBus.self)
4045
)
4146
}
4247

Application/DevLogData/Sources/Repository/TodoRepositoryImpl.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ final class TodoRepositoryImpl: TodoRepository {
1313
private let todoService: TodoService
1414
private let todoCategoryService: TodoCategoryService
1515
private let widgetSyncEventBus: WidgetSyncEventBus
16+
private let todoMutationEventBus: TodoMutationEventBus
1617

1718
init(
1819
todoService: TodoService,
1920
todoCategoryService: TodoCategoryService,
20-
widgetSyncEventBus: WidgetSyncEventBus
21+
widgetSyncEventBus: WidgetSyncEventBus,
22+
todoMutationEventBus: TodoMutationEventBus
2123
) {
2224
self.todoService = todoService
2325
self.todoCategoryService = todoCategoryService
2426
self.widgetSyncEventBus = widgetSyncEventBus
27+
self.todoMutationEventBus = todoMutationEventBus
2528
}
2629

2730
func fetchTodos(_ query: TodoQuery, cursor: TodoCursor?) async throws -> TodoPage {
@@ -107,6 +110,7 @@ final class TodoRepositoryImpl: TodoRepository {
107110
func upsertTodo(_ todo: Todo) async throws {
108111
let todoRequest = TodoRequest.fromDomain(todo)
109112
try await upsertTodo(todoRequest)
113+
await todoMutationEventBus.publish(.updated(todo.id))
110114
}
111115

112116
func upsertTodo(_ todoDraft: TodoDraft) async throws {
@@ -127,6 +131,7 @@ final class TodoRepositoryImpl: TodoRepository {
127131
do {
128132
try await todoService.deleteTodo(todoId: todoId)
129133
widgetSyncEventBus.publish(.syncRequested)
134+
await todoMutationEventBus.publish(.deleted(todoId))
130135
} catch {
131136
throw error.toDomain()
132137
}
@@ -136,6 +141,7 @@ final class TodoRepositoryImpl: TodoRepository {
136141
do {
137142
try await todoService.undoDeleteTodo(todoId: todoId)
138143
widgetSyncEventBus.publish(.syncRequested)
144+
await todoMutationEventBus.publish(.restored(todoId))
139145
} catch {
140146
throw error.toDomain()
141147
}

Application/DevLogData/Tests/Repository/TodoRepositoryImplTests.swift

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import Foundation
1010
import Testing
1111
import DevLogCore
1212
import DevLogDomain
13-
@testable import DevLogData
13+
@testable @preconcurrency import DevLogData
1414

1515
struct TodoRepositoryImplTests {
16-
@Test("Todo 변경 성공 시 위젯 동기화 이벤트를 발행한다")
17-
func todo_변경_성공_시_위젯_동기화_이벤트를_발행한다() async throws {
16+
@Test("Todo 변경 성공 시 위젯 동기화와 mutation 이벤트를 발행한다")
17+
func todo_변경_성공_시_위젯_동기화와_mutation_이벤트를_발행한다() async throws {
1818
let fixture = makeFixture()
1919
let todo = makeTodo()
2020

@@ -24,10 +24,13 @@ struct TodoRepositoryImplTests {
2424

2525
let events = fixture.widgetSyncEventBus.events
2626
#expect(events == [.syncRequested, .syncRequested, .syncRequested])
27+
28+
let mutationEvents = await fixture.todoMutationEventBus.publishedEvents()
29+
#expect(mutationEvents == [.updated(todo.id), .deleted(todo.id), .restored(todo.id)])
2730
}
2831

29-
@Test("Todo 변경 실패 시 위젯 동기화 이벤트를 발행하지 않는다")
30-
func todo_변경_실패_시_위젯_동기화_이벤트를_발행하지_않는다() async throws {
32+
@Test("Todo 변경 실패 시 위젯 동기화와 mutation 이벤트를 발행하지 않는다")
33+
func todo_변경_실패_시_위젯_동기화와_mutation_이벤트를_발행하지_않는다() async throws {
3134
let fixture = makeFixture()
3235
let todo = makeTodo()
3336

@@ -52,24 +55,30 @@ struct TodoRepositoryImplTests {
5255
#expect(error as? TodoRepositoryImplTestsError == .serviceFailed)
5356
}
5457

55-
let events = fixture.widgetSyncEventBus.events
56-
#expect(events.isEmpty)
58+
let syncEvents = fixture.widgetSyncEventBus.events
59+
#expect(syncEvents.isEmpty)
60+
61+
let mutationEvents = await fixture.todoMutationEventBus.publishedEvents()
62+
#expect(mutationEvents.isEmpty)
5763
}
5864

5965
private func makeFixture() -> Fixture {
6066
let todoService = TodoServiceSpy()
6167
let todoCategoryService = TodoCategoryServiceSpy()
6268
let widgetSyncEventBus = WidgetSyncEventBusSpy()
69+
let todoMutationEventBus = TodoMutationEventBusSpy()
6370
let repository = TodoRepositoryImpl(
6471
todoService: todoService,
6572
todoCategoryService: todoCategoryService,
66-
widgetSyncEventBus: widgetSyncEventBus
73+
widgetSyncEventBus: widgetSyncEventBus,
74+
todoMutationEventBus: todoMutationEventBus
6775
)
6876

6977
return Fixture(
7078
repository: repository,
7179
todoService: todoService,
72-
widgetSyncEventBus: widgetSyncEventBus
80+
widgetSyncEventBus: widgetSyncEventBus,
81+
todoMutationEventBus: todoMutationEventBus
7382
)
7483
}
7584

@@ -97,6 +106,7 @@ private struct Fixture {
97106
let repository: TodoRepositoryImpl
98107
let todoService: TodoServiceSpy
99108
let widgetSyncEventBus: WidgetSyncEventBusSpy
109+
let todoMutationEventBus: TodoMutationEventBusSpy
100110
}
101111

102112
private actor TodoServiceSpy: TodoService {
@@ -159,6 +169,24 @@ private final class WidgetSyncEventBusSpy: WidgetSyncEventBus {
159169
}
160170
}
161171

172+
private actor TodoMutationEventBusSpy: TodoMutationEventBus {
173+
private var capturedEvents = [TodoMutationEvent]()
174+
175+
func publish(_ event: TodoMutationEvent) async {
176+
capturedEvents.append(event)
177+
}
178+
179+
func publishedEvents() -> [TodoMutationEvent] {
180+
capturedEvents
181+
}
182+
183+
func events() async -> AsyncStream<TodoMutationEvent> {
184+
AsyncStream { continuation in
185+
continuation.finish()
186+
}
187+
}
188+
}
189+
162190
private enum TodoRepositoryImplTestsError: Error, Equatable {
163191
case serviceFailed
164192
case unexpectedCall

0 commit comments

Comments
 (0)