https://github.com/dankinsoid/combineoperators
Combine operators
https://github.com/dankinsoid/combineoperators
Last synced: 10 months ago
JSON representation
Combine operators
- Host: GitHub
- URL: https://github.com/dankinsoid/combineoperators
- Owner: dankinsoid
- License: mit
- Created: 2021-02-25T12:53:38.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-11-15T06:50:03.000Z (over 3 years ago)
- Last Synced: 2025-08-03T10:41:55.129Z (11 months ago)
- Language: Swift
- Size: 230 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CombineOperators
[](https://travis-ci.org/Voidilov/CombineOperators)
[](https://cocoapods.org/pods/CombineOperators)
[](https://cocoapods.org/pods/CombineOperators)
[](https://cocoapods.org/pods/CombineOperators)
## Description
This repo includes operators for Combine, almost complete re-implementation of CombineCocoa, RxViewController, RxGestures and RxKeyboard libraries and some additional features.
Example
```swift
import UIKit
import Combine
import CombineCocoa
import CombineOperators
final class SomeViewModel {
let title = CurrentValueSubject("Title")
let icon = CurrentValueSubject(nil)
let color = CurrentValueSubject(UIColor.white)
let bool = CurrentValueSubject(false)
...
}
class ViewController: UIViewController {
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var iconView: UIImageView!
@IBOutlet private weak var switchView: UISwitch!
let viewModel = SomeViewModel()
...
private func configureSubscriptions() {
viewModel.title ==> titleLabel.cb.text
viewModel.iconView ==> iconView.cb.image
viewModel.bool ==> switchView.cb.isOn
viewModel.color ==> (self, ViewController.setTint)
//or viewModel.color ==> cb.weak(method: ViewController.setTint)
//or viewModel.color ==> {[weak self] in self?.setTint(color: $0) }
}
private func setTint(color: UIColor) {
...
}
...
}
```
## Usage
1. Operator `=>`
- From `Publisher` to `Subscriber`, creates a subscription:
```swift
intPublisher => intSubscriber
```
- From `Publisher` to `Subject`, creates a subscription and returns `Cancellable`:
```swift
let Cancellable = intPublisher => intSubject
```
- From `Cancellable` to `DisposeBag`:
```swift
someCancellable => cancellableSet
somePublisher => someSubject => cancellableSet
```
- From `Publisher` to `Scheduler`, returns `AnyPublisher`:
```swift
somePublisher => DispatchQueue.main => someSubscriber
```
- From `Publisher` to `(Output) -> Void`:
```swift
somePublisher => { print($0) }
```
- From `Publisher` to `@autoescaping () -> Void`:
```swift
somePublisher => print("action")
```
All operators casts output-input types and errors types where possible
2. Operator `==>`
Drive `Publisher` to `Subscriber` on main queue:
```swift
intPublisher ==> intSubscriber => cancellableSet
```
3. Operators `=>>` and `==>>` replaces `.removeDublicates()`
4. `CancellableBuilder` and `MergeBuilder`, `CombineLatestBuilder` - result builders
5. Some features:
- `UIView.cb` operators: `isVisible`, `willAppear`, `isOnScreen`, `didAppear`, `movedToWindow`, `frame`, `frameOnWindow`, etc.
- `skipNil()` operator
- `or(Bool), .toggle(), !` operators for boolean sequences
- use `+` and `+=` operator for merging publishers, creating Cancellables, etc
- `interval(...)`
- `withLast() -> AnyPublisher<(previous: Output?, current: Output), Failure>`
- `.mp` - `@dynamicMemberLookup` mapper
- `asResult() -> AnyPublisher, Never>`
- `nilIfEmpty`
- `isEmpty`
- `isNil`
- `isNilOrEmpty`
- `append(...)`
- `smooth(...)` methods to smooth changes, example: sequence`[0, 1]` turns to `[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]`
- `onValue, onFailure, onFinished, onSubscribe, onCancel, onRequest` wrappers on `handleEvents(...)` operator
- `guard()`
- `cb.isFirstResponder`
- `UIStackView().cb.update(...)`
- `UIView().cb.transform.scale(), .rotation(), .translation()`
## Installation
1. [CocoaPods](https://cocoapods.org)
CombineOperators is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'CombineOperators/CombineCocoa'
```
and run `pod update` from the podfile directory first.
2. [Swift Package Manager](https://github.com/apple/swift-package-manager)
Create a `Package.swift` file.
```swift
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/CombineOperators.git", from: "1.81.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["CombineOperators"])
]
)
```
```ruby
$ swift build
```
## Author
Voidilov, voidilov@gmail.com
## License
CombineOperators is available under the MIT license. See the LICENSE file for more info.