Skip to content

Commit 07fd80a

Browse files
committed
Remove the need to type-erase publisher to render the workflow view
1 parent 2b727df commit 07fd80a

File tree

9 files changed

+51
-55
lines changed

9 files changed

+51
-55
lines changed

Diff for: Development.podspec

-22
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Pod::Spec.new do |s|
1313
s.dependency 'WorkflowUI'
1414
s.dependency 'WorkflowReactiveSwift'
1515
s.dependency 'WorkflowRxSwift'
16-
# s.dependency 'WorkflowCombine' # TODO: Disabled because app specs cannot increase the deployment target of the root
1716
s.source_files = 'Samples/Dummy.swift'
1817

1918
s.subspec 'Dummy' do |ss|
@@ -142,25 +141,4 @@ Pod::Spec.new do |s|
142141
test_spec.dependency 'WorkflowTesting'
143142
test_spec.dependency 'WorkflowRxSwiftTesting'
144143
end
145-
146-
# TODO: Disabled because app specs cannot increase the deployment target of the root
147-
# To use, increase the deployment target of this spec to 13.0 or higher
148-
# s.app_spec 'WorkflowCombineSampleApp' do |app_spec|
149-
# app_spec.source_files = 'Samples/WorkflowCombineSampleApp/WorkflowCombineSampleApp/**/*.swift'
150-
# end
151-
152-
# s.test_spec 'WorkflowCombineSampleAppTests' do |test_spec|
153-
# test_spec.dependency 'Development/WorkflowCombineSampleApp'
154-
# test_spec.requires_app_host = true
155-
# test_spec.app_host_name = 'Development/WorkflowCombineSampleApp'
156-
# test_spec.source_files = 'Samples/WorkflowCombineSampleApp/WorkflowCombineSampleAppUnitTests/**/*.swift'
157-
# end
158-
#
159-
# s.test_spec 'WorkflowCombineTests' do |test_spec|
160-
# test_spec.requires_app_host = true
161-
# test_spec.source_files = 'WorkflowCombine/Tests/**/*.swift'
162-
# test_spec.framework = 'XCTest'
163-
# test_spec.dependency 'WorkflowTesting'
164-
# test_spec.dependency 'WorkflowCombineTesting'
165-
# end
166144
end

Diff for: Samples/WorkflowCombineSampleApp/WorkflowCombineSampleApp/DemoWorker.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import Combine
99
import Workflow
1010
import WorkflowCombine
11-
import WorkflowUI
1211

1312
// MARK: Workers
1413

