class Async::Node
Represents a node in a tree, used for nested {Task} instances.
def add_child child
def add_child child ||= Children.new .insert(child) _parent(self)
def annotate(annotation)
def annotate(annotation) if block_given? previous_annotation = @annotation @annotation = annotation yield @annotation = previous_annotation else @annotation = annotation end end
def backtrace(*arguments)
def backtrace(*arguments) nil end
def children?
def children? @children != nil && !@children.empty? end
def consume
If the node has a parent, and is {finished?}, then remove this node from
def consume if parent = @parent and finished? parent.delete_child(self) if @children @children.each do |child| if child.finished? delete_child(child) else parent.add_child(child) end end @children = nil end parent.consume end end
def delete_child(child)
def delete_child(child) .delete(child) _parent(nil)
def description
def description @object_name ||= "#{self.class}:0x#{object_id.to_s(16)}#{@transient ? ' transient' : nil}" if @annotation "#{@object_name} #{@annotation}" else @object_name end end
def finished?
-
(Boolean)
-
def finished? @children.nil? || @children.finished? end
def initialize(parent = nil, annotation: nil, transient: false)
-
parent
(Node, nil
) -- This node will attach to the given parent.
def initialize(parent = nil, annotation: nil, transient: false) @parent = nil @children = nil @annotation = annotation @object_name = nil @transient = transient @head = nil @tail = nil if parent parent.add_child(self) end end
def parent=(parent)
-
(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.delete_child(self) @parent = nil end if parent parent.add_child(self) end return self end
def print_backtrace(out, indent, node)
def print_backtrace(out, indent, node) if backtrace = node.backtrace backtrace.each_with_index do |line, index| out.puts "#{indent}#{index.zero? ? "→ " : " "}#{line}" end end end
def print_hierarchy(out = $stdout, backtrace: true)
def print_hierarchy(out = $stdout, backtrace: true) self.traverse do |node, level| indent = "\t" * level out.puts "#{indent}#{node}" print_backtrace(out, indent, node) if backtrace end end
def set_parent parent
def set_parent parent parent
def stop
def stop @children&.each(&:stop) end
def to_s
def to_s "\#<#{description}>" end
def transient?
def transient? @transient end
def traverse(level = 0, &block)
- 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