Class: ViewModel::GarbageCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/garbage_collection.rb

Class Method Summary collapse

Class Method Details

.garbage_collect_references!(serialization) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/view_model/garbage_collection.rb', line 5

def garbage_collect_references!(serialization)
  return unless serialization.has_key?('references')

  roots      = serialization.except('references')
  references = serialization['references']

  worklist = Set.new(collect_references(roots))
  visited  = Set.new

  while (live = worklist.first)
    worklist.delete(live)
    visited << live
    collect_references(references[live]) do |ref|
      worklist << ref unless visited.include?(ref)
    end
  end

  references.keep_if { |ref, _val| visited.include?(ref) }
end