From 97296d03a069a0567b5a6350ddd8a92328d524a1 Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Wed, 15 Jun 2022 14:46:28 -0400 Subject: [PATCH] Fixed Swift 5.6+ compiler crash in TaskWorkflow Removing the closure to a Task and just passing in the Task to the TaskWorkflow fixes the compiler crash. --- WorkflowConcurrency/Sources/TaskWorkflow.swift | 17 ++++++++--------- WorkflowConcurrency/Tests/TaskTests.swift | 4 ++-- 2 files changed, 10 insertions(+), 11 deletions(-) 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()