-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deallocate Trigger Signal #99
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,33 @@ extension NSObject { | |
// Errors aren't possible, but the compiler doesn't know that. | ||
assertionFailure("Unexpected error from KVO signal: \(error)") | ||
return .empty | ||
} | ||
} | ||
} | ||
|
||
/// Creates a signal that will be triggered when the object | ||
/// is deallocated. | ||
@warn_unused_result(message="Did you forget to call `observe` on the signal?") | ||
public final func willDeallocSignal() -> Signal<(), NoError> { | ||
return self | ||
.rac_willDeallocSignal() | ||
.toTriggerSignal() | ||
} | ||
} | ||
|
||
extension SignalProducerType { | ||
/// Forwards events from `self` until `object` is deallocated, | ||
/// at which point the returned producer will complete. | ||
@warn_unused_result(message="Did you forget to call `start` on the producer?") | ||
public final func takeUntilObjectDeallocates(object: NSObject) -> SignalProducer<Self.Value, Self.Error> { | ||
return self.lift { $0.takeUntilObjectDeallocates(object) } | ||
} | ||
} | ||
|
||
extension SignalType { | ||
/// Forwards events from `self` until `object` is deallocated, | ||
/// at which point the returned signal will complete. | ||
@warn_unused_result(message="Did you forget to call `observe` on the signal?") | ||
public final func takeUntilObjectDeallocates(object: NSObject) -> Signal<Self.Value, Self.Error> { | ||
return self.takeUntil(object.willDeallocSignal()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem like this buys us much. I feel like it reads nicer as |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// RACSignal.swift | ||
// Rex | ||
// | ||
// Created by Rui Peres on 14/04/2016. | ||
// Copyright © 2016 Neil Pankey. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Result | ||
import ReactiveCocoa | ||
|
||
extension RACSignal { | ||
|
||
/// Converts `self` into a `Signal`. | ||
/// | ||
/// Because the operator can't know whether `self` is hot or cold, | ||
/// for certain things, like event streams (see `UIControl.signalForControlEvents`) | ||
/// use this method to be able to expose these inherently hot streams | ||
/// as `Signal`s. | ||
@warn_unused_result(message="Did you forget to call `observe` on the signal?") | ||
public func toSignalAssumingHot() -> Signal<AnyObject?, NSError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept the original comments from Nacho with a slight modification, but yes I agree with your suggestion. |
||
return Signal { observer in | ||
return self.toSignalProducer().start(observer) | ||
} | ||
} | ||
|
||
/// Converts `self` into a `Signal`, that can be used | ||
/// with the `takeUntil` operator, or as an "activation" signal. | ||
/// (e.g. a button) | ||
@warn_unused_result(message="Did you forget to call `observe` on the signal?") | ||
public final func toTriggerSignal() -> Signal<(), NoError> { | ||
return self | ||
.toSignalAssumingHot() | ||
.map { _ in () } | ||
.ignoreError() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this actually ignore errors? Notice that my operators don't actually ignore unexpected errors, they assert they won't happen. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When an error event is sent, it will complete (by default case .Failed:
observer.action(replacement) Your operator is slightly more aggressive. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could/should be a property. It's idempotent.