Skip to content

Commit

Permalink
Use CompositeDisposable for all Lifetimes.
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed May 14, 2017
1 parent bd64be9 commit 92dccf5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 51 deletions.
61 changes: 18 additions & 43 deletions Sources/Lifetime.swift
Expand Up @@ -4,46 +4,29 @@ import enum Result.NoError
/// Represents the lifetime of an object, and provides a hook to observe when
/// the object deinitializes.
public final class Lifetime {
internal enum Backing {
case token(ended: Signal<(), NoError>)
case disposable(CompositeDisposable)
}

private let base: Backing
private let disposables: CompositeDisposable

/// A signal that sends a `completed` event when the lifetime ends.
///
/// - note: Consider using `Lifetime.observeEnded` if only a closure observer
/// is to be attached.
public var ended: Signal<(), NoError> {
switch base {
case let .token(ended):
return ended

case let .disposable(disposable):
return Signal { observer in
return disposable += observer.sendCompleted
}
return Signal { observer in
return disposables += observer.sendCompleted
}
}

/// A flag indicating whether the lifetime has ended.
public var hasEnded: Bool {
switch base {
case let .token(ended):
return ended.hasTerminated

case let .disposable(disposable):
return disposable.isDisposed
}
return disposables.isDisposed
}

/// Initialize a `Lifetime` object with the supplied ended signal.
/// Initialize a `Lifetime` object with the supplied composite disposable.
///
/// - parameters:
/// - signal: The ended signal.
internal init(_ base: Backing) {
self.base = base
/// - signal: The composite disposable.
internal init(_ disposables: CompositeDisposable) {
self.disposables = disposables
}

/// Initialize a `Lifetime` from a lifetime token, which is expected to be
Expand All @@ -56,7 +39,7 @@ public final class Lifetime {
/// - token: A lifetime token for detecting the deinitialization of the
/// associated object.
public convenience init(_ token: Token) {
self.init(.token(ended: token.ended))
self.init(token.disposables)
}

/// Observe the termination of `self`.
Expand All @@ -68,17 +51,7 @@ public final class Lifetime {
/// if `lifetime` has already ended.
@discardableResult
public func observeEnded(_ action: @escaping () -> Void) -> Disposable? {
switch base {
case let .token(ended):
return ended.observe { event in
if event.isTerminating {
action()
}
}

case let .disposable(disposable):
return disposable += action
}
return disposables += action
}
}

Expand All @@ -92,7 +65,11 @@ extension Lifetime {
}

/// A `Lifetime` that has already ended.
public static let empty = Lifetime(.token(ended: .empty))
public static let empty: Lifetime = {
let disposables = CompositeDisposable()
disposables.dispose()
return Lifetime(disposables)
}()
}

extension Lifetime {
Expand All @@ -108,16 +85,14 @@ extension Lifetime {
/// ```
public final class Token {
/// A signal that sends a Completed event when the lifetime ends.
fileprivate let ended: Signal<(), NoError>

private let endedObserver: Signal<(), NoError>.Observer
fileprivate let disposables: CompositeDisposable

public init() {
(ended, endedObserver) = Signal.pipe()
disposables = CompositeDisposable()
}

deinit {
endedObserver.sendCompleted()
disposables.dispose()
}
}
}
7 changes: 0 additions & 7 deletions Sources/Signal.swift
Expand Up @@ -43,13 +43,6 @@ public final class Signal<Value, Error: Swift.Error> {
/// Used to ensure that events are serialized during delivery to observers.
private let sendLock: Lock

internal var hasTerminated: Bool {
switch state {
case .alive: return false
case .terminating, .terminated: return true
}
}

/// Initialize a Signal that will immediately invoke the given generator,
/// then forward events sent to the given observer.
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/SignalProducer.swift
Expand Up @@ -181,7 +181,7 @@ public struct SignalProducer<Value, Error: Swift.Error> {
return
}

startHandler(observer, Lifetime(.disposable(producerDisposable)))
startHandler(observer, Lifetime(producerDisposable))
}
}

Expand Down

0 comments on commit 92dccf5

Please sign in to comment.