Skip to content

Commit dd9042c

Browse files
authored
Expose Publisher.asAnyWorkflow as public API (#213)
1 parent 1f86835 commit dd9042c

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

Samples/WorkflowCombineSampleApp/WorkflowCombineSampleApp/DemoWorker.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension DemoWorkflow {
1717

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

Samples/WorkflowCombineSampleApp/WorkflowCombineSampleApp/DemoWorkflow.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ extension DemoWorkflow {
4646
// MARK: Rendering
4747

4848
extension DemoWorkflow {
49-
// TODO: Change this to your actual rendering type
5049
typealias Rendering = DemoScreen
5150

5251
func render(state: DemoWorkflow.State, context: RenderContext<DemoWorkflow>) -> Rendering {
52+
// Combine-based worker example
5353
DemoWorker()
5454
.rendered(in: context)
5555

56+
// Directly consume a Publisher
57+
Timer.publish(every: 2, on: .main, in: .common)
58+
.autoconnect()
59+
.delay(for: 1.0, scheduler: DispatchQueue.main)
60+
.asAnyWorkflow()
61+
.onOutput { state, output in
62+
state.date = Date()
63+
return nil
64+
}
65+
.rendered(in: context)
66+
5667
dateFormatter.dateStyle = .long
5768
dateFormatter.timeStyle = .long
5869
let formattedDate = dateFormatter.string(from: state.date)

WorkflowCombine/Sources/Publisher+Extensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension Publisher where Failure == Never {
2525
asAnyWorkflow().mapOutput(transform)
2626
}
2727

28-
func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
28+
public func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
2929
PublisherWorkflow(publisher: self).asAnyWorkflow()
3030
}
3131
}

WorkflowCombine/Sources/PublisherWorkflow.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ import Foundation
2121
import Workflow
2222

2323
struct PublisherWorkflow<WorkflowPublisher: Publisher>: Workflow where WorkflowPublisher.Failure == Never {
24-
public typealias Output = WorkflowPublisher.Output
25-
public typealias State = Void
26-
public typealias Rendering = Void
24+
typealias Output = WorkflowPublisher.Output
25+
typealias State = Void
26+
typealias Rendering = Void
2727

2828
let publisher: WorkflowPublisher
2929

30-
public init(publisher: WorkflowPublisher) {
30+
init(publisher: WorkflowPublisher) {
3131
self.publisher = publisher
3232
}
3333

34-
public func render(state: State, context: RenderContext<Self>) -> Rendering {
34+
func render(state: State, context: RenderContext<Self>) -> Rendering {
3535
let sink = context.makeSink(of: AnyWorkflowAction.self)
3636
context.runSideEffect(key: "") { [publisher] lifetime in
3737
let cancellable = publisher

0 commit comments

Comments
 (0)