Skip to content

Commit 6040a6a

Browse files
committed
feat: Todo 수정 및 삭제를 감지하는 버스 구현
1 parent d98d39f commit 6040a6a

4 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// TodoMutationEventBusImpl.swift
3+
// DevLogData
4+
//
5+
// Created by opfic on 6/6/26.
6+
//
7+
8+
import Foundation
9+
import DevLogDomain
10+
11+
actor TodoMutationEventBusImpl: TodoMutationEventBus {
12+
private var continuations = [UUID: AsyncStream<TodoMutationEvent>.Continuation]()
13+
14+
func publish(_ event: TodoMutationEvent) async {
15+
continuations.values.forEach { $0.yield(event) }
16+
}
17+
18+
func events() -> AsyncStream<TodoMutationEvent> {
19+
let id = UUID()
20+
let (stream, continuation) = AsyncStream.makeStream(of: TodoMutationEvent.self)
21+
22+
continuations[id] = continuation
23+
continuation.onTermination = { [weak self] _ in
24+
Task {
25+
await self?.removeContinuation(id: id)
26+
}
27+
}
28+
29+
return stream
30+
}
31+
}
32+
33+
private extension TodoMutationEventBusImpl {
34+
func removeContinuation(id: UUID) {
35+
continuations[id] = nil
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// TodoMutationEventBusImplTests.swift
3+
// DevLogDataTests
4+
//
5+
// Created by opfic on 6/6/26.
6+
//
7+
8+
import Testing
9+
import DevLogDomain
10+
@testable import DevLogData
11+
12+
struct TodoMutationEventBusImplTests {
13+
@Test("TodoMutationEventBus는 발행된 이벤트를 관찰자에게 전달한다")
14+
func todoMutationEventBus는_발행된_이벤트를_관찰자에게_전달한다() async {
15+
let bus = TodoMutationEventBusImpl()
16+
let events = await bus.events()
17+
let task = Task {
18+
var iterator = events.makeAsyncIterator()
19+
return await iterator.next()
20+
}
21+
22+
await bus.publish(.updated("todo-id"))
23+
24+
let event = await task.value
25+
#expect(event == .updated("todo-id"))
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// TodoMutationEvent.swift
3+
// DevLogDomain
4+
//
5+
// Created by opfic on 6/6/26.
6+
//
7+
8+
public enum TodoMutationEvent: Equatable, Sendable {
9+
case updated(String)
10+
case deleted(String)
11+
case restored(String)
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// TodoMutationEventBus.swift
3+
// DevLogDomain
4+
//
5+
// Created by opfic on 6/6/26.
6+
//
7+
8+
public protocol TodoMutationEventBus: Sendable {
9+
func publish(_ event: TodoMutationEvent) async
10+
func events() async -> AsyncStream<TodoMutationEvent>
11+
}

0 commit comments

Comments
 (0)