Skip to content

[chore]: update swiftformat ifdef indent rule to no-indent #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
--fragment false
--hexgrouping none
--hexliteralcase uppercase
--ifdef indent
--ifdef no-indent
--indentcase false
--linebreaks lf
--maxwidth none
Expand All @@ -54,4 +54,4 @@
--wrapparameters preserve
--xcodeindentation disabled

--disable unusedArguments
--disable unusedArguments
2 changes: 1 addition & 1 deletion Workflow/Tests/WorkflowNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,5 @@ private struct StateTransitioningWorkflow: Workflow {
#if compiler(>=5.0)
// Never gains Equatable and Hashable conformance in Swift 5
#else
extension Never: Equatable {}
extension Never: Equatable {}
#endif
38 changes: 19 additions & 19 deletions WorkflowCombine/Sources/Publisher+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@

#if canImport(Combine) && swift(>=5.1)

import Combine
import Foundation
import Workflow
import Combine
import Foundation
import Workflow

@available(iOS 13.0, macOS 10.15, *)
/// This is a workaround to the fact you extensions of protocols cannot have an inheritance clause.
/// a previous solution had extending the `AnyPublisher` to conform to `AnyWorkflowConvertible`,
/// but was limited in the fact that rendering was only available to `AnyPublisher`s.
/// this solutions makes it so that all publishers can render its view.
extension Publisher where Failure == Never {
public func running<Parent>(in context: RenderContext<Parent>, key: String = "") where
Output == AnyWorkflowAction<Parent> {
asAnyWorkflow().rendered(in: context, key: key, outputMap: { $0 })
}
@available(iOS 13.0, macOS 10.15, *)
/// This is a workaround to the fact you extensions of protocols cannot have an inheritance clause.
/// a previous solution had extending the `AnyPublisher` to conform to `AnyWorkflowConvertible`,
/// but was limited in the fact that rendering was only available to `AnyPublisher`s.
/// this solutions makes it so that all publishers can render its view.
extension Publisher where Failure == Never {
public func running<Parent>(in context: RenderContext<Parent>, key: String = "") where
Output == AnyWorkflowAction<Parent> {
asAnyWorkflow().rendered(in: context, key: key, outputMap: { $0 })
}

public func mapOutput<NewOutput>(_ transform: @escaping (Output) -> NewOutput) -> AnyWorkflow<Void, NewOutput> {
asAnyWorkflow().mapOutput(transform)
}
public func mapOutput<NewOutput>(_ transform: @escaping (Output) -> NewOutput) -> AnyWorkflow<Void, NewOutput> {
asAnyWorkflow().mapOutput(transform)
}

func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
PublisherWorkflow(publisher: self).asAnyWorkflow()
}
func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
PublisherWorkflow(publisher: self).asAnyWorkflow()
}
}

#endif
46 changes: 23 additions & 23 deletions WorkflowCombine/Sources/PublisherWorkflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@

#if canImport(Combine) && swift(>=5.1)

import Combine
import Foundation
import Workflow
import Combine
import Foundation
import Workflow

