class Async::Node

Represents a node in a tree, used for nested {Task} instances.

def add_child child

def add_child child
 ||= Set.new
 << child

def annotate(annotation)

def annotate(annotation)
	if block_given?
		previous_annotation = @annotation
		@annotation = annotation
		yield
		@annotation = previous_annotation
	else
		@annotation = annotation
	end
end

def consume

the parent.
If the node has a parent, and is {finished?}, then remove this node from
def consume
	if @parent and finished?
		@parent.reap(self)
		@parent.consume
		@parent = nil
	end
end

def description

def description
	@object_name ||= "#{self.class}:0x#{object_id.to_s(16)}"
	
	if @annotation
		"#{@object_name} #{@annotation}"
	else
		@object_name
	end
end

def finished?

Returns:
  • (Boolean) -
def finished?
	@children.nil? or @children.empty?
end

def initialize(parent = nil)

Parameters:
  • parent (Node, nil) -- This node will attach to the given parent.
def initialize(parent = nil)
	@children = nil
	@parent = nil
	
	@annotation = nil
	@object_name = nil
	
	if parent
		self.parent = parent
	end
end

def parent=(parent)

Returns:
  • (self) -

Parameters:
  • parent (Node, nil) -- the parent to attach to, or nil to detach.
def parent=(parent)
	return if @parent.equal?(parent)
	
	if @parent
		@parent.reap(self)
		@parent = nil
	end
	
	if parent
		@parent = parent
		@parent.add_child(self)
	end
	
	return self
end

def print_hierarchy(out = $stdout)

def print_hierarchy(out = $stdout)
	self.traverse do |node, level|
		out.puts "#{"\t" * level}#{node}"
	end
end

def reap(child)

Parameters:
  • child (Node) --
def reap(child)
	@children.delete(child)
end

def to_s

def to_s
	"\#<#{description}>"
end

def traverse(level = 0, &block)

Other tags:
    Yield: - The node and the level relative to the given root.
def traverse(level = 0, &block)
	yield self, level
	
	@children&.each do |child|
		child.traverse(level + 1, &block)
	end
end