diff --git a/Sources/Propagate/PublisherAndSubscriber/Publisher.swift b/Sources/Propagate/PublisherAndSubscriber/Publisher.swift index 64daf16..4ffbd80 100644 --- a/Sources/Propagate/PublisherAndSubscriber/Publisher.swift +++ b/Sources/Propagate/PublisherAndSubscriber/Publisher.swift @@ -17,7 +17,7 @@ public class Publisher { fileprivate var subscribers = WeakBag>() private(set) public var isCancelled = false - internal var loggingCombo: (LoggingCombo)? + internal var loggingCombo: LoggingCombo? public init() { } @@ -35,7 +35,11 @@ public class Publisher { } deinit { - safePrint("Releasing \(self) from memory.", logType: .lifeCycle, loggingCombo: loggingCombo) + safePrint( + "Releasing \(self) from memory.", + logType: .lifeCycle, + loggingCombo: loggingCombo + ) // The asynchronous cancelAll() can't be called from deinit // because it results in a bad access crash. handleCancellation() @@ -48,17 +52,19 @@ public class Publisher { /// `cancelAll()`, which will remove all subscribers and emit the `.cancelled` /// state on each of them. public func subscriber() -> Subscriber { - /* - Any changes made to this function's implementation will - also need to be made to the same method on StatefulPublisher. - */ let newSub = Subscriber(canceller: .init { [weak self] in self?.removeSubscriber($0) }) + /// Any changes made in this method that need to shared between + /// this and any subclasses should be added to the below helper method. + return addNewSubscriberAndLog(newSub) + } + + fileprivate func addNewSubscriberAndLog(_ newSub: Subscriber) -> Subscriber { lockQueue.async { [weak self] in self?.subscribers.insert(newSub) } - safePrint("Generating new subscriber: \(newSub) from \(self)", logType: .lifeCycle, loggingCombo: loggingCombo) + logPublisher(self, generatingNewSubscriber: newSub, loggingCombo: loggingCombo) return newSub } @@ -122,7 +128,7 @@ extension Publisher: PropagateDebuggable { @discardableResult public func enableLogging( logLevel: DebugLogLevel = .all, _ additionalMessage: String = "", - _ logMethod: LoggingMethod = .debugPrint + _ logMethod: LoggingMethod ) -> Self { self.loggingCombo = (logLevel, additionalMessage, logMethod) return self @@ -219,24 +225,14 @@ public class StatefulPublisher: Publisher { } public override func subscriber() -> Subscriber { - /* - Any changes made to this function's implementation will - also need to be made to the same method on Publisher. - */ let newSub = OnSubscribeCallbackSubscriber(canceller: .init { [weak self] in self?.removeSubscriber($0) }) newSub.lastStateCallback = { [weak self] in self?.lastState } - lockQueue.async { [weak self] in - self?.subscribers.insert(newSub) - } - safePrint( - "Generating new subscriber. -- \(self) --> \(newSub)", - logType: .lifeCycle, - loggingCombo: loggingCombo - ) - return newSub + /// Any changes made in this method that need to shared between + /// this and the superclass should be added to the below helper method. + return addNewSubscriberAndLog(newSub) } } @@ -248,3 +244,15 @@ public extension Publisher where T == Void { } } + +private func logPublisher( + _ pub: Publisher, + generatingNewSubscriber sub: Subscriber, + loggingCombo: LoggingCombo? +) { + safePrint( + "Instance \(memoryAddressStringFor(pub)) generating new subscriber --> \(sub)", + logType: .lifeCycle, + loggingCombo: loggingCombo + ) +} diff --git a/Sources/Propagate/PublisherAndSubscriber/Subscriber+Statify.swift b/Sources/Propagate/PublisherAndSubscriber/Subscriber+Statify.swift index d2c6a46..5214c95 100644 --- a/Sources/Propagate/PublisherAndSubscriber/Subscriber+Statify.swift +++ b/Sources/Propagate/PublisherAndSubscriber/Subscriber+Statify.swift @@ -12,7 +12,8 @@ public extension Subscriber { boundStatefulPublisher() .subscriber() .onCancelled { - _ = self // Capturing self to keep subscriber alive for easier chaining. + // Capturing self to keep subscriber alive for easier chaining. + _ = self } } @@ -21,7 +22,8 @@ public extension Subscriber { publisher.publish(value) return publisher.subscriber() .onCancelled { - _ = self // Capturing self to keep subscriber alive for easier chaining. + // Capturing self to keep subscriber alive for easier chaining. + _ = self } } diff --git a/Sources/Propagate/PublisherAndSubscriber/Subscriber.swift b/Sources/Propagate/PublisherAndSubscriber/Subscriber.swift index e54ff5e..3c0159d 100644 --- a/Sources/Propagate/PublisherAndSubscriber/Subscriber.swift +++ b/Sources/Propagate/PublisherAndSubscriber/Subscriber.swift @@ -59,7 +59,7 @@ extension Subscriber: PropagateDebuggable { @discardableResult public func enableLogging( logLevel: DebugLogLevel = .all, _ additionalMessage: String = "", - _ logMethod: LoggingMethod = .debugPrint + _ logMethod: LoggingMethod ) -> Self { self.loggingCombo = (logLevel, additionalMessage, logMethod) return self diff --git a/Sources/Propagate/PublisherAndSubscriber/ValueOnlySubscriber.swift b/Sources/Propagate/PublisherAndSubscriber/ValueOnlySubscriber.swift index fd4203b..f24f16a 100644 --- a/Sources/Propagate/PublisherAndSubscriber/ValueOnlySubscriber.swift +++ b/Sources/Propagate/PublisherAndSubscriber/ValueOnlySubscriber.swift @@ -128,7 +128,7 @@ extension ValueOnlySubscriber: PropagateDebuggable, CustomStringConvertible { @discardableResult public func enableLogging( logLevel: DebugLogLevel = .all, _ additionalMessage: String = "", - _ logMethod: LoggingMethod = .debugPrint + _ logMethod: LoggingMethod ) -> Self { self.loggingCombo = (logLevel, additionalMessage, logMethod) return self