@@ -17,7 +16,7 @@ extension DemoWorkflow {
1716
typealias Output = Action
1817

1918
// This publisher publishes the current date on a timer that fires every second
20-
func run() -> AnyPublisher<Action, Never> {
19+
func run() -> AnyPublisher<Output, Never> {
2120
Timer.publish(every: 1, on: .main, in: .common)
2221
.autoconnect()
2322
.map { Action(publishedDate: $0) }

Diff for: Samples/WorkflowCombineSampleApp/WorkflowCombineSampleAppUnitTests/DemoWorkflowTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import XCTest
1212
@testable import Development_WorkflowCombineSampleApp
1313

1414
class DemoWorkflowTests: XCTestCase {
15-
func test_workflowIsRenderedEverySecondForFiveSeconds() {
15+
func test_demoWorkflow_publishesNewDate() {
1616
let expectedDate = Date(timeIntervalSince1970: 0)
1717

1818
DemoWorkflow

Diff for: WorkflowCombine/Sources/Publisher+Extensions.swift

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Publisher+Extensions.swift
3+
// WorkflowCombine
4+
//
5+
// Created by Soo Rin Park on 11/3/21.
6+
//
7+
8+
#if canImport(Combine) && swift(>=5.1)
9+
10+
import Combine
11+
import Foundation
12+
import Workflow
13+
14+
@available(iOS 13.0, macOS 10.15, *)
15+
/// This is a workaround to the fact you extensions of protocols cannot have an inheritance clause.
16+
/// a previous solution had extending the `AnyPublisher` to conform to `AnyWorkflowConvertible`,
17+
/// but was limited in the fact that rendering was only available to `AnyPublisher`s.
18+
/// this solutions makes it so that all publishers can render its view.
19+
extension Publisher where Failure == Never {
20+
public func running<Parent>(in context: RenderContext<Parent>, key: String = "") where
21+
Output == AnyWorkflowAction<Parent> {
22+
asAnyWorkflow().rendered(in: context, key: key, outputMap: { $0 })
23+
}
24+
25+
public func mapOutput<NewOutput>(_ transform: @escaping (Output) -> NewOutput) -> AnyWorkflow<Void, NewOutput> {
26+
asAnyWorkflow().mapOutput(transform)
27+
}
28+
29+
func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
30+
PublisherWorkflow(publisher: self).asAnyWorkflow()
31+
}
32+
}
33+
34+
#endif

Diff for: WorkflowCombine/Sources/PublisherWorkflow.swift

-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
import Foundation
2121
import Workflow
2222

23-
@available(iOS 13.0, macOS 10.15, *)
24-
25-
extension AnyPublisher: AnyWorkflowConvertible where Failure == Never {
26-
public func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
27-
return PublisherWorkflow(publisher: self).asAnyWorkflow()
28-
}
29-
}
30-
3123
@available(iOS 13.0, macOS 10.15, *)
3224
struct PublisherWorkflow<WorkflowPublisher: Publisher>: Workflow where WorkflowPublisher.Failure == Never {
3325
public typealias Output = WorkflowPublisher.Output

Diff for: WorkflowCombine/Sources/Worker.swift

-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
)
8989
.map { AnyWorkflowAction<Self>(sendingOutput: $0) }
9090
}
91-
.eraseToAnyPublisher()
9291
.running(in: context, key: state.uuidString)
9392
}
9493
}

Diff for: WorkflowCombine/Tests/Helpers/AnyPublisherTesting.swift renamed to WorkflowCombine/Tests/Helpers/PublisherTesting.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
/// - Parameters:
3131
/// - producingOutput: An output that should be returned when this worker is requested, if any.
3232
/// - key: Key to expect this `Workflow` to be rendered with.
33-
public func expectPublisher<OutputType>(
34-
producingOutput output: OutputType? = nil,
35-
key: String = "",
36-
file: StaticString = #file, line: UInt = #line
37-
) -> RenderTester<WorkflowType> {
33+
public func expectPublisher<PublisherType: Publisher>(
34+
publisher: PublisherType.Type,
35+
output: PublisherType.Output,
36+
key: String = ""
37+
) -> RenderTester<WorkflowType> where PublisherType.Failure == Never {
3838
expectWorkflow(
39-
type: PublisherWorkflow<AnyPublisher<OutputType, Never>>.self,
39+
type: PublisherWorkflow<PublisherType>.self,
4040
key: key,
4141
producingRendering: (),
4242
producingOutput: output,

Diff for: WorkflowCombine/Tests/AnyPublisherTests.swift renamed to WorkflowCombine/Tests/PublisherTests.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import Workflow
2020
import XCTest
2121
@testable import WorkflowCombine
2222

23-
class AnyPublisherTests: XCTestCase {
23+
class PublisherTests: XCTestCase {
2424
func testPublisherWorkflow() {
2525
TestWorkflow()
2626
.renderTester()
27-
.expectPublisher(producingOutput: 1, key: "123")
27+
.expectPublisher(publisher: Publishers.Sequence<[Int], Never>.self, output: 1, key: "123")
2828
.render {}
2929
}
3030

@@ -34,22 +34,21 @@ class AnyPublisherTests: XCTestCase {
3434

3535
func render(state: State, context: RenderContext<Self>) -> Rendering {
3636
[1].publisher
37-
.eraseToAnyPublisher()
3837
.mapOutput { _ in AnyWorkflowAction<TestWorkflow>.noAction }
3938
.running(in: context, key: "123")
4039
}
4140
}
4241

4342
func test_publisherWorkflow_usesSideEffectWithKey() {
44-
let publisher = Just(1).eraseToAnyPublisher()
43+
let publisher = Just(1)
4544
PublisherWorkflow(publisher: publisher)
4645
.renderTester()
4746
.expectSideEffect(key: "")
4847
.render { _ in }
4948
}
5049

5150
func test_output() {
52-
let publisher = Just(1).eraseToAnyPublisher()
51+
let publisher = Just(1)
5352

5453
let host = WorkflowHost(
5554
workflow: PublisherWorkflow(publisher: publisher)
@@ -72,7 +71,7 @@ class AnyPublisherTests: XCTestCase {
7271
let publisher = [1, 2, 3].publisher
7372

7473
let host = WorkflowHost(
75-
workflow: PublisherWorkflow(publisher: publisher.eraseToAnyPublisher())
74+
workflow: PublisherWorkflow(publisher: publisher)
7675
)
7776

7877
let expectation = XCTestExpectation()

Diff for: WorkflowCombine/Tests/WorkerTests.swift

+4-9
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ class WorkerTests: XCTestCase {
136136
}
137137

138138
struct TestWorker: Worker {
139-
typealias Output = WorkerPublisher.Output
140-
typealias WorkerPublisher = AnyPublisher<Int, Never>
139+
typealias Output = Int
141140

142141
func isEquivalent(to otherWorker: TestWorker) -> Bool {
143142
true
144143
}
145144

146-
func run() -> WorkerPublisher {
145+
func run() -> AnyPublisher<Int, Never> {
147146
[1, 2].publisher
148147
.delay(for: .milliseconds(1), scheduler: RunLoop.main)
149148
.eraseToAnyPublisher()
@@ -194,11 +193,7 @@ private struct PublisherTestWorkflow: Workflow {
194193

195194
private struct PublisherTestWorker: Worker {
196195
typealias Output = Int
197-
func run() -> AnyPublisher<Int, Never> {
198-
Just(1).eraseToAnyPublisher()
199-
}
196+
func run() -> Just<Int> { Just(1) }
200197

201-
func isEquivalent(to otherWorker: PublisherTestWorker) -> Bool {
202-
true
203-
}
198+
func isEquivalent(to otherWorker: PublisherTestWorker) -> Bool { true }
204199
}

0 commit comments

Comments
 (0)