Class: Async::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/async/node.rb

Overview

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

Direct Known Subclasses

Reactor, Task

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Node

Create a new node in the tree.

Parameters:

  • parent (Node, nil) (defaults to: nil)

    This node will attach to the given parent.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/async/node.rb', line 30

def initialize(parent = nil)
	@children = nil
	@parent = nil
	
	@annotation = nil
	@object_name = nil
	
	if parent
		self.parent = parent
	end
end

Instance Attribute Details

#annotationObject (readonly)

A useful identifier for the current node.



49
50
51
# File 'lib/async/node.rb', line 49

def annotation
  @annotation
end

#childrenObject (readonly)



46
47
48
# File 'lib/async/node.rb', line 46

def children
  @children
end

#parentObject



43
44
45
# File 'lib/async/node.rb', line 43

def parent
  @parent
end

Instance Method Details

#annotate(annotation) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/async/node.rb', line 51

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

#consumeObject

If the node has a parent, and is #finished?, then remove this node from the parent.



113
114
115
116
117
118
119
# File 'lib/async/node.rb', line 113

def consume
	if @parent and finished?
		@parent.reap(self)
		@parent.consume
		@parent = nil
	end
end

#descriptionObject



62
63
64
65
66
67
68
69
70
# File 'lib/async/node.rb', line 62

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

#finished?Boolean

Whether the node can be consumed safely. By default, checks if the children set is empty.

Returns:

  • (Boolean)


107
108
109
# File 'lib/async/node.rb', line 107

def finished?
	@children.nil? or @children.empty?
end

#inspectObject



76
77
78
# File 'lib/async/node.rb', line 76

def inspect
	to_s
end


141
142
143
144
145
# File 'lib/async/node.rb', line 141

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

#reap(child) ⇒ Object

Remove a given child node.

Parameters:



123
124
125
# File 'lib/async/node.rb', line 123

def reap(child)
	@children.delete(child)
end

#stopObject



137
138
139
# File 'lib/async/node.rb', line 137

def stop
	@children&.each(&:stop)
end

#to_sObject



72
73
74
# File 'lib/async/node.rb', line 72

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

#traverse(level = 0) {|node, level| ... } ⇒ Object

Traverse the tree.

Yields:

  • (node, level)

    The node and the level relative to the given root.



129
130
131
132
133
134
135
# File 'lib/async/node.rb', line 129

def traverse(level = 0, &block)
	yield self, level
	
	@children&.each do |child|
		child.traverse(level + 1, &block)
	end
end