lib/async/task.rb in async-2.0.3 vs lib/async/task.rb in async-2.1.0

- old
+ new

@@ -1,26 +1,12 @@ # frozen_string_literal: true -# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com> -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# 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. +# Released under the MIT License. +# Copyright, 2017-2022, by Samuel Williams. +# Copyright, 2017, by Kent Gruber. +# Copyright, 2017, by Devin Christensen. +# Copyright, 2020, by Patrik Wenger. require 'fiber' require_relative 'node' require_relative 'condition' @@ -115,11 +101,13 @@ # Begin the execution of the task. def run(*arguments) if @status == :initialized @status = :running - schedule(arguments) + schedule do + @block.call(self, *arguments) + end else raise RuntimeError, "Task already running!" end end @@ -245,15 +233,15 @@ @status = :stopped stop_children(true) end - def schedule(arguments) + def schedule(&block) @fiber = Fiber.new do set! begin - @result = @block.call(self, *arguments) + @result = yield @status = :complete # Console.logger.debug(self) {"Task was completed with #{@children.size} children!"} rescue Stop stop! rescue StandardError => error