From 4313562d8a8c3504de8efc74429e8a3ac793def7 Mon Sep 17 00:00:00 2001 From: Srdan Rasic Date: Wed, 6 Sep 2017 13:48:07 +0200 Subject: [PATCH] Optimise ConnectableSignal so it does not connect if the subject is already terminated. --- ReactiveKit.podspec | 4 ++-- ReactiveKit/Info.plist | 2 +- Sources/Connectable.swift | 6 +++++- Sources/Disposable.swift | 4 +++- Sources/Subjects.swift | 6 +++--- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ReactiveKit.podspec b/ReactiveKit.podspec index 86556d9..376186a 100644 --- a/ReactiveKit.podspec +++ b/ReactiveKit.podspec @@ -1,12 +1,12 @@ Pod::Spec.new do |s| s.name = "ReactiveKit" - s.version = "3.6.0" + s.version = "3.6.1" 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/ReactiveKit/ReactiveKit" s.license = 'MIT' s.author = { "Srdan Rasic" => "srdan.rasic@gmail.com" } - s.source = { :git => "https://github.com/ReactiveKit/ReactiveKit.git", :tag => "v3.6.0" } + s.source = { :git => "https://github.com/ReactiveKit/ReactiveKit.git", :tag => "v3.6.1" } s.ios.deployment_target = '8.0' s.osx.deployment_target = '10.9' diff --git a/ReactiveKit/Info.plist b/ReactiveKit/Info.plist index 03812ed..582778e 100644 --- a/ReactiveKit/Info.plist +++ b/ReactiveKit/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 3.6.0 + 3.6.1 CFBundleSignature ???? CFBundleVersion diff --git a/Sources/Connectable.swift b/Sources/Connectable.swift index 8439e0f..2814044 100644 --- a/Sources/Connectable.swift +++ b/Sources/Connectable.swift @@ -46,7 +46,11 @@ public final class ConnectableSignal: ConnectableSignalP /// Start the signal. public func connect() -> Disposable { lock.lock(); defer { lock.unlock() } - return source.observe(with: subject) + if !subject.isTerminated { + return source.observe(with: subject) + } else { + return SimpleDisposable(isDisposed: true) + } } /// Register an observer that will receive events from the signal. diff --git a/Sources/Disposable.swift b/Sources/Disposable.swift index c0a4c19..3bc28c6 100644 --- a/Sources/Disposable.swift +++ b/Sources/Disposable.swift @@ -65,7 +65,9 @@ public final class SimpleDisposable: Disposable { isDisposed = true } - public init() {} + public init(isDisposed: Bool = false) { + self.isDisposed = isDisposed + } } /// A disposable that executes the given block upon disposing. diff --git a/Sources/Subjects.swift b/Sources/Subjects.swift index fbc0582..9637b24 100644 --- a/Sources/Subjects.swift +++ b/Sources/Subjects.swift @@ -36,7 +36,7 @@ open class Subject: SubjectProtocol { private var observers: [(Token, Observer)] = [] - private var terminated = false + public private(set) var isTerminated = false public let lock = NSRecursiveLock(name: "com.reactivekit.subject") public let disposeBag = DisposeBag() @@ -45,8 +45,8 @@ open class Subject: SubjectProtocol { public func on(_ event: Event) { lock.lock(); defer { lock.unlock() } - guard !terminated else { return } - terminated = event.isTerminal + guard !isTerminated else { return } + isTerminated = event.isTerminal send(event) }