From 820b528827663a9abead8642498129b2e02aa3fa Mon Sep 17 00:00:00 2001 From: Tristan Celder Date: Wed, 3 Jul 2019 12:19:55 +0200 Subject: [PATCH] Updated for latest Combine beta API --- Sources/Common/Utilities/SinkQueue.swift | 9 ++++----- Sources/Entwine/Deprecations.swift | 9 +++++++++ .../{CancellationBag.swift => CancellableBag.swift} | 4 ++-- .../EntwineTest/TestableSubscriber/DemandLedger.swift | 4 ++-- .../TestableSubscriber/TestableSubscriber.swift | 4 +--- Tests/EntwineTestTests/TestableSubscriberTests.swift | 8 ++++---- ...cellationBagTests.swift => CancellableBagTests.swift} | 6 +++--- Tests/EntwineTests/MaterializeTests.swift | 2 +- 8 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 Sources/Entwine/Deprecations.swift rename Sources/Entwine/Utilities/{CancellationBag.swift => CancellableBag.swift} (94%) rename Tests/EntwineTests/{CancellationBagTests.swift => CancellableBagTests.swift} (95%) diff --git a/Sources/Common/Utilities/SinkQueue.swift b/Sources/Common/Utilities/SinkQueue.swift index 916ba28..71857bc 100644 --- a/Sources/Common/Utilities/SinkQueue.swift +++ b/Sources/Common/Utilities/SinkQueue.swift @@ -34,7 +34,6 @@ class SinkQueue { private var demandRequested = Subscribers.Demand.none private var demandProcessed = Subscribers.Demand.none private var demandForwarded = Subscribers.Demand.none - private var demandQueued: Subscribers.Demand { .max(buffer.count) } private var completion: Subscribers.Completion? private var isActive: Bool { sink != nil && completion == nil } @@ -78,13 +77,13 @@ class SinkQueue { demandProcessed += 1 demandRequested += sink.receive(next) } - if let completion = completion, demandQueued < 1 { + if let completion = completion, buffer.count < 1 { expediteCompletion(completion) return .none } - let spareDemand = max(.none, demandRequested - demandProcessed - demandQueued - demandForwarded) - demandForwarded += spareDemand - return spareDemand + let forwardableDemand = (demandRequested - demandForwarded) + demandForwarded += forwardableDemand + return forwardableDemand } func assertPreCompletion() { diff --git a/Sources/Entwine/Deprecations.swift b/Sources/Entwine/Deprecations.swift new file mode 100644 index 0000000..41d1aa4 --- /dev/null +++ b/Sources/Entwine/Deprecations.swift @@ -0,0 +1,9 @@ +// +// File.swift +// +// +// Created by Tristan Celder on 01/07/2019. +// + +@available(*, deprecated, renamed: "CancellableBag") +public typealias CancellationBag = CancellableBag diff --git a/Sources/Entwine/Utilities/CancellationBag.swift b/Sources/Entwine/Utilities/CancellableBag.swift similarity index 94% rename from Sources/Entwine/Utilities/CancellationBag.swift rename to Sources/Entwine/Utilities/CancellableBag.swift index 89f462a..b8e89c2 100644 --- a/Sources/Entwine/Utilities/CancellationBag.swift +++ b/Sources/Entwine/Utilities/CancellableBag.swift @@ -25,7 +25,7 @@ import Combine /// A container for cancellables that will be cancelled when the bag is deallocated or cancelled itself -public final class CancellationBag: Cancellable { +public final class CancellableBag: Cancellable { public init() {} @@ -47,7 +47,7 @@ public final class CancellationBag: Cancellable { public extension Cancellable { /// Adds this cancellable to the passed `CancellationBag` - func cancelled(by cancellationBag: CancellationBag) { + func cancelled(by cancellationBag: CancellableBag) { cancellationBag.add(self) } } diff --git a/Sources/EntwineTest/TestableSubscriber/DemandLedger.swift b/Sources/EntwineTest/TestableSubscriber/DemandLedger.swift index 2ca8b49..24b65a3 100644 --- a/Sources/EntwineTest/TestableSubscriber/DemandLedger.swift +++ b/Sources/EntwineTest/TestableSubscriber/DemandLedger.swift @@ -147,10 +147,10 @@ extension DemandLedger.Transaction: CustomDebugStringConvertible { extension Subscribers.Demand { func prettyDescription() -> String { - guard case .max(let amount) = self else { + guard let max = max else { return ".unlimited" } - return ".max(\(amount))" + return ".max(\(max))" } } diff --git a/Sources/EntwineTest/TestableSubscriber/TestableSubscriber.swift b/Sources/EntwineTest/TestableSubscriber/TestableSubscriber.swift index 7c06a9e..5904259 100644 --- a/Sources/EntwineTest/TestableSubscriber/TestableSubscriber.swift +++ b/Sources/EntwineTest/TestableSubscriber/TestableSubscriber.swift @@ -93,10 +93,8 @@ public final class TestableSubscriber { func debitDemand(_ demand: Subscribers.Demand) { let authorized = (demandBalance > .none) - - demandBalance -= demand + demandBalance -= authorized ? demand : .none demands.append((scheduler.now, demandBalance, .debit(authorized: authorized))) - if !authorized { signalNegativeBalance() } diff --git a/Tests/EntwineTestTests/TestableSubscriberTests.swift b/Tests/EntwineTestTests/TestableSubscriberTests.swift index 9276740..a283208 100644 --- a/Tests/EntwineTestTests/TestableSubscriberTests.swift +++ b/Tests/EntwineTestTests/TestableSubscriberTests.swift @@ -188,10 +188,10 @@ final class TestableSubscriberTests: XCTestCase { let expectedDemandLedger: DemandLedger = [ - (200, .max( 2), .credit(amount: .max(2))), - (200, .max( 1), .debit(authorized: true)), - (200, .none, .debit(authorized: true)), - (200, .max(-1), .debit(authorized: false)), + (200, .max(2), .credit(amount: .max(2))), + (200, .max(1), .debit(authorized: true)), + (200, .none, .debit(authorized: true)), + (200, .none, .debit(authorized: false)), ] XCTAssertEqual(expectedDemandLedger, testableSubscriber.demands) diff --git a/Tests/EntwineTests/CancellationBagTests.swift b/Tests/EntwineTests/CancellableBagTests.swift similarity index 95% rename from Tests/EntwineTests/CancellationBagTests.swift rename to Tests/EntwineTests/CancellableBagTests.swift index 579d0e6..2dfd12a 100644 --- a/Tests/EntwineTests/CancellationBagTests.swift +++ b/Tests/EntwineTests/CancellableBagTests.swift @@ -28,7 +28,7 @@ import Combine @testable import Entwine @testable import EntwineTest -final class CancellationBagTests: XCTestCase { +final class CancellableBagTests: XCTestCase { func testBagCancelsContainedCancellablesOnDeallocation() { @@ -36,7 +36,7 @@ final class CancellationBagTests: XCTestCase { let subject = PassthroughSubject() let subscriber = scheduler.createTestableSubscriber(Int.self, Never.self) - var sut: CancellationBag! = CancellationBag() + var sut: CancellableBag! = CancellableBag() subscriber.cancelled(by: sut) @@ -61,7 +61,7 @@ final class CancellationBagTests: XCTestCase { let subject = PassthroughSubject() let subscriber = scheduler.createTestableSubscriber(Int.self, Never.self) - let sut = CancellationBag() + let sut = CancellableBag() subscriber.cancelled(by: sut) diff --git a/Tests/EntwineTests/MaterializeTests.swift b/Tests/EntwineTests/MaterializeTests.swift index e1d74e8..867e222 100644 --- a/Tests/EntwineTests/MaterializeTests.swift +++ b/Tests/EntwineTests/MaterializeTests.swift @@ -74,7 +74,7 @@ final class MaterializeTests: XCTestCase { func testMaterializesJust1() { - let results1 = scheduler.start { Publishers.Just(1).materialize() } + let results1 = scheduler.start { Just(1).materialize() } let expected1: TestSequence, Never> = [ (200, .subscription),