Skip to content

Commit

Permalink
Merge pull request #6 from oliverfoggin/multipleAnalyticsData
Browse files Browse the repository at this point in the history
Multiple analytics reducer.
  • Loading branch information
oliverfoggin authored Nov 9, 2023
2 parents 4c9f8a3 + 26106b3 commit bfc6526
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation
import ComposableArchitecture

public struct MultipleAnalyticsReducer<State, Action>: Reducer {
@usableFromInline
let toAnalyticsData: (State, Action) -> [AnalyticsData]?

@usableFromInline
@Dependency(\.analyticsClient) var analyticsClient

@inlinable
public init(_ toAnalyticsData: @escaping (State, Action) -> [AnalyticsData]?) {
self.init(toAnalyticsData: toAnalyticsData, internal: ())
}

@usableFromInline
init(toAnalyticsData: @escaping (State, Action) -> [AnalyticsData]?, internal: Void) {
self.toAnalyticsData = toAnalyticsData
}

@inlinable
public func reduce(into state: inout State, action: Action) -> Effect<Action> {
guard let analyticsData = toAnalyticsData(state, action) else {
return .none
}

return .concatenate(
analyticsData.map { data in
.run { _ in analyticsClient.sendAnalytics(data) }
}
)
}
}

0 comments on commit bfc6526

Please sign in to comment.