Class: Reacto::SharedTrackable

Inherits:
Trackable show all
Defined in:
lib/reacto/shared_trackable.rb

Constant Summary

Constants inherited from Trackable

Trackable::EXECUTOR_ALIASES, Trackable::TOPICS

Instance Method Summary collapse

Methods inherited from Trackable

#[], #act, #all?, #any?, #append, #await, #buffer, #chunk, #chunk_while, close, combine, #combine, combine_last, #combine_last, combine_latest, concat, #concat, #count, #cycle, #delay, #delay_each, #depend_on, #diff, #do_track, #drop, #drop_errors, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, enumerable, error, #execute_on, #find, #find_index, #first, #flat_map, #flat_map_latest, #flatten, #flatten_labeled, #grep, #grep_v, #group_by_label, #include?, #inject, interval, #last, later, #lazy, #lift, make, #map, #max, #max_by, #merge, #min, #min_by, #minmax, #minmax_by, never, #none?, #on, #one?, #partition, #prepend, #reject, repeat, #rescue_and_replace_error, #rescue_and_replace_error_with, #retry, #retry_when, #select, #slice, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #split_labeled, #take, #take_while, #throttle, #to_a, #to_h, #track_on, #uniq, value, #wrap, zip, #zip

Constructor Details

#initialize(executor = nil, activate_on_subscribe = false, &block) ⇒ SharedTrackable

Returns a new instance of SharedTrackable.



5
6
7
8
9
10
# File 'lib/reacto/shared_trackable.rb', line 5

def initialize(executor = nil, activate_on_subscribe = false, &block)
  super(executor, &block)

  @activate_on_subscribe = activate_on_subscribe
  @active = false
end

Instance Method Details

#activate!Object



34
35
36
37
38
39
40
41
42
# File 'lib/reacto/shared_trackable.rb', line 34

def activate!
  return if @shared_subscription.nil?
  return if @active

  @active = true
  do_track(shared_subscription)

  self
end

#activate_on_subscribeObject



44
45
# File 'lib/reacto/shared_trackable.rb', line 44

def activate_on_subscribe
end

#off(notification_tracker = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/reacto/shared_trackable.rb', line 12

def off(notification_tracker = nil)
  shared_subscription.subscriptions.reject! do |subscription|
    !subscription.subscribed?
  end

  return if shared_subscription.subscriptions.count > 0

  shared_subscription.unsubscribe
  @shared_subscription = nil
  @active = false
end

#track(notification_tracker) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/reacto/shared_trackable.rb', line 24

def track(notification_tracker)
  subscription =
    Subscriptions::TrackerSubscription.new(notification_tracker, self)

  shared_subscription.add(subscription)
  activate! if @activate_on_subscribe

  Subscriptions::SubscriptionWrapper.new(subscription)
end