lib/elevate/elevate.rb in elevate-0.6.0 vs lib/elevate/elevate.rb in elevate-0.7.0

- old
+ new

@@ -1,45 +1,54 @@ module Elevate - # Launches a new asynchronous task. - # - # @param args [Hash] - # input arguments for the task, available to the +task+ block - # - # @return [NSOperation] - # operation representing this task - # - # @api public - def async(args = {}, &block) - with_operation(args, block) do |operation| - queue.addOperation(operation) - end + def self.included(base) + base.extend(ClassMethods) end - private + module ClassMethods + def task(name, options = {}, &block) + task_definitions[name.to_sym] = TaskDefinition.new(name.to_sym, options, &block) + end - def queue - Dispatch.once do - $elevate_queue = NSOperationQueue.alloc.init - $elevate_queue.maxConcurrentOperationCount = 1 + def task_definitions + @task_definitions ||= {} end + end - $elevate_queue + def cancel(name) + active_tasks.each do |task| + if task.name == name + task.cancel + end + end end - def with_operation(args, dsl_block, &block) - dsl = DSL.new(&dsl_block) + def cancel_all + active_tasks.each do |task| + task.cancel + end + end - raise "No task block specified!" unless dsl.task_callback + def launch(name, args = {}) + raise ArgumentError, "args must be a Hash" unless args.is_a? Hash - operation = ElevateOperation.alloc.initWithTarget(dsl.task_callback, args: args) - operation.on_start = Callback.new(self, dsl.start_callback) if dsl.start_callback - operation.on_finish = Callback.new(self, dsl.finish_callback) if dsl.finish_callback - operation.on_update = Callback.new(self, dsl.update_callback) if dsl.update_callback - operation.on_timeout= Callback.new(self, dsl.timeout_callback) if dsl.timeout_callback + definition = self.class.task_definitions[name.to_sym] - operation.timeout = dsl.timeout_interval if dsl.timeout_interval + task = Task.new(definition, self, active_tasks) + task.start(args) - yield operation + task + end - operation + def task_args + @__elevate_task_args + end + + def task_args=(args) + @__elevate_task_args = args + end + + private + + def active_tasks + @__elevate_active_tasks ||= [] end end