Skip to content

Commit

Permalink
New reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoggin committed Nov 9, 2023
1 parent 4c9f8a3 commit b1e1c0f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/ComposableAnalytics/AnalyticsReducer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import ComposableArchitecture

@available(*, deprecated)
public struct AnalyticsReducer<State, Action>: Reducer {
@usableFromInline
let toAnalyticsData: (State, Action) -> AnalyticsData?
Expand Down
34 changes: 34 additions & 0 deletions Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation
import ComposableArchitecture

@available(*, deprecated)
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 b1e1c0f

Please sign in to comment.