Class: RxRuby::SerialSubscription

Inherits:
Object
  • Object
show all
Includes:
Subscription
Defined in:
lib/rx_ruby/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

Methods included from Subscription

create, #dispose, empty

Constructor Details

#initializeSerialSubscription

Returns a new instance of SerialSubscription.



11
12
13
14
15
# File 'lib/rx_ruby/subscriptions/serial_subscription.rb', line 11

def initialize
  @gate = Mutex.new
  @current = nil
  @unsubscribed = false
end

Instance Method Details

#subscriptionObject

Gets the underlying subscription.



25
26
27
# File 'lib/rx_ruby/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_ruby/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

#unsubscribeObject

Unsubscribes the current underlying subscription and all future subscriptions.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rx_ruby/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.

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/rx_ruby/subscriptions/serial_subscription.rb', line 18

def unsubscribed?
  @gate.synchronize do
    return @unsubscribed
  end
end