Skip to content

Commit 3968bb1

Browse files
committed
fix: Auth 세션 관찰 흐름 단일화
1 parent b80bc92 commit 3968bb1

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

Application/DevLogData/Sources/Repository/AuthSessionRepositoryImpl.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class AuthSessionRepositoryImpl: AuthSessionRepository {
1717
private let todoCategoryService: TodoCategoryService
1818
private let store: MemoryCacheStore
1919
private let provider: AuthSessionStateProvider
20+
private var cancellables = Set<AnyCancellable>()
2021

2122
init(
2223
authService: AuthService,
@@ -28,30 +29,34 @@ final class AuthSessionRepositoryImpl: AuthSessionRepository {
2829
self.todoCategoryService = todoCategoryService
2930
self.store = store
3031
self.provider = provider
32+
33+
setupObservation()
3134
}
3235

3336
func observeSignedIn() -> AnyPublisher<Bool, Never> {
37+
provider.observeSignedIn()
38+
}
39+
}
40+
41+
private extension AuthSessionRepositoryImpl {
42+
func setupObservation() {
3443
authService.observeSignedIn()
3544
.removeDuplicates()
36-
.map { [self] isSignedIn in
37-
Future { promise in
38-
Task {
39-
if isSignedIn {
40-
await self.cachePreferencesIfNeeded()
41-
} else {
42-
self.clearPreferencesCache()
43-
}
44-
self.provider.publish(isSignedIn)
45-
promise(.success(isSignedIn))
45+
.sink { [weak self] isSignedIn in
46+
Task { [weak self] in
47+
guard let self else { return }
48+
49+
if isSignedIn {
50+
await self.cachePreferencesIfNeeded()
51+
} else {
52+
self.clearPreferencesCache()
4653
}
54+
self.provider.publish(isSignedIn)
4755
}
4856
}
49-
.switchToLatest()
50-
.eraseToAnyPublisher()
57+
.store(in: &cancellables)
5158
}
52-
}
5359

54-
private extension AuthSessionRepositoryImpl {
5560
func cachePreferencesIfNeeded() async {
5661
if store.value(forKey: Key.preferences) as [TodoCategoryPreferenceResponse]? != nil {
5762
return

Application/DevLogData/Tests/Repository/AuthSessionRepositoryImplTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct AuthSessionRepositoryImplTests {
4040
#expect(await valueTask.value)
4141
#expect(store.value(forKey: Key.preferences) == [preference])
4242
#expect(await todoCategoryService.fetchCategoryPreferencesCallCount() == 1)
43-
#expect(provider.events == [true])
43+
#expect(provider.events.contains(true))
4444
}
4545

4646
@Test("로그아웃 세션은 category preference 캐시를 제거한다")
@@ -68,7 +68,7 @@ struct AuthSessionRepositoryImplTests {
6868

6969
#expect(await valueTask.value == false)
7070
#expect(store.value(forKey: Key.preferences) == Optional<[TodoCategoryPreferenceResponse]>.none)
71-
#expect(provider.events == [false])
71+
#expect(provider.events.contains(false))
7272
}
7373

7474
private func makePreferenceResponse() -> TodoCategoryPreferenceResponse {
@@ -86,14 +86,16 @@ struct AuthSessionRepositoryImplTests {
8686
}
8787

8888
private final class AuthSessionStateProviderSpy: AuthSessionStateProvider {
89+
private let subject = PassthroughSubject<Bool, Never>()
8990
private(set) var events = [Bool]()
9091

9192
func publish(_ isSignedIn: Bool) {
9293
events.append(isSignedIn)
94+
subject.send(isSignedIn)
9395
}
9496

9597
func observeSignedIn() -> AnyPublisher<Bool, Never> {
96-
Empty().eraseToAnyPublisher()
98+
subject.eraseToAnyPublisher()
9799
}
98100
}
99101

0 commit comments

Comments
 (0)