Class: Emit::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/emit/process.rb

Direct Known Subclasses

MainProcess

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fiberObject (readonly)

Returns the value of attribute fiber.



6
7
8
# File 'lib/emit/process.rb', line 6

def fiber
  @fiber
end

#return_valueObject (readonly)

Returns the value of attribute return_value.



6
7
8
# File 'lib/emit/process.rb', line 6

def return_value
  @return_value
end

#stateObject

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

#runObject



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

#startObject



30
31
32
# File 'lib/emit/process.rb', line 30

def start
  @fiber = Fiber.new { run }
end

#transferObject



34
35
36
# File 'lib/emit/process.rb', line 34

def transfer
  @fiber.transfer
end

#waitObject



21
22
23
# File 'lib/emit/process.rb', line 21

def wait
  Scheduler.get_next.transfer while active?
end