class Rx::RefCountSubscription

Represents a subscription resource that only disposes its underlying subscription resource when all dependent subscription objects have been unsubscribed.

Public Class Methods

new(subscription) click to toggle source
# 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

Public Instance Methods

release() click to toggle source
# 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() click to toggle source

Returns a dependent subscription that when disposed decreases the refcount on the underlying subscription.

# 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() click to toggle source

Unsubscribes the underlying subscription only when all dependent subscriptions have been unsubscribed.

# 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?() click to toggle source

Gets a value that indicates whether the object is disposed.

# File lib/rx/subscriptions/ref_count_subscription.rb, line 21
def unsubscribed?
  @subscription.nil?
end