Skip to content

Commit

Permalink
Issue 351: Update input to root workflow in WorkflowHost
Browse files Browse the repository at this point in the history
Resolves #351
  • Loading branch information
Dave Apgar committed Jul 23, 2019
1 parent 65445fb commit b7f9d09
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions swift/Workflow/Sources/WorkflowHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ public final class WorkflowHost<WorkflowType: Workflow> {

}

/// Update the input for the workflow. Will cause a render pass.
public func update(workflow: WorkflowType) {
rootNode.update(workflow: workflow)

// Treat the update as an "output" from the workflow as an external event to force a render pass.
let output = WorkflowNode<WorkflowType>.Output(
outputEvent: nil,
debugInfo: WorkflowUpdateDebugInfo(
workflowType: "\(WorkflowType.self)",
kind: .didUpdate(source: .external)))
handle(output: output)
}

private func handle(output: WorkflowNode<WorkflowType>.Output) {
mutableRendering.value = rootNode.render()

Expand Down
43 changes: 43 additions & 0 deletions swift/Workflow/Tests/WorkflowHostTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import XCTest
import Workflow


final class WorkflowHostTests: XCTestCase {

func test_updatedInputCausesRenderPass() {
let host = WorkflowHost(workflow: TestWorkflow(step: .first))

XCTAssertEqual(1, host.rendering.value)

host.update(workflow: TestWorkflow(step: .second))

XCTAssertEqual(2, host.rendering.value)
}

fileprivate struct TestWorkflow: Workflow {
var step: Step
enum Step {
case first
case second
}

struct State {}
func makeInitialState() -> State {
return State()
}

func workflowDidChange(from previousWorkflow: TestWorkflow, state: inout State) {
}

typealias Rendering = Int

func render(state: State, context: RenderContext<TestWorkflow>) -> Rendering {
switch self.step {
case .first:
return 1
case .second:
return 2
}
}
}
}

0 comments on commit b7f9d09

Please sign in to comment.