From b1e1c0fb7046582fe6ffe28b15cc55c364fa9ba6 Mon Sep 17 00:00:00 2001 From: Oliver Foggin Date: Thu, 9 Nov 2023 15:10:17 +0000 Subject: [PATCH] New reducer --- .../AnalyticsReducer.swift | 1 + .../MultipleAnalyticsReducer.swift | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift diff --git a/Sources/ComposableAnalytics/AnalyticsReducer.swift b/Sources/ComposableAnalytics/AnalyticsReducer.swift index f1c6565..ba31635 100644 --- a/Sources/ComposableAnalytics/AnalyticsReducer.swift +++ b/Sources/ComposableAnalytics/AnalyticsReducer.swift @@ -1,6 +1,7 @@ import Foundation import ComposableArchitecture +@available(*, deprecated) public struct AnalyticsReducer: Reducer { @usableFromInline let toAnalyticsData: (State, Action) -> AnalyticsData? diff --git a/Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift b/Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift new file mode 100644 index 0000000..c26cea5 --- /dev/null +++ b/Sources/ComposableAnalytics/MultipleAnalyticsReducer.swift @@ -0,0 +1,34 @@ +import Foundation +import ComposableArchitecture + +@available(*, deprecated) +public struct MultipleAnalyticsReducer: 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 { + guard let analyticsData = toAnalyticsData(state, action) else { + return .none + } + + return .concatenate( + analyticsData.map { data in + .run { _ in analyticsClient.sendAnalytics(data) } + } + ) + } +}