├── .gitignore ├── CancelBag.podspec ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── CancelBag │ └── CancelBag.swift └── Tests └── CancelBagTests └── CancelBagTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | -------------------------------------------------------------------------------- /CancelBag.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "CancelBag" 3 | s.version = "1.0.0" 4 | s.summary = "A DisposeBag for Combine" 5 | s.homepage = "https://github.com/devxoul/CancelBag" 6 | s.license = { :type => "MIT", :file => "LICENSE" } 7 | s.author = { "Suyeol Jeon" => "devxoul@gmail.com" } 8 | s.source = { :git => "https://github.com/devxoul/CancelBag.git", 9 | :tag => s.version.to_s } 10 | s.source_files = "Sources/**/*.{swift,h,m}" 11 | s.frameworks = "Foundation", "Combine" 12 | s.swift_version = "5.1" 13 | 14 | s.osx.deployment_target = "10.15" 15 | s.ios.deployment_target = "13.0" 16 | s.tvos.deployment_target = "13.0" 17 | s.watchos.deployment_target = "6.0" 18 | end 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Suyeol Jeon (xoul.kr) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.1 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "CancelBag", 7 | platforms: [ 8 | .macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6) 9 | ], 10 | products: [ 11 | .library(name: "CancelBag", targets: ["CancelBag"]), 12 | ], 13 | dependencies: [], 14 | targets: [ 15 | .target(name: "CancelBag", dependencies: []), 16 | .testTarget(name: "CancelBagTests", dependencies: ["CancelBag"]), 17 | ] 18 | ) 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CancelBag 2 | 3 | A DisposeBag for [Combine](https://developer.apple.com/documentation/combine). 4 | 5 | ## Usage 6 | 7 | ```swift 8 | let cancelBag = CancelBag() 9 | 10 | myPublisher 11 | .sink { _ in } 12 | .cancel(with: cancelBag) 13 | ``` 14 | 15 | ## Installation 16 | 17 | - **Using [Swift Package Manager](https://swift.org/package-manager)**: 18 | 19 | ```swift 20 | import PackageDescription 21 | 22 | let package = Package( 23 | name: "MyAwesomeApp", 24 | dependencies: [ 25 | .Package(url: "https://github.com/devxoul/CancelBag", majorVersion: 1), 26 | ] 27 | ) 28 | ``` 29 | 30 | - **Using [CocoaPods](https://cocoapods.org)**: 31 | 32 | ```ruby 33 | pod 'CancelBag' 34 | ``` 35 | 36 | ## License 37 | 38 | **CancelBag** is under MIT license. See the [LICENSE](LICENSE) file for more info. 39 | -------------------------------------------------------------------------------- /Sources/CancelBag/CancelBag.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import class Foundation.NSLock 3 | 4 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 5 | public final class CancelBag: Cancellable { 6 | 7 | private let lock: NSLock = NSLock() 8 | private var cancellables: [Cancellable] = [] 9 | 10 | public init() { 11 | } 12 | 13 | internal func add(_ cancellable: Cancellable) { 14 | self.lock.lock() 15 | defer { self.lock.unlock() } 16 | self.cancellables.append(cancellable) 17 | } 18 | 19 | public func cancel() { 20 | self.lock.lock() 21 | let cancellables = self.cancellables 22 | self.cancellables.removeAll() 23 | self.lock.unlock() 24 | 25 | for cancellable in cancellables { 26 | cancellable.cancel() 27 | } 28 | } 29 | 30 | deinit { 31 | self.cancel() 32 | } 33 | } 34 | 35 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 36 | public extension Cancellable { 37 | func cancel(with cancellable: CancelBag) { 38 | cancellable.add(self) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Tests/CancelBagTests/CancelBagTests.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import XCTest 3 | @testable import CancelBag 4 | 5 | @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) 6 | final class CancelBagTests: XCTestCase { 7 | func testCancelBag() { 8 | var cancellationCount = 0 9 | _ = { 10 | let cancelBag = CancelBag() 11 | 12 | for _ in 0..<10 { 13 | Publishers.Future { _ in } 14 | .handleEvents(receiveCancel: { cancellationCount += 1 }) 15 | .sink { _ in } 16 | .cancel(with: cancelBag) 17 | } 18 | }() 19 | XCTAssertEqual(cancellationCount, 10) 20 | } 21 | } 22 | --------------------------------------------------------------------------------