lib/async/container/controller.rb in async-container-0.12.0 vs lib/async/container/controller.rb in async-container-0.13.0

- old
+ new

@@ -16,15 +16,33 @@ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +require_relative 'terminator' + require 'async/reactor' module Async module Container class Controller + def initialize + @attached = [] + end + + def attach(controller = nil, &block) + if controller + @attached << controller + end + + if block_given? + @attached << Terminator.new(&block) + end + + return self + end + def async(**options, &block) spawn(**options) do |instance| begin Async::Reactor.run(instance, &block) rescue Interrupt @@ -37,9 +55,13 @@ count.times do async(**options, &block) end return self + end + + def stop(graceful = true) + @attached.each(&:stop) end end end end