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, annotation: nil, transient: false) ⇒ Node

Create a new node in the tree.

Parameters:

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

    This node will attach to the given parent.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/async/node.rb', line 157

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

Instance Attribute Details

#annotationObject (readonly)

A useful identifier for the current node.



186
187
188
# File 'lib/async/node.rb', line 186

def annotation
  @annotation
end

#childrenObject (readonly)



183
184
185
# File 'lib/async/node.rb', line 183

def children
  @children
end

#headObject

You should not directly rely on these pointers but instead use #children. List pointers:



176
177
178
# File 'lib/async/node.rb', line 176

def head
  @head
end

#parentObject



180
181
182
# File 'lib/async/node.rb', line 180

def parent
  @parent
end

#tailObject

Returns the value of attribute tail.



177
178
179
# File 'lib/async/node.rb', line 177

def tail
  @tail
end

Instance Method Details

#annotate(annotation) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/async/node.rb', line 198

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

#children?Boolean

Whether there are children?

Returns:

  • (Boolean)


189
190
191
# File 'lib/async/node.rb', line 189

def children?
	@children != nil && !@children.empty?
end

#consumeObject

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/async/node.rb', line 265

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

#descriptionObject



209
210
211
212
213
214
215
216
217
# File 'lib/async/node.rb', line 209

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

#finished?Boolean

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

Returns:

  • (Boolean)


259
260
261
# File 'lib/async/node.rb', line 259

def finished?
	@children.nil? || @children.finished?
end


299
300
301
302
303
# File 'lib/async/node.rb', line 299

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

#stopObject



295
296
297
# File 'lib/async/node.rb', line 295

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

#to_sObject



219
220
221
# File 'lib/async/node.rb', line 219

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

#transient?Boolean

Is this node transient?

Returns:

  • (Boolean)


194
195
196
# File 'lib/async/node.rb', line 194

def transient?
	@transient
end

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

Traverse the tree.

Yields:

  • (node, level)

    The node and the level relative to the given root.



287
288
289
290
291
292
293
# File 'lib/async/node.rb', line 287

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