Skip to content

Commit

Permalink
Change indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dehesa committed Oct 2, 2021
1 parent cff128f commit a3e95ef
Show file tree
Hide file tree
Showing 42 changed files with 2,751 additions and 2,743 deletions.
30 changes: 15 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
import PackageDescription

var package = Package(
name: "Conbini",
platforms: [
.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6)
],
products: [
.library(name: "Conbini", targets: ["Conbini"]),
.library(name: "ConbiniForTesting", targets: ["ConbiniForTesting"])
],
dependencies: [],
targets: [
.target(name: "Conbini", path: "sources/runtime"),
.target(name: "ConbiniForTesting", path: "sources/testing"),
.testTarget(name: "ConbiniTests", dependencies: ["Conbini"], path: "tests/runtime"),
.testTarget(name: "ConbiniForTestingTests", dependencies: ["Conbini", "ConbiniForTesting"], path: "tests/testing"),
]
name: "Conbini",
platforms: [
.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6)
],
products: [
.library(name: "Conbini", targets: ["Conbini"]),
.library(name: "ConbiniForTesting", targets: ["ConbiniForTesting"])
],
dependencies: [],
targets: [
.target(name: "Conbini", path: "sources/runtime"),
.target(name: "ConbiniForTesting", path: "sources/testing"),
.testTarget(name: "ConbiniTests", dependencies: ["Conbini"], path: "tests/runtime"),
.testTarget(name: "ConbiniForTestingTests", dependencies: ["Conbini", "ConbiniForTesting"], path: "tests/testing"),
]
)
176 changes: 88 additions & 88 deletions sources/runtime/operators/AssignOp.swift
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
import Combine

extension Publisher where Self.Failure==Never {
/// Assigns a publisher's output to a property of an object.
///
/// The difference between `assign(to:onWeak:)` and Combine's `assign(to:on:)` is two-fold:
/// - `assign(to:onWeak:)` doesn't set a _strong bond_ to `object`.
/// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - `assign(to:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized.
///
/// The difference between is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - parameter keyPath: A key path that indicates the property to assign.
/// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment.
@_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onWeak object: Root) -> AnyCancellable where Root:AnyObject {
weak var cancellable: AnyCancellable? = nil
let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in
cancellable?.cancel()
cancellable = nil
}

let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in
guard let object = object else { return cleanup(.finished) }
object[keyPath: keyPath] = value
})

let result = AnyCancellable(subscriber)
cancellable = result
self.subscribe(subscriber)
return result
}

/// Assigns a publisher's output to a property of an object.
///
/// The difference between `assign(to:onUnowned:)` and Combine's `assign(to:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - parameter keyPath: A key path that indicates the property to assign.
/// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment.
@_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onUnowned object: Root) -> AnyCancellable where Root:AnyObject {
self.sink(receiveValue: { [unowned object] (value) in
object[keyPath: keyPath] = value
})
/// Assigns a publisher's output to a property of an object.
///
/// The difference between `assign(to:onWeak:)` and Combine's `assign(to:on:)` is two-fold:
/// - `assign(to:onWeak:)` doesn't set a _strong bond_ to `object`.
/// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - `assign(to:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized.
///
/// The difference between is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - parameter keyPath: A key path that indicates the property to assign.
/// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment.
@_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onWeak object: Root) -> AnyCancellable where Root:AnyObject {
weak var cancellable: AnyCancellable? = nil
let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in
cancellable?.cancel()
cancellable = nil
}

let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in
guard let object = object else { return cleanup(.finished) }
object[keyPath: keyPath] = value
})

let result = AnyCancellable(subscriber)
cancellable = result
self.subscribe(subscriber)
return result
}

/// Assigns a publisher's output to a property of an object.
///
/// The difference between `assign(to:onUnowned:)` and Combine's `assign(to:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - parameter keyPath: A key path that indicates the property to assign.
/// - parameter object: The object that contains the property. The subscriber assigns the object's property every time it receives a new value.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically assign the property. Deinitializing this instance will also cancel automatic assignment.
@_transparent public func assign<Root>(to keyPath: ReferenceWritableKeyPath<Root,Output>, onUnowned object: Root) -> AnyCancellable where Root:AnyObject {
self.sink(receiveValue: { [unowned object] (value) in
object[keyPath: keyPath] = value
})
}
}

extension Publisher where Self.Failure==Never {
/// Invoke on the given instance the specified method.
/// - remark: A strong bond is set to `instance`. If you store the cancellable in the same instance as `instance`, a memory cycle will be created.
/// - parameter method: A method/function metatype.
/// - parameter instance: The instance defining the specified method.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation.
@_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, on instance: Root) -> AnyCancellable {
return self.sink(receiveValue: { (value) in
method(instance)(value)
})
}

/// Invoke on the given instance the specified method.
///
/// The difference between `invoke(_:onWeak:)` and Combine's `invoke(_:on:)` is two-fold:
/// - `invoke(_:onWeak:)` doesn't set a _strong bond_ to `object`.
/// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - `invoke(_:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized.
///
/// - parameter method: A method/function metatype.
/// - parameter instance: The instance defining the specified method.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation.
@_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onWeak object: Root) -> AnyCancellable where Root:AnyObject {
weak var cancellable: AnyCancellable? = nil
let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in
cancellable?.cancel()
cancellable = nil
}

let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in
guard let object = object else { return cleanup(.finished) }
method(object)(value)
})

let result = AnyCancellable(subscriber)
cancellable = result
self.subscribe(subscriber)
return result
}

