From ce0e189720cc5313451c68d92bfa5a6a7b6368ba Mon Sep 17 00:00:00 2001 From: Srdan Rasic Date: Sun, 2 Aug 2020 12:49:37 +0200 Subject: [PATCH] Improvements to `share` and `refCount`. --- ReactiveKit.podspec | 4 ++-- ReactiveKit.xcodeproj/project.pbxproj | 6 ++---- Sources/Connectable.swift | 11 +++++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ReactiveKit.podspec b/ReactiveKit.podspec index 9af3157..2b327e1 100644 --- a/ReactiveKit.podspec +++ b/ReactiveKit.podspec @@ -1,12 +1,12 @@ Pod::Spec.new do |s| s.name = "ReactiveKit" - s.version = "3.17.3" + s.version = "3.17.4" s.summary = "A Swift Reactive Programming Framework" s.description = "ReactiveKit is a Swift framework for reactive and functional reactive programming." s.homepage = "https://github.com/DeclarativeHub/ReactiveKit" s.license = 'MIT' s.author = { "Srdan Rasic" => "srdan.rasic@gmail.com" } - s.source = { :git => "https://github.com/DeclarativeHub/ReactiveKit.git", :tag => "v3.17.3" } + s.source = { :git => "https://github.com/DeclarativeHub/ReactiveKit.git", :tag => "v3.17.4" } s.ios.deployment_target = '8.0' s.osx.deployment_target = '10.11' diff --git a/ReactiveKit.xcodeproj/project.pbxproj b/ReactiveKit.xcodeproj/project.pbxproj index 0a6fbc9..fd3c3a7 100644 --- a/ReactiveKit.xcodeproj/project.pbxproj +++ b/ReactiveKit.xcodeproj/project.pbxproj @@ -1089,7 +1089,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 8.0; MACOSX_DEPLOYMENT_TARGET = 10.10; - MARKETING_VERSION = 3.17.1; + MARKETING_VERSION = 3.17.4; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -1144,7 +1144,7 @@ GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 8.0; MACOSX_DEPLOYMENT_TARGET = 10.10; - MARKETING_VERSION = 3.17.1; + MARKETING_VERSION = 3.17.4; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_VERSION = 4.0; @@ -1169,7 +1169,6 @@ INFOPLIST_FILE = "Supporting Files/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 3.17.3; PRODUCT_BUNDLE_IDENTIFIER = DeclarativeHub.ReactiveKit; PRODUCT_NAME = ReactiveKit; SKIP_INSTALL = YES; @@ -1192,7 +1191,6 @@ INFOPLIST_FILE = "Supporting Files/Info.plist"; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; - MARKETING_VERSION = 3.17.3; PRODUCT_BUNDLE_IDENTIFIER = DeclarativeHub.ReactiveKit; PRODUCT_NAME = ReactiveKit; SKIP_INSTALL = YES; diff --git a/Sources/Connectable.swift b/Sources/Connectable.swift index 92585c7..e4facc4 100644 --- a/Sources/Connectable.swift +++ b/Sources/Connectable.swift @@ -62,7 +62,8 @@ extension ConnectableSignalProtocol { /// Convert connectable signal into the ordinary signal by calling `connect` /// on the first observation and calling dispose when number of observers goes down to zero. - public func refCount() -> Signal { + /// - parameter disconnectCount: Subscriptions count on which to disconnect. Defaults to `0`. + public func refCount(disconnectCount: Int = 0) -> Signal { let lock = NSRecursiveLock(name: "com.reactive_kit.connectable_signal.ref_count") var _count = 0 var _connectionDisposable: Disposable? = nil @@ -77,7 +78,7 @@ extension ConnectableSignalProtocol { lock.lock(); defer { lock.unlock() } disposable.dispose() _count = _count - 1 - if _count == 0 { + if _count == disconnectCount { _connectionDisposable?.dispose() _connectionDisposable = nil } @@ -114,8 +115,10 @@ extension SignalProtocol { /// Ensure that all observers see the same sequence of elements. /// Shorthand for `replay(limit).refCount()`. - public func share(limit: Int = Int.max) -> Signal { - return replay(limit: limit).refCount() + /// - parameter limit: Number of latest elements to buffer. + /// - parameter keepAlive: Whether to keep the source signal connected even when all subscribers disconnect. + public func share(limit: Int = Int.max, keepAlive: Bool = false) -> Signal { + return replay(limit: limit).refCount(disconnectCount: keepAlive ? Int.min : 0) } }