diff --git a/WorkflowConcurrency/Sources/TaskWorkflow.swift b/WorkflowConcurrency/Sources/TaskWorkflow.swift index eabe012d3..a2c2e60dc 100644 --- a/WorkflowConcurrency/Sources/TaskWorkflow.swift +++ b/WorkflowConcurrency/Sources/TaskWorkflow.swift @@ -21,7 +21,7 @@ import Workflow @available(iOS 13.0, macOS 10.15, *) extension Task: AnyWorkflowConvertible where Failure == Never { public func asAnyWorkflow() -> AnyWorkflow { - TaskWorkflow(taskProvider: { self }).asAnyWorkflow() + TaskWorkflow(task: self).asAnyWorkflow() } } @@ -31,18 +31,17 @@ struct TaskWorkflow: Workflow { public typealias State = Void public typealias Rendering = Void - var taskProvider: () -> Task + private let task: Task - public init(taskProvider: @escaping () -> Task) { - self.taskProvider = taskProvider + public init(task: Task) { + self.task = task } public func render(state: State, context: RenderContext) -> Rendering { let sink = context.makeSink(of: AnyWorkflowAction.self) - context.runSideEffect(key: "") { [taskProvider] lifetime in - let providedTask = taskProvider() - let task = Task { - let output = await providedTask.value + context.runSideEffect(key: "") { [task] lifetime in + let sideEffectTask = Task { + let output = await task.value if Task.isCancelled { return } let action = AnyWorkflowAction(sendingOutput: output) DispatchQueue.main.async { @@ -51,8 +50,8 @@ struct TaskWorkflow: Workflow { } lifetime.onEnded { + sideEffectTask.cancel() task.cancel() - providedTask.cancel() } } } diff --git a/WorkflowConcurrency/Tests/TaskTests.swift b/WorkflowConcurrency/Tests/TaskTests.swift index e9a9476df..3e4cb80a9 100644 --- a/WorkflowConcurrency/Tests/TaskTests.swift +++ b/WorkflowConcurrency/Tests/TaskTests.swift @@ -24,9 +24,9 @@ import XCTest class PublisherTests: XCTestCase { func test_output() { let host = WorkflowHost( - workflow: TaskWorkflow(taskProvider: { + workflow: TaskWorkflow(task: Task { "hello world" } - }) + ) ) let expectation = XCTestExpectation()