Class: Rx::RefCountSubscription
- Inherits:
-
Object
- Object
- Rx::RefCountSubscription
- Defined in:
- lib/rx/subscriptions/ref_count_subscription.rb
Overview
Represents a subscription resource that only disposes its underlying subscription resource when all dependent subscription objects have been unsubscribed.
Defined Under Namespace
Classes: InnerSubscription
Instance Method Summary collapse
-
#initialize(subscription) ⇒ RefCountSubscription
constructor
A new instance of RefCountSubscription.
- #release ⇒ Object
-
#subscription ⇒ Object
Returns a dependent subscription that when disposed decreases the refcount on the underlying subscription.
-
#unsubscribe ⇒ Object
Unsubscribes the underlying subscription only when all dependent subscriptions have been unsubscribed.
-
#unsubscribed? ⇒ Boolean
Gets a value that indicates whether the object is disposed.
Constructor Details
#initialize(subscription) ⇒ RefCountSubscription
Returns a new instance of RefCountSubscription.
11 12 13 14 15 16 17 18 |
# File 'lib/rx/subscriptions/ref_count_subscription.rb', line 11 def initialize(subscription) raise ArgumentError.new 'Subscription cannot be nil' unless subscription @subscription = subscription @primary_unsubscribed = false @gate = Mutex.new @count = 0 end |
Instance Method Details
#release ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rx/subscriptions/ref_count_subscription.rb', line 56 def release subscription = nil @gate.synchronize do if @subscription @count -= 1 if @primary_unsubscribed && @count == 0 subscription = @subscription @subscription = nil end end end subscription.unsubscribe if subscription end |
#subscription ⇒ Object
Returns a dependent subscription that when disposed decreases the refcount on the underlying subscription.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rx/subscriptions/ref_count_subscription.rb', line 26 def subscription @gate.synchronize do if @subscription @count += 1 return InnerSubscription.new self else return Subscription.empty end end end |
#unsubscribe ⇒ Object
Unsubscribes the underlying subscription only when all dependent subscriptions have been unsubscribed.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rx/subscriptions/ref_count_subscription.rb', line 38 def unsubscribe subscription = nil @gate.synchronize do if @subscription unless @primary_unsubscribed @primary_unsubscribed = true if @count == 0 subscription = @subscription @subscription = nil end end end end subscription.unsubscribe if subscription end |
#unsubscribed? ⇒ Boolean
Gets a value that indicates whether the object is disposed.
21 22 23 |
# File 'lib/rx/subscriptions/ref_count_subscription.rb', line 21 def unsubscribed? @subscription.nil? end |