-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from oliverfoggin/multipleAnalyticsData
Multiple analytics reducer.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
) | ||
} | ||
} |