Class: Rubactive::DiscreteValueStream
- Inherits:
-
ReactiveNode
- Object
- ReactiveNode
- Rubactive::DiscreteValueStream
- Defined in:
- lib/rubactive/rubactive.rb
Overview
A DiscreteValueStream represents a stream of values. When a value is added to the stream, other DiscreteValueStreams may react if they follow this stream.
Streams are explicitly created with DiscreteValueStream.manual or DiscreteValueStream.follows. In the first case, values are added only with add_value. In the second, values can also be added in reaction to the streams being followed.
Streams can also be implicity created by sending other streams unrecognized messages.
origin = DiscreteValueStream.manual
follower = origin + 1
The previous definition of the follower stream is equivalent to this:
follower = DiscreteValueStream.follows(origin) { | o | o + 1 }
Constant Summary
Constants inherited from ReactiveNode
Instance Attribute Summary
Attributes inherited from ReactiveNode
Class Method Summary collapse
-
.follows(*streams, &block) ⇒ Object
Create a stream that reacts to one or more other streams.
-
.manual ⇒ Object
Create an empty value stream.
Instance Method Summary collapse
-
#add_value(new_value) ⇒ Object
Place a new value on the stream.
-
#empty? ⇒ Boolean
True iff no value has ever been added to the stream.
-
#most_recent_value ⇒ Object
Retrieve last value added to the stream.
-
#on_addition(&callback) ⇒ Object
Run a callback when a new value is added.
Methods inherited from ReactiveNode
blank, #initialize, #just_values, #method_missing, #on_change, #propagate, #recalculate, #tell_earlier_nodes_about_me, #this_node_is_later_than_you
Constructor Details
This class inherits a constructor from Rubactive::ReactiveNode
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rubactive::ReactiveNode
Class Method Details
.follows(*streams, &block) ⇒ Object
Create a stream that reacts to one or more other streams.
The addition of values to any of the streams will cause the block to be called with their most recent values. The result is added to this stream (as with add_value).
Example:
origin = DiscreteValueStream.manual
follower = DiscreteValueStream.follows(origin) { | o | o+1 }
origin.add_value(5)
follower.most_recent_value #=> 6
If no block is given, this stream should be following only one other stream. The value just added to that stream is also added to this one. – Defined here so that I can write special-purpose documentation.
214 215 216 |
# File 'lib/rubactive/rubactive.rb', line 214 def self.follows(*streams, &block) super end |
.manual ⇒ Object
Create an empty value stream
Use add_value to insert values into the stream.
221 222 223 224 225 |
# File 'lib/rubactive/rubactive.rb', line 221 def self.manual follows { raise "Incorrect use of recalculation in a manual event stream" } end |
Instance Method Details
#add_value(new_value) ⇒ Object
Place a new value on the stream
244 245 246 |
# File 'lib/rubactive/rubactive.rb', line 244 def add_value(new_value) # this? self.value = new_value end |
#empty? ⇒ Boolean
True iff no value has ever been added to the stream.
249 250 251 |
# File 'lib/rubactive/rubactive.rb', line 249 def empty? most_recent_value == DEFAULT_VALUE end |
#most_recent_value ⇒ Object
Retrieve last value added to the stream
Earlier values are inaccessible.
It is an error to ask for the value of an #empty? stream.
241 |
# File 'lib/rubactive/rubactive.rb', line 241 def most_recent_value; @value; end |
#on_addition(&callback) ⇒ Object
Run a callback when a new value is added.
This is an interface to the non-reactive world. When a new value is added (whether with add_value or in reaction to a followed stream), the callback is called and given that value. The callback will typically do something with the value, like add it to a GUI.
233 |
# File 'lib/rubactive/rubactive.rb', line 233 def on_addition(&callback); on_change(&callback); end |