Class: Ruck::FiberShred

Inherits:
Object
  • Object
show all
Defined in:
lib/ruck/shred.rb

Overview

See the documentation for CallccShred

Direct Known Subclasses

Shred

Constant Summary collapse

@@current_shreds =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ FiberShred

Returns a new instance of FiberShred.



86
87
88
# File 'lib/ruck/shred.rb', line 86

def initialize(&block)
  @fiber = Fiber.new(&block)
end

Class Method Details

.currentObject



82
83
84
# File 'lib/ruck/shred.rb', line 82

def self.current
  @@current_shreds.last
end

Instance Method Details

#[](*args) ⇒ Object



108
109
110
# File 'lib/ruck/shred.rb', line 108

def [](*args)
  call(*args)
end

#call(*args) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/ruck/shred.rb', line 98

def call(*args)
  return unless @fiber
  @@current_shreds << self
  @fiber.resume
rescue FiberError
  @fiber = nil
ensure
  @@current_shreds.pop
end

#finished?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/ruck/shred.rb', line 112

def finished?
  @fiber.nil?
end

#killObject



120
121
122
# File 'lib/ruck/shred.rb', line 120

def kill
  @fiber = nil
end

#pauseObject



90
91
92
93
94
95
96
# File 'lib/ruck/shred.rb', line 90

def pause
  return unless Shred.current == self
  
  @@current_shreds.pop
  
  Fiber.yield
end

#running?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/ruck/shred.rb', line 116

def running?
  !finished?
end