Skip to content

Commit 2b727df

Browse files
committed
Fix Workflows to take a generic publisher type
1 parent 2057460 commit 2b727df

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

Diff for: Samples/WorkflowCombineSampleApp/WorkflowCombineSampleApp/DemoWorker.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ extension DemoWorkflow {
1717
typealias Output = Action
1818

1919
// This publisher publishes the current date on a timer that fires every second
20-
func run() -> AnyPublisher<DemoWorkflow.Action, Never> {
20+
func run() -> AnyPublisher<Action, Never> {
2121
Timer.publish(every: 1, on: .main, in: .common)
2222
.autoconnect()
23-
.map { .init(publishedDate: $0) }
23+
.map { Action(publishedDate: $0) }
2424
.eraseToAnyPublisher()
2525
}
2626

Diff for: WorkflowCombine/Sources/PublisherWorkflow.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@
2121
import Workflow
2222

2323
@available(iOS 13.0, macOS 10.15, *)
24+
2425
extension AnyPublisher: AnyWorkflowConvertible where Failure == Never {
2526
public func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
2627
return PublisherWorkflow(publisher: self).asAnyWorkflow()
2728
}
2829
}
2930

3031
@available(iOS 13.0, macOS 10.15, *)
31-
struct PublisherWorkflow<Value>: Workflow {
32-
public typealias Output = Value
32+
struct PublisherWorkflow<WorkflowPublisher: Publisher>: Workflow where WorkflowPublisher.Failure == Never {
33+
public typealias Output = WorkflowPublisher.Output
3334
public typealias State = Void
3435
public typealias Rendering = Void
3536

36-
let publisher: AnyPublisher<Output, Never>
37+
let publisher: WorkflowPublisher
3738

38-
public init(publisher: AnyPublisher<Output, Never>) {
39+
public init(publisher: WorkflowPublisher) {
3940
self.publisher = publisher
4041
}
4142

Diff for: WorkflowCombine/Sources/Worker.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@
3232
public protocol Worker: AnyWorkflowConvertible where Rendering == Void {
3333
/// The type of output events returned by this worker.
3434
associatedtype Output
35+
associatedtype WorkerPublisher: Publisher where
36+
WorkerPublisher.Output == Output, WorkerPublisher.Failure == Never
3537

3638
/// Returns a publisher to execute the work represented by this worker.
37-
func run() -> AnyPublisher<Output, Never>
38-
39+
func run() -> WorkerPublisher
3940
/// Returns `true` if the other worker should be considered equivalent to `self`. Equivalence should take into
4041
/// account whatever data is meaningful to the task. For example, a worker that loads a user account from a server
4142
/// would not be equivalent to another worker with a different user ID.
@@ -85,7 +86,7 @@
8586
logger.logFinished(status: "Cancelled")
8687
}
8788
)
88-
.map { AnyWorkflowAction(sendingOutput: $0) }
89+
.map { AnyWorkflowAction<Self>(sendingOutput: $0) }
8990
}
9091
.eraseToAnyPublisher()
9192
.running(in: context, key: state.uuidString)

Diff for: WorkflowCombine/Tests/Helpers/AnyPublisherTesting.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#if DEBUG
1818

19+
import Combine
1920
import Workflow
2021
import WorkflowTesting
2122
import XCTest
@@ -35,7 +36,7 @@
3536
file: StaticString = #file, line: UInt = #line
3637
) -> RenderTester<WorkflowType> {
3738
expectWorkflow(
38-
type: PublisherWorkflow<OutputType>.self,
39+
type: PublisherWorkflow<AnyPublisher<OutputType, Never>>.self,
3940
key: key,
4041
producingRendering: (),
4142
producingOutput: output,

Diff for: WorkflowCombine/Tests/TestingTests.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,13 @@ private struct TestWorkflow: Workflow {
183183
}
184184

185185
private struct TestWorker: Worker {
186+
typealias Output = String
187+
typealias WorkerPublisher = Just<Output>
188+
186189
let input: String
187190

188-
func run() -> AnyPublisher<String, Never> {
189-
Just("").eraseToAnyPublisher()
191+
func run() -> WorkerPublisher {
192+
Just("")
190193
}
191194

192195
func isEquivalent(to otherWorker: TestWorker) -> Bool {

Diff for: WorkflowCombine/Tests/WorkerTests.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,14 @@ class WorkerTests: XCTestCase {
136136
}
137137

138138
struct TestWorker: Worker {
139+
typealias Output = WorkerPublisher.Output
140+
typealias WorkerPublisher = AnyPublisher<Int, Never>
141+
139142
func isEquivalent(to otherWorker: TestWorker) -> Bool {
140143
true
141144
}
142145

143-
func run() -> AnyPublisher<Int, Never> {
146+
func run() -> WorkerPublisher {
144147
[1, 2].publisher
145148
.delay(for: .milliseconds(1), scheduler: RunLoop.main)
146149
.eraseToAnyPublisher()

0 commit comments

Comments
 (0)