/// Invoke on the given instance the specified method.
///
/// The difference between `invoke(_:onUnowned:)` and Combine's `invoke(_:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - parameter method: A method/function metatype.
/// - parameter instance: The instance defining the specified method.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation.
@_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onUnowned object: Root) -> AnyCancellable where Root:AnyObject {
return self.sink(receiveValue: { [unowned object] (value) in
method(object)(value)
})
/// Invoke on the given instance the specified method.
/// - remark: A strong bond is set to `instance`. If you store the cancellable in the same instance as `instance`, a memory cycle will be created.
/// - parameter method: A method/function metatype.
/// - parameter instance: The instance defining the specified method.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation.
@_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, on instance: Root) -> AnyCancellable {
self.sink(receiveValue: { (value) in
method(instance)(value)
})
}

/// Invoke on the given instance the specified method.
///
/// The difference between `invoke(_:onWeak:)` and Combine's `invoke(_:on:)` is two-fold:
/// - `invoke(_:onWeak:)` doesn't set a _strong bond_ to `object`.
/// This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - `invoke(_:onWeak:)` cancels the upstream publisher if it detects `object` is deinitialized.
///
/// - parameter method: A method/function metatype.
/// - parameter instance: The instance defining the specified method.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation.
@_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onWeak object: Root) -> AnyCancellable where Root:AnyObject {
weak var cancellable: AnyCancellable? = nil
let cleanup: (Subscribers.Completion<Never>) -> Void = { _ in
cancellable?.cancel()
cancellable = nil
}

let subscriber = Subscribers.Sink<Output,Never>(receiveCompletion: cleanup, receiveValue: { [weak object] (value) in
guard let object = object else { return cleanup(.finished) }
method(object)(value)
})

let result = AnyCancellable(subscriber)
cancellable = result
self.subscribe(subscriber)
return result
}

/// Invoke on the given instance the specified method.
///
/// The difference between `invoke(_:onUnowned:)` and Combine's `invoke(_:on:)` is that a _strong bond_ is not set to the`object`. This breaks memory cycles when `object` also stores the returned cancellable (e.g. passing `self` is a common case).
/// - parameter method: A method/function metatype.
/// - parameter instance: The instance defining the specified method.
/// - returns: An `AnyCancellable` instance. Call `cancel()` on the instance when you no longer want the publisher to automatically call the method. Deinitializing this instance will also cancel automatic invocation.
@_transparent public func invoke<Root>(_ method: @escaping (Root)->(Output)->Void, onUnowned object: Root) -> AnyCancellable where Root:AnyObject {
self.sink(receiveValue: { [unowned object] (value) in
method(object)(value)
})
}
}
42 changes: 21 additions & 21 deletions sources/runtime/operators/AwaitOp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ import Combine
import Foundation

extension Publisher {
/// Subscribes to the receiving publichser and expects a single value and a subsequent successfull completion.
///
/// If no values is received, or more than one value is received, or a failure is received, the program will crash.
/// - warning: The publisher must receive the value and completion event in a different queue from the queue where this property is called or the code will never execute.
@inlinable public var await: Output {
let group = DispatchGroup()
group.enter()

var value: Output? = nil
let cancellable = self.sink(fixedDemand: 1, receiveCompletion: {
switch $0 {
case .failure(let error): fatalError("\(error)")
case .finished:
guard case .some = value else { fatalError() }
group.leave()
}
}, receiveValue: { value = $0 })
/// Subscribes to the receiving publichser and expects a single value and a subsequent successfull completion.
///
/// If no values is received, or more than one value is received, or a failure is received, the program will crash.
/// - warning: The publisher must receive the value and completion event in a different queue from the queue where this property is called or the code will never execute.
@inlinable public var await: Output {
let group = DispatchGroup()
group.enter()

group.wait()
cancellable.cancel()
return value!
}
var value: Output? = nil
let cancellable = self.sink(fixedDemand: 1, receiveCompletion: {
switch $0 {
case .failure(let error): fatalError("\(error)")
case .finished:
guard case .some = value else { fatalError() }
group.leave()
}
}, receiveValue: { value = $0 })

group.wait()
cancellable.cancel()
return value!
}
}
16 changes: 8 additions & 8 deletions sources/runtime/operators/HandleEndOp.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Combine

extension Publisher {
/// Performs the specified closure when the publisher completes (whether successfully or with a failure) or when the publisher gets cancelled.
///
/// The closure will get executed exactly once.
/// - parameter handle: A closure that executes when the publisher receives a completion event or when the publisher gets cancelled.
/// - parameter completion: A completion event if the publisher completes (whether successfully or not), or `nil` in case the publisher is cancelled.
@inlinable public func handleEnd(_ handle: @escaping (_ completion: Subscribers.Completion<Failure>?)->Void) -> Publishers.HandleEnd<Self> {
.init(upstream: self, handle: handle)
}
/// Performs the specified closure when the publisher completes (whether successfully or with a failure) or when the publisher gets cancelled.
///
/// The closure will get executed exactly once.
/// - parameter handle: A closure that executes when the publisher receives a completion event or when the publisher gets cancelled.
/// - parameter completion: A completion event if the publisher completes (whether successfully or not), or `nil` in case the publisher is cancelled.
@inlinable public func handleEnd(_ handle: @escaping (_ completion: Subscribers.Completion<Failure>?)->Void) -> Publishers.HandleEnd<Self> {
.init(upstream: self, handle: handle)
}
}

0 comments on commit a3e95ef

Please sign in to comment.