Skip to content

Commit

Permalink
Optimise ConnectableSignal so it does not connect if the subject is a…
Browse files Browse the repository at this point in the history
…lready terminated.
  • Loading branch information
srdanrasic committed Sep 6, 2017
1 parent e68d326 commit 4313562
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions ReactiveKit.podspec
Original file line number Diff line number Diff line change
@@ -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" => "[email protected]" }
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'
Expand Down
2 changes: 1 addition & 1 deletion ReactiveKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>3.6.0</string>
<string>3.6.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
6 changes: 5 additions & 1 deletion Sources/Connectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public final class ConnectableSignal<Source: SignalProtocol>: 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.
Expand Down
4 changes: 3 additions & 1 deletion Sources/Disposable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Subjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class Subject<Element, Error: Swift.Error>: SubjectProtocol {

private var observers: [(Token, Observer<Element, Error>)] = []

private var terminated = false
public private(set) var isTerminated = false

public let lock = NSRecursiveLock(name: "com.reactivekit.subject")
public let disposeBag = DisposeBag()
Expand All @@ -45,8 +45,8 @@ open class Subject<Element, Error: Swift.Error>: SubjectProtocol {

public func on(_ event: Event<Element, Error>) {
lock.lock(); defer { lock.unlock() }
guard !terminated else { return }
terminated = event.isTerminal
guard !isTerminated else { return }
isTerminated = event.isTerminal
send(event)
}

Expand Down

0 comments on commit 4313562

Please sign in to comment.