@available(iOS 13.0, macOS 10.15, *)
struct PublisherWorkflow<WorkflowPublisher: Publisher>: Workflow where WorkflowPublisher.Failure == Never {
public typealias Output = WorkflowPublisher.Output
public typealias State = Void
public typealias Rendering = Void
@available(iOS 13.0, macOS 10.15, *)
struct PublisherWorkflow<WorkflowPublisher: Publisher>: Workflow where WorkflowPublisher.Failure == Never {
public typealias Output = WorkflowPublisher.Output
public typealias State = Void
public typealias Rendering = Void

let publisher: WorkflowPublisher
let publisher: WorkflowPublisher

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

public func render(state: State, context: RenderContext<Self>) -> Rendering {
let sink = context.makeSink(of: AnyWorkflowAction.self)
context.runSideEffect(key: "") { [publisher] lifetime in
let cancellable = publisher
.map { AnyWorkflowAction(sendingOutput: $0) }
.receive(on: DispatchQueue.main)
.sink { sink.send($0) }

public func render(state: State, context: RenderContext<Self>) -> Rendering {
let sink = context.makeSink(of: AnyWorkflowAction.self)
context.runSideEffect(key: "") { [publisher] lifetime in
let cancellable = publisher
.map { AnyWorkflowAction(sendingOutput: $0) }
.receive(on: DispatchQueue.main)
.sink { sink.send($0) }

lifetime.onEnded {
cancellable.cancel()
}
lifetime.onEnded {
cancellable.cancel()
}
}
}
}

#endif
134 changes: 67 additions & 67 deletions WorkflowCombine/Sources/Worker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,87 @@

#if canImport(Combine) && swift(>=5.1)

import Combine
import Foundation
import Workflow
import Combine
import Foundation
import Workflow

/// Workers define a unit of asynchronous work.
///
/// During a render pass, a workflow can ask the context to await the result of a worker.
///
/// When this occurs, the context checks to see if there is already a running worker of the same type.
/// If there is, and if the workers are 'equivalent', the context leaves the existing worker running.
///
/// If there is not an existing worker of this type, the context will kick off the new worker (via `run`).
@available(iOS 13.0, macOS 10.15, *)
public protocol Worker: AnyWorkflowConvertible where Rendering == Void {
/// The type of output events returned by this worker.
associatedtype Output
associatedtype WorkerPublisher: Publisher where
WorkerPublisher.Output == Output, WorkerPublisher.Failure == Never
/// Workers define a unit of asynchronous work.
///
/// During a render pass, a workflow can ask the context to await the result of a worker.
///
/// When this occurs, the context checks to see if there is already a running worker of the same type.
/// If there is, and if the workers are 'equivalent', the context leaves the existing worker running.
///
/// If there is not an existing worker of this type, the context will kick off the new worker (via `run`).
@available(iOS 13.0, macOS 10.15, *)
public protocol Worker: AnyWorkflowConvertible where Rendering == Void {
/// The type of output events returned by this worker.
associatedtype Output
associatedtype WorkerPublisher: Publisher where
WorkerPublisher.Output == Output, WorkerPublisher.Failure == Never

/// Returns a publisher to execute the work represented by this worker.
func run() -> WorkerPublisher
/// Returns `true` if the other worker should be considered equivalent to `self`. Equivalence should take into
/// account whatever data is meaningful to the task. For example, a worker that loads a user account from a server
/// would not be equivalent to another worker with a different user ID.
func isEquivalent(to otherWorker: Self) -> Bool
}
/// Returns a publisher to execute the work represented by this worker.
func run() -> WorkerPublisher
/// Returns `true` if the other worker should be considered equivalent to `self`. Equivalence should take into
/// account whatever data is meaningful to the task. For example, a worker that loads a user account from a server
/// would not be equivalent to another worker with a different user ID.
func isEquivalent(to otherWorker: Self) -> Bool
}

@available(iOS 13.0, macOS 10.15, *)
extension Worker {
public func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
WorkerWorkflow(worker: self).asAnyWorkflow()
}
@available(iOS 13.0, macOS 10.15, *)
extension Worker {
public func asAnyWorkflow() -> AnyWorkflow<Void, Output> {
WorkerWorkflow(worker: self).asAnyWorkflow()
}
}

@available(iOS 13.0, macOS 10.15, *)
struct WorkerWorkflow<WorkerType: Worker>: Workflow {
let worker: WorkerType
@available(iOS 13.0, macOS 10.15, *)
struct WorkerWorkflow<WorkerType: Worker>: Workflow {
let worker: WorkerType

typealias Output = WorkerType.Output
typealias Rendering = Void
typealias State = UUID
typealias Output = WorkerType.Output
typealias Rendering = Void
typealias State = UUID

func makeInitialState() -> State { UUID() }
func makeInitialState() -> State { UUID() }

func workflowDidChange(from previousWorkflow: WorkerWorkflow<WorkerType>, state: inout UUID) {
if !worker.isEquivalent(to: previousWorkflow.worker) {
state = UUID()
}
func workflowDidChange(from previousWorkflow: WorkerWorkflow<WorkerType>, state: inout UUID) {
if !worker.isEquivalent(to: previousWorkflow.worker) {
state = UUID()
}
}

func render(state: State, context: RenderContext<WorkerWorkflow>) -> Rendering {
let logger = WorkerLogger<WorkerType>()
func render(state: State, context: RenderContext<WorkerWorkflow>) -> Rendering {
let logger = WorkerLogger<WorkerType>()

Deferred {
worker.run()
.handleEvents(
receiveSubscription: { _ in
logger.logStarted()
},
receiveOutput: { output in
logger.logOutput()
},
receiveCompletion: { completion in
// no need to switch completion since Failure is hardcoded to Never
logger.logFinished(status: "Finished")
},
receiveCancel: {
logger.logFinished(status: "Cancelled")
}
)
.map { AnyWorkflowAction<Self>(sendingOutput: $0) }
}
.running(in: context, key: state.uuidString)
Deferred {
worker.run()
.handleEvents(
receiveSubscription: { _ in
logger.logStarted()
},
receiveOutput: { output in
logger.logOutput()
},
receiveCompletion: { completion in
// no need to switch completion since Failure is hardcoded to Never
logger.logFinished(status: "Finished")
},
receiveCancel: {
logger.logFinished(status: "Cancelled")
}
)
.map { AnyWorkflowAction<Self>(sendingOutput: $0) }
}
.running(in: context, key: state.uuidString)
}
}

@available(iOS 13.0, macOS 10.15, *)
extension Worker where Self: Equatable {
public func isEquivalent(to otherWorker: Self) -> Bool {
self == otherWorker
}
@available(iOS 13.0, macOS 10.15, *)
extension Worker where Self: Equatable {
public func isEquivalent(to otherWorker: Self) -> Bool {
self == otherWorker
}
}

#endif
56 changes: 28 additions & 28 deletions WorkflowCombine/Testing/PublisherTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@

#if DEBUG

import Combine
import Workflow
import WorkflowTesting
import XCTest
@testable import WorkflowCombine
import Combine
import Workflow
import WorkflowTesting
import XCTest
@testable import WorkflowCombine

@available(macOS 10.15, *)
@available(iOS 13.0, *)
extension RenderTester {
/// Expect a `Publisher`s.
///
/// `PublisherWorkflow` is used to subscribe to `Publisher`s.
///
/// - Parameters:
/// - producingOutput: An output that should be returned when this worker is requested, if any.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expect<PublisherType: Publisher>(
publisher: PublisherType.Type,
output: PublisherType.Output,
key: String = ""
) -> RenderTester<WorkflowType> where PublisherType.Failure == Never {
expectWorkflow(
type: PublisherWorkflow<PublisherType>.self,
key: key,
producingRendering: (),
producingOutput: output,
assertions: { _ in }
)
}
@available(macOS 10.15, *)
@available(iOS 13.0, *)
extension RenderTester {
/// Expect a `Publisher`s.
///
/// `PublisherWorkflow` is used to subscribe to `Publisher`s.
///
/// - Parameters:
/// - producingOutput: An output that should be returned when this worker is requested, if any.
/// - key: Key to expect this `Workflow` to be rendered with.
public func expect<PublisherType: Publisher>(
publisher: PublisherType.Type,
output: PublisherType.Output,
key: String = ""
) -> RenderTester<WorkflowType> where PublisherType.Failure == Never {
expectWorkflow(
type: PublisherWorkflow<PublisherType>.self,
key: key,
producingRendering: (),
producingOutput: output,
assertions: { _ in }
)
}
}

#endif
Loading