Class: ObjectTracker

Inherits:
Object show all
Defined in:
lib/volt/reactive/object_tracker.rb

Constant Summary collapse

@@queue =
{}
@@cache_enabled =
false
@@cache_version =
0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(main_object) ⇒ ObjectTracker

Returns a new instance of ObjectTracker.



28
29
30
31
# File 'lib/volt/reactive/object_tracker.rb', line 28

def initialize(main_object)
  @main_object = main_object
  @enabled = false
end

Class Method Details

.cache_enabledObject



7
8
9
# File 'lib/volt/reactive/object_tracker.rb', line 7

def self.cache_enabled
  @@cache_enabled
end

.cache_versionObject



20
21
22
# File 'lib/volt/reactive/object_tracker.rb', line 20

def self.cache_version
  @@cache_version
end

.clear_cacheObject



24
25
26
# File 'lib/volt/reactive/object_tracker.rb', line 24

def self.clear_cache
  @@cache_version = (@@cache_version || 0) + 1
end

.disable_cacheObject



16
17
18
# File 'lib/volt/reactive/object_tracker.rb', line 16

def self.disable_cache
  @@cache_enabled = false
end

.enable_cacheObject



11
12
13
14
# File 'lib/volt/reactive/object_tracker.rb', line 11

def self.enable_cache
  clear_cache
  @@cache_enabled = true
end

.process_queueObject

Run through the queue and update the followers for each



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/volt/reactive/object_tracker.rb', line 43

def self.process_queue
  return if @@queue.size == 0
  # puts "PROCESS QUEUE: #{@@queue.size}"
  queue = @@queue

  # puts "Update Followers #{@@queue.size}"

  # Clear before running incase someone adds during
  @@queue = {}
  # self.enable_cache

  queue.each_pair do |object_tracker,val|
    object_tracker.update_followers
  end

  # self.disable_cache

  # puts "UPDATED FOLLOWERS"
end

.queueObject



33
34
35
# File 'lib/volt/reactive/object_tracker.rb', line 33

def self.queue
  @@queue
end

Instance Method Details

#disable!Object



70
71
72
73
74
# File 'lib/volt/reactive/object_tracker.rb', line 70

def disable!
  remove_followers
  @@queue.delete(self)
  @enabled = false
end

#enable!Object



63
64
65
66
67
68
# File 'lib/volt/reactive/object_tracker.rb', line 63

def enable!
  unless @enabled
    @enabled = true
    queue_update
  end
end

#queue_updateObject



37
38
39
40
# File 'lib/volt/reactive/object_tracker.rb', line 37

def queue_update
  # puts "Queue: #{@main_object.inspect}"
  @@queue[self] = true
end

#remove_followersObject

Remove follower



98
99
100
101
102
103
104
105
106
# File 'lib/volt/reactive/object_tracker.rb', line 98

def remove_followers
  # Remove from previous
  if @cached_current_obj
    @current_obj_chain_listener.remove
    @current_obj_chain_listener = nil

    @cached_current_obj = nil
  end
end

#update_followersObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/volt/reactive/object_tracker.rb', line 76

def update_followers
  if @enabled
    current_obj = @main_object.cur#(true)

    # puts "UPDATE ON #{current_obj.inspect}"

    if !@cached_current_obj || current_obj.object_id != @cached_current_obj.object_id
      remove_followers

      # Add to current
      should_attach = current_obj.respond_to?(:on)
      if should_attach
        @cached_current_obj = current_obj
        # puts "ATTACH: #{@cached_current_obj}"
        @current_obj_chain_listener = @main_object.event_chain.add_object(@cached_current_obj)
      end
    end
  else
  end
end