Class: Rx::SerialSubscription
- Inherits:
-
Object
- Object
- Rx::SerialSubscription
- Includes:
- Subscription
- Defined in:
- lib/rx/subscriptions/serial_subscription.rb
Overview
Represents a subscription resource whose underlying subscription resource can be replaced by another subscription resource, causing automatic disposal of the previous underlying subscription resource.
Instance Method Summary collapse
-
#initialize ⇒ SerialSubscription
constructor
A new instance of SerialSubscription.
-
#subscription ⇒ Object
Gets the underlying subscription.
-
#subscription=(new_subscription) ⇒ Object
Sets the underlying subscription.
-
#unsubscribe ⇒ Object
Unsubscribes the current underlying subscription and all future subscriptions.
-
#unsubscribed? ⇒ Boolean
Gets a value that indicates whether the object is unsubscribed.
Methods included from Subscription
Constructor Details
#initialize ⇒ SerialSubscription
Returns a new instance of SerialSubscription.
11 12 13 14 15 |
# File 'lib/rx/subscriptions/serial_subscription.rb', line 11 def initialize @gate = Mutex.new @current = nil @unsubscribed = false end |
Instance Method Details
#subscription ⇒ Object
Gets the underlying subscription.
25 26 27 |
# File 'lib/rx/subscriptions/serial_subscription.rb', line 25 def subscription @current end |
#subscription=(new_subscription) ⇒ Object
Sets the underlying subscription.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rx/subscriptions/serial_subscription.rb', line 30 def subscription=(new_subscription) should_unsubscribe = false old = nil @gate.synchronize do should_unsubscribe = @unsubscribed unless should_unsubscribe old = @current @current = new_subscription end end old.unsubscribe if old new_subscription.unsubscribe if should_unsubscribe && !new_subscription.nil? end |
#unsubscribe ⇒ Object
Unsubscribes the current underlying subscription and all future subscriptions.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rx/subscriptions/serial_subscription.rb', line 46 def unsubscribe old = nil @gate.synchronize do unless @unsubscribed @unsubscribed = true old = @current @current = nil end end old.unsubscribe if old end |
#unsubscribed? ⇒ Boolean
Gets a value that indicates whether the object is unsubscribed.
18 19 20 21 22 |
# File 'lib/rx/subscriptions/serial_subscription.rb', line 18 def unsubscribed? @gate.synchronize do return @unsubscribed end end |