Class: Reacto::Subscriptions::TrackerSubscription

Inherits:
Object
  • Object
show all
Includes:
Subscription
Defined in:
lib/reacto/subscriptions/tracker_subscription.rb

Instance Method Summary collapse

Constructor Details

#initialize(notification_tracker, trackable) ⇒ TrackerSubscription

Returns a new instance of TrackerSubscription.



8
9
10
11
12
13
14
15
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 8

def initialize(notification_tracker, trackable)
  @notification_tracker = notification_tracker
  @trackable = trackable

  @subscribed = true
  @subscriptions = []
  @resources = []
end

Instance Method Details

#add(subscription) ⇒ Object



38
39
40
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 38

def add(subscription)
  @subscriptions << subscription
end

#add_resource(resource) ⇒ Object



34
35
36
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 34

def add_resource(resource)
  @resources << resource
end

#on_closeObject



64
65
66
67
68
69
70
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 64

def on_close
  return unless subscribed?

  @notification_tracker.on_close
  @subscriptions.each(&:on_close)
  unsubscribe
end

#on_error(e) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 56

def on_error(e)
  return unless subscribed?

  @notification_tracker.on_error(e)
  @subscriptions.each { |s| s.on_error(e) }
  unsubscribe
end

#on_openObject



42
43
44
45
46
47
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 42

def on_open
  return unless subscribed?

  @notification_tracker.on_open
  @subscriptions.each(&:on_open)
end

#on_value(v) ⇒ Object



49
50
51
52
53
54
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 49

def on_value(v)
  return unless subscribed?

  @notification_tracker.on_value(v)
  @subscriptions.each { |s| s.on_value(v) }
end

#subscribed?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 17

def subscribed?
  @subscribed
end

#unsubscribeObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/reacto/subscriptions/tracker_subscription.rb', line 21

def unsubscribe
  @subscriptions.each(&:unsubscribe)
  return unless subscribed?

  @trackable.off(@notification_tracker)

  @trackable = nil
  @notification_tracker = nil
  @subscribed = false
  @resources.each(&:cleanup)
  @resources = []
end