-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from thoughtbot/merge-bindings
Merge thoughtbot/Bindings as a subtree of CombineViewModel
- Loading branch information
Showing
29 changed files
with
580 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Contributing | ||
|
||
We love contributions from everyone. | ||
By participating in this project, | ||
you agree to abide by the thoughtbot [code of conduct][]. | ||
|
||
[code of conduct]: https://thoughtbot.com/open-source-code-of-conduct | ||
|
||
We expect everyone to follow the code of conduct | ||
anywhere in thoughtbot's project codebases, | ||
issue trackers, chatrooms, and mailing lists. | ||
|
||
## Contributing Code | ||
|
||
Reactive extensions are usually small and straightforward to implement, | ||
making them a great way to share useful utilities for others to use. | ||
|
||
We consider all kinds of changes. If you have an idea but you're not sure | ||
whether it's likely to be accepted, feel free to [open an issue][] first to | ||
discuss it. | ||
|
||
[open an issue]: https://github.com/thoughtbot/CombineViewModel/issues | ||
|
||
First make your change, being sure to test that it compiles on all supported | ||
platforms. | ||
|
||
Push to your fork. Write a [good commit message][commit]. Submit a pull request. | ||
|
||
[commit]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html | ||
|
||
Thank you for contributing! |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2019-20 thoughtbot, inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Combine | ||
import Foundation | ||
|
||
private var bindingOwnerSubscriptionsKey: UInt8 = 0 | ||
|
||
public protocol BindingOwner: AnyObject, ReactiveExtensionProvider { | ||
associatedtype Subscriptions: Collection & ExpressibleByArrayLiteral = Set<AnyCancellable> | ||
where Subscriptions.Element == AnyCancellable | ||
|
||
var subscriptions: Subscriptions { get set } | ||
func store<C: Cancellable>(_ subcription: C) | ||
} | ||
|
||
extension NSObject: BindingOwner {} | ||
|
||
extension BindingOwner where Subscriptions: RangeReplaceableCollection { | ||
@inlinable | ||
public var subscriptions: Subscriptions { | ||
_read { yield _subscriptions } | ||
set { _subscriptions = newValue } | ||
_modify { yield &_subscriptions } | ||
} | ||
|
||
@inlinable | ||
public func store<C: Cancellable>(_ subscription: C) { | ||
subscription.store(in: &subscriptions) | ||
} | ||
} | ||
|
||
extension BindingOwner where Subscriptions == Set<AnyCancellable> { | ||
@inlinable | ||
public var subscriptions: Subscriptions { | ||
_read { yield _subscriptions } | ||
set { _subscriptions = newValue } | ||
_modify { yield &_subscriptions } | ||
} | ||
|
||
@inlinable | ||
public func store<C: Cancellable>(_ subscription: C) { | ||
subscription.store(in: &subscriptions) | ||
} | ||
} | ||
|
||
extension BindingOwner { | ||
@usableFromInline | ||
var _subscriptions: Subscriptions { | ||
_read { | ||
yield _getBox().value | ||
} | ||
set { | ||
_getBox().value = newValue | ||
} | ||
_modify { | ||
yield &_getBox().value | ||
} | ||
} | ||
|
||
@usableFromInline | ||
func _getBox() -> Box<Subscriptions> { | ||
let box: Box<Subscriptions> | ||
if let object = objc_getAssociatedObject(self, &bindingOwnerSubscriptionsKey) { | ||
box = object as! Box<Subscriptions> | ||
} else { | ||
box = Box([]) | ||
objc_setAssociatedObject(self, &bindingOwnerSubscriptionsKey, box, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | ||
} | ||
return box | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Combine | ||
|
||
public final class BindingSink<Owner: BindingOwner, Input> { | ||
public typealias Failure = Never | ||
|
||
private(set) weak var owner: Owner? | ||
private let receiveCompletion: (Owner, Subscribers.Completion<Failure>) -> Void | ||
private let receiveValue: (Owner, Input) -> Void | ||
private var subscription: Subscription? | ||
|
||
public init(owner: Owner, receiveCompletion: @escaping (Owner, Subscribers.Completion<Failure>) -> Void = { _, _ in }, receiveValue: @escaping (Owner, Input) -> Void) { | ||
self.owner = owner | ||
self.receiveCompletion = receiveCompletion | ||
self.receiveValue = receiveValue | ||
} | ||
|
||
private func withOwner(_ body: (Owner) -> Void) { | ||
if let owner = owner { | ||
body(owner) | ||
} else { | ||
cancel() | ||
} | ||
} | ||
} | ||
|
||
extension BindingSink where Input == Void { | ||
public convenience init(owner: Owner, receiveCompletion: @escaping (Owner, Subscribers.Completion<Failure>) -> Void = { _, _ in }, receiveValue: @escaping (Owner) -> Void) { | ||
self.init(owner: owner, receiveCompletion: receiveCompletion, receiveValue: { owner, _ in receiveValue(owner) }) | ||
} | ||
} | ||
|
||
extension BindingSink where Input == Never { | ||
public convenience init(owner: Owner, receiveCompletion: @escaping (Owner, Subscribers.Completion<Failure>) -> Void) { | ||
self.init(owner: owner, receiveCompletion: receiveCompletion, receiveValue: { _, _ in }) | ||
} | ||
} | ||
|
||
extension BindingSink: Subscriber { | ||
public func receive(subscription: Subscription) { | ||
if owner != nil { | ||
subscription.request(.unlimited) | ||
self.subscription = subscription | ||
} else { | ||
subscription.cancel() | ||
} | ||
} | ||
|
||
public func receive(_ input: Input) -> Subscribers.Demand { | ||
withOwner { receiveValue($0, input) } | ||
return .max(1) | ||
} | ||
|
||
public func receive(completion: Subscribers.Completion<Failure>) { | ||
withOwner { receiveCompletion($0, completion) } | ||
} | ||
} | ||
|
||
extension BindingSink: Cancellable { | ||
public func cancel() { | ||
subscription?.cancel() | ||
subscription = nil | ||
owner = nil | ||
} | ||
} | ||
|
||
extension BindingSink: BindingSubscriber { | ||
@discardableResult | ||
public static func <~ <P: Publisher> (sink: BindingSink, publisher: P) -> AnyCancellable | ||
where P.Output == Input, P.Failure == Failure | ||
{ | ||
guard let owner = sink.owner else { return AnyCancellable({}) } | ||
let cancellable = AnyCancellable(sink) | ||
owner.store(cancellable) | ||
publisher.subscribe(sink) | ||
return cancellable | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Combine | ||
|
||
infix operator <~: DefaultPrecedence | ||
|
||
public protocol BindingSubscriber: Subscriber, Cancellable { | ||
@discardableResult | ||
static func <~ <P: Publisher> (subscriber: Self, source: P) -> AnyCancellable | ||
where P.Output == Input, P.Failure == Failure | ||
} | ||
|
||
extension Publisher { | ||
@discardableResult | ||
public static func ~> <B: BindingSubscriber> (source: Self, subscriber: B) -> AnyCancellable | ||
where Output == B.Input, Failure == B.Failure | ||
{ | ||
subscriber <~ source | ||
} | ||
} | ||
|
||
// MARK: Optional | ||
|
||
extension BindingSubscriber { | ||
@discardableResult | ||
public static func <~ <P: Publisher> (subscriber: Self, source: P) -> AnyCancellable | ||
where Input == P.Output?, Failure == P.Failure | ||
{ | ||
subscriber <~ source.map(Optional.some) | ||
} | ||
} | ||
|
||
extension Publisher { | ||
@discardableResult | ||
public static func ~> <B: BindingSubscriber> (source: Self, subscriber: B) -> AnyCancellable | ||
where B.Input == Output?, B.Failure == Failure | ||
{ | ||
subscriber <~ source | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@usableFromInline | ||
final class Box<T> { | ||
var value: T | ||
|
||
init(_ value: T) { | ||
self.value = value | ||
} | ||
} |
Oops, something went wrong.