Add transform function

This commit is contained in:
Oliver Foggin
2023-12-14 18:56:45 +00:00
parent 9b4d72b6f0
commit b7ac23cb54

View File

@@ -1,21 +1,37 @@
import ComposableArchitecture import ComposableArchitecture
extension Reducer { extension Reducer {
public func subscribe<TriggerAction, T>( public func subscribe<TriggerAction, StreamElement>(
to stream: @escaping () async -> AsyncStream<T>, to stream: @escaping () async -> AsyncStream<StreamElement>,
on triggerAction: CaseKeyPath<Action, TriggerAction>, on triggerAction: CaseKeyPath<Action, TriggerAction>,
with responseAction: CaseKeyPath<Action, T> with responseAction: CaseKeyPath<Action, StreamElement>
) -> _SubscribeReducer<Self, TriggerAction, T> { ) -> _SubscribeReducer<Self, TriggerAction, StreamElement, StreamElement> {
.init( .init(
parent: self, parent: self,
on: triggerAction, on: triggerAction,
to: stream, to: stream,
with: responseAction with: responseAction,
transform: { $0 }
)
}
public func subscribe<TriggerAction, StreamElement, Value>(
to stream: @escaping () async -> AsyncStream<StreamElement>,
on triggerAction: CaseKeyPath<Action, TriggerAction>,
with responseAction: CaseKeyPath<Action, Value>,
transform: @escaping (StreamElement) -> Value
) -> _SubscribeReducer<Self, TriggerAction, StreamElement, Value> {
.init(
parent: self,
on: triggerAction,
to: stream,
with: responseAction,
transform: transform
) )
} }
} }
public struct _SubscribeReducer<Parent: Reducer, TriggerAction, T>: Reducer { public struct _SubscribeReducer<Parent: Reducer, TriggerAction, StreamElement, Value>: Reducer {
@usableFromInline @usableFromInline
let parent: Parent let parent: Parent
@@ -23,21 +39,26 @@ public struct _SubscribeReducer<Parent: Reducer, TriggerAction, T>: Reducer {
let triggerAction: AnyCasePath<Parent.Action, TriggerAction> let triggerAction: AnyCasePath<Parent.Action, TriggerAction>
@usableFromInline @usableFromInline
let stream: () async -> AsyncStream<T> let stream: () async -> AsyncStream<StreamElement>
@usableFromInline @usableFromInline
let responseAction: AnyCasePath<Parent.Action, T> let responseAction: AnyCasePath<Parent.Action, Value>
@usableFromInline
let transform: (StreamElement) -> Value
init( init(
parent: Parent, parent: Parent,
on triggerAction: CaseKeyPath<Parent.Action, TriggerAction>, on triggerAction: CaseKeyPath<Parent.Action, TriggerAction>,
to stream: @escaping () async -> AsyncStream<T>, to stream: @escaping () async -> AsyncStream<StreamElement>,
with responseAction: CaseKeyPath<Parent.Action, T> with responseAction: CaseKeyPath<Parent.Action, Value>,
transform: @escaping (StreamElement) -> Value
) { ) {
self.parent = parent self.parent = parent
self.triggerAction = AnyCasePath(triggerAction) self.triggerAction = AnyCasePath(triggerAction)
self.stream = stream self.stream = stream
self.responseAction = AnyCasePath(responseAction) self.responseAction = AnyCasePath(responseAction)
self.transform = transform
} }
public func reduce(into state: inout Parent.State, action: Parent.Action) -> Effect<Parent.Action> { public func reduce(into state: inout Parent.State, action: Parent.Action) -> Effect<Parent.Action> {
@@ -51,7 +72,7 @@ public struct _SubscribeReducer<Parent: Reducer, TriggerAction, T>: Reducer {
effects, effects,
.run { send in .run { send in
for await value in await stream() { for await value in await stream() {
await send(responseAction.embed(value)) await send(responseAction.embed(transform(value)))
} }
} }
) )