Skip to content

Commit f389204

Browse files
committed
refactor(GlanceService): use pipeline pattern for loadData
- Extract executeFetch() to isolate I/O and logging - Simplify loadData to single-expression pipeline: fetch → compose - Move all logging into executeFetch where I/O happens - Use tuple destructuring for cleaner map closure
1 parent 20f55a0 commit f389204

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

Packages/ClaudeUsage/Sources/Glance/Data/GlanceService.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,29 @@ public actor GlanceService {
5454
/// Loads glance data. Returns nil if a load is already in progress.
5555
public func loadData(invalidateCache: Bool = true) async -> Result<GlanceData, Error>? {
5656
guard !isLoading else {
57-
logger.debug("GlanceService: loadData skipped - already loading")
57+
logger.debug("loadData: skipped - already loading")
5858
return nil
5959
}
60-
6160
isLoading = true
6261
defer { isLoading = false }
6362

64-
logger.info("GlanceService: loadData started (invalidateCache: \(invalidateCache))")
65-
66-
do {
67-
if invalidateCache {
68-
await clearCache()
69-
}
70-
71-
async let costTask = todayCostProvider.getTodayCost()
72-
async let sessionTask = sessionProvider.getActiveSession()
63+
if invalidateCache { await clearCache() }
7364

74-
let todayCost = try await costTask
75-
let activeSession = await sessionTask
65+
// Pipeline: fetch → compose
66+
return await executeFetch()
67+
.map { cost, session in GlanceData(todayCost: cost, activeSession: session) }
68+
}
7669

77-
logger.info("GlanceService: loadData completed - cost: \(todayCost.total)")
78-
return .success(GlanceData(todayCost: todayCost, activeSession: activeSession))
70+
private func executeFetch() async -> Result<(TodayCost, UsageSession?), Error> {
71+
logger.info("loadData: fetching")
72+
do {
73+
async let cost = todayCostProvider.getTodayCost()
74+
async let session = sessionProvider.getActiveSession()
75+
let result = (try await cost, await session)
76+
logger.info("loadData: success - cost: \(result.0.total)")
77+
return .success(result)
7978
} catch {
80-
logger.error("GlanceService: loadData failed - \(error.localizedDescription)")
79+
logger.error("loadData: failed - \(error.localizedDescription)")
8180
return .failure(error)
8281
}
8382
}

0 commit comments

Comments
 (0)