From 1ee469ff4f0c31873a9bbfff580099be20221bd1 Mon Sep 17 00:00:00 2001 From: Oliver Foggin Date: Thu, 14 Dec 2023 20:20:28 +0000 Subject: [PATCH] Added ability to access the `send` operation and run custom code --- .../SubscriberReducer.swift | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/Sources/ComposableSubscriber/SubscriberReducer.swift b/Sources/ComposableSubscriber/SubscriberReducer.swift index fd10ab9..d91262c 100644 --- a/Sources/ComposableSubscriber/SubscriberReducer.swift +++ b/Sources/ComposableSubscriber/SubscriberReducer.swift @@ -3,7 +3,21 @@ import SwiftUI extension Reducer { public func subscribe( - to stream: @escaping () async -> AsyncStream, + to stream: @escaping @Sendable () async -> AsyncStream, + on triggerAction: CaseKeyPath, + operation: @escaping @Sendable (_ send: Send, StreamElement) async throws -> Void + ) -> _SubscribeReducer { + .init( + parent: self, + on: triggerAction, + to: stream, + with: .operation(f: operation), + transform: { $0 } + ) + } + + public func subscribe( + to stream: @escaping @Sendable () async -> AsyncStream, on triggerAction: CaseKeyPath, with responseAction: CaseKeyPath, animation: Animation? = nil @@ -12,30 +26,34 @@ extension Reducer { parent: self, on: triggerAction, to: stream, - with: responseAction, - animation: animation, + with: .action(action: AnyCasePath(responseAction), animation: animation), transform: { $0 } ) } public func subscribe( - to stream: @escaping () async -> AsyncStream, + to stream: @escaping @Sendable () async -> AsyncStream, on triggerAction: CaseKeyPath, with responseAction: CaseKeyPath, animation: Animation? = nil, - transform: @escaping (StreamElement) -> Value + transform: @escaping @Sendable (StreamElement) -> Value ) -> _SubscribeReducer { .init( parent: self, on: triggerAction, to: stream, - with: responseAction, - animation: animation, + with: .action(action: AnyCasePath(responseAction), animation: animation), transform: transform ) } } +@usableFromInline +enum Operation { + case action(action: AnyCasePath, animation: Animation?) + case operation(f: (_ send: Send, Value) async throws -> Void) +} + public struct _SubscribeReducer: Reducer { @usableFromInline let parent: Parent @@ -47,28 +65,23 @@ public struct _SubscribeReducer AsyncStream @usableFromInline - let responseAction: AnyCasePath + let operation: Operation @usableFromInline let transform: (StreamElement) -> Value - @usableFromInline - let animation: Animation? - init( parent: Parent, on triggerAction: CaseKeyPath, - to stream: @escaping () async -> AsyncStream, - with responseAction: CaseKeyPath, - animation: Animation?, - transform: @escaping (StreamElement) -> Value + to stream: @escaping @Sendable () async -> AsyncStream, + with operation: Operation, + transform: @escaping @Sendable (StreamElement) -> Value ) { self.parent = parent self.triggerAction = AnyCasePath(triggerAction) self.stream = stream - self.responseAction = AnyCasePath(responseAction) - self.animation = animation self.transform = transform + self.operation = operation } public func reduce(into state: inout Parent.State, action: Parent.Action) -> Effect { @@ -82,7 +95,12 @@ public struct _SubscribeReducer