From fc3bc4de9ad7d9bf6b9f8f6ad283c696eec2a661 Mon Sep 17 00:00:00 2001 From: Tristan Celder Date: Fri, 8 May 2020 15:39:37 +0100 Subject: [PATCH] Removed ReplaySubject Subscriber accounting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The behaviour of Apple's stock Subjects suggests that Subscriber accounting (to prevent duplicate subscriptions of the same Subscriber) is the responsibility of the Subscriber itself – not the Subjects they're subscribed to. Thanks for the heads-up @jasdev! --- Sources/Entwine/Operators/ReplaySubject.swift | 27 +++---------------- Tests/EntwineTests/ReplaySubjectTests.swift | 1 - 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/Sources/Entwine/Operators/ReplaySubject.swift b/Sources/Entwine/Operators/ReplaySubject.swift index fb90c9d..9cda932 100644 --- a/Sources/Entwine/Operators/ReplaySubject.swift +++ b/Sources/Entwine/Operators/ReplaySubject.swift @@ -35,8 +35,6 @@ public final class ReplaySubject { typealias Sink = AnySubscriber private var subscriptions = [ReplaySubjectSubscription]() - private var subscriberIdentifiers = Set() - private var replayValues: ReplaySubjectValueBuffer private var completion: Subscribers.Completion? @@ -55,32 +53,13 @@ public final class ReplaySubject { extension ReplaySubject: Publisher { public func receive(subscriber: S) where Failure == S.Failure, Output == S.Input { - - guard !subscriberIdentifiers.contains(subscriber.combineIdentifier) else { - subscriber.receive(subscription: Subscriptions.empty) - return - } - let subscriberIdentifier = subscriber.combineIdentifier - let subscription = ReplaySubjectSubscription(sink: AnySubscriber(subscriber)) - - // we use seperate collections for identifiers and subscriptions - // to improve performance of identifier lookups and to keep the - // order in which subscribers are signalled to be in the order that - // they intially subscribed. - - subscriberIdentifiers.insert(subscriberIdentifier) subscriptions.append(subscription) - subscription.cleanupHandler = { [weak self] in - - guard let self = self else { return } - - if let index = self.subscriptions.firstIndex(where: { subscriberIdentifier == $0.subscriberIdentifier }) { - self.subscriberIdentifiers.remove(subscriberIdentifier) - self.subscriptions.remove(at: index) - } + let firstIndex = self?.subscriptions.firstIndex { subscriberIdentifier == $0.subscriberIdentifier } + guard let index = firstIndex else { return } + self?.subscriptions.remove(at: index) } subscriber.receive(subscription: subscription) subscription.replayInputs(replayValues.buffer, completion: completion) diff --git a/Tests/EntwineTests/ReplaySubjectTests.swift b/Tests/EntwineTests/ReplaySubjectTests.swift index 33b44dc..c0eb598 100644 --- a/Tests/EntwineTests/ReplaySubjectTests.swift +++ b/Tests/EntwineTests/ReplaySubjectTests.swift @@ -169,7 +169,6 @@ final class ReplaySubjectTests: XCTestCase { scheduler.schedule(after: 600) { subject.send(0) } scheduler.schedule(after: 700) { subject.send(completion: .finished) } - scheduler.resume() let expected1: TestSequence = [