Class: Emit::Process
- Inherits:
-
Object
- Object
- Emit::Process
- Defined in:
- lib/emit/process.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fiber ⇒ Object
readonly
Returns the value of attribute fiber.
-
#return_value ⇒ Object
readonly
Returns the value of attribute return_value.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #*(number) ⇒ Object
- #active? ⇒ Boolean
- #executed? ⇒ Boolean
-
#initialize(*args, **kwargs, &block) ⇒ Process
constructor
A new instance of Process.
- #notify(new_state) ⇒ Object
- #run ⇒ Object
- #start ⇒ Object
- #transfer ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(*args, **kwargs, &block) ⇒ Process
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/emit/process.rb', line 8 def initialize(*args, **kwargs, &block) fail ArgumentError.new("Must have a block as argument for Emit::Process.") unless block_given? @block = block @args = args @kwargs = kwargs @state = nil @executed = false @return_value = nil @fiber = nil end |
Instance Attribute Details
#fiber ⇒ Object (readonly)
Returns the value of attribute fiber.
6 7 8 |
# File 'lib/emit/process.rb', line 6 def fiber @fiber end |
#return_value ⇒ Object (readonly)
Returns the value of attribute return_value.
6 7 8 |
# File 'lib/emit/process.rb', line 6 def return_value @return_value end |
#state ⇒ Object
Returns the value of attribute state.
5 6 7 |
# File 'lib/emit/process.rb', line 5 def state @state end |
Instance Method Details
#*(number) ⇒ Object
68 69 70 |
# File 'lib/emit/process.rb', line 68 def *(number) [*number.times.map { self.dup }] end |
#active? ⇒ Boolean
60 61 62 |
# File 'lib/emit/process.rb', line 60 def active? @state == :active end |
#executed? ⇒ Boolean
64 65 66 |
# File 'lib/emit/process.rb', line 64 def executed? @executed end |
#notify(new_state) ⇒ Object
25 26 27 28 |
# File 'lib/emit/process.rb', line 25 def notify(new_state) @state = new_state Scheduler.activate(self) unless Scheduler.current == self end |
#run ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/emit/process.rb', line 38 def run @executed = false begin if @block.arity.negative? @return_value = @block.call(*@args, **@kwargs) elsif @block.arity > 0 @return_value = @block.call(*@args) else @return_value = @block.call end rescue ::Emit::ChannelPoisonedException => e propagate_poison raise e rescue ::Emit::ChannelRetiredException => e propagate_retire raise e end @executed = true end |
#start ⇒ Object
30 31 32 |
# File 'lib/emit/process.rb', line 30 def start @fiber = Fiber.new { run } end |
#transfer ⇒ Object
34 35 36 |
# File 'lib/emit/process.rb', line 34 def transfer @fiber.transfer end |
#wait ⇒ Object
21 22 23 |
# File 'lib/emit/process.rb', line 21 def wait Scheduler.get_next.transfer while active? end |