Skip to content

Commit

Permalink
Expose Publisher.asAnyWorkflow as public API (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
amorde authored May 17, 2023
1 parent 1f86835 commit dd9042c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension DemoWorkflow {

// This publisher publishes the current date on a timer that fires every second
func run() -> AnyPublisher<Output, Never> {
Timer.publish(every: 1, on: .main, in: .common)
Timer.publish(every: 2, on: .main, in: .common)
.autoconnect()
.map { Action(publishedDate: $0) }
.eraseToAnyPublisher()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,24 @@ extension DemoWorkflow {
// MARK: Rendering

extension DemoWorkflow {
// TODO: Change this to your actual rendering type
typealias Rendering = DemoScreen

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

// Directly consume a Publisher
Timer.publish(every: 2, on: .main, in: .common)
.autoconnect()
.delay(for: 1.0, scheduler: DispatchQueue.main)
.asAnyWorkflow()
.onOutput { state, output in
state.date = Date()
return nil
}
.rendered(in: context)

dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
let formattedDate = dateFormatter.string(from: state.date)
Expand Down
2 changes: 1 addition & 1 deletion WorkflowCombine/Sources/Publisher+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension Publisher where Failure == Never {
asAnyWorkflow().mapOutput(transform)
}

func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
public func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
PublisherWorkflow(publisher: self).asAnyWorkflow()
}
}
Expand Down
10 changes: 5 additions & 5 deletions WorkflowCombine/Sources/PublisherWorkflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import Foundation
import Workflow

struct PublisherWorkflow<WorkflowPublisher: Publisher>: Workflow where WorkflowPublisher.Failure == Never {
public typealias Output = WorkflowPublisher.Output
public typealias State = Void
public typealias Rendering = Void
typealias Output = WorkflowPublisher.Output
typealias State = Void
typealias Rendering = Void

let publisher: WorkflowPublisher

public init(publisher: WorkflowPublisher) {
init(publisher: WorkflowPublisher) {
self.publisher = publisher
}

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

0 comments on commit dd9042c

Please sign in to comment.