Class: Elevate::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/elevate/task.rb

Instance Method Summary collapse

Constructor Details

#initialize(definition, controller, active_tasks) ⇒ Task

Returns a new instance of Task.



3
4
5
6
7
8
9
10
11
# File 'lib/elevate/task.rb', line 3

def initialize(definition, controller, active_tasks)
  @definition = definition
  @controller = WeakRef.new(controller)
  @active_tasks = active_tasks
  @operation = nil
  @channel = Channel.new(method(:on_update))
  @args = nil
  @timer = nil
end

Instance Method Details

#cancelObject



13
14
15
16
17
18
19
20
21
# File 'lib/elevate/task.rb', line 13

def cancel
  if @operation
    @operation.cancel

    if @timer
      @timer.invalidate
    end
  end
end

#nameObject



23
24
25
# File 'lib/elevate/task.rb', line 23

def name
  @definition.name
end

#start(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/elevate/task.rb', line 27

def start(args)
  @operation = ElevateOperation.alloc.initWithTarget(@definition.handlers[:background],
                                                     args: args,
                                                     channel: WeakRef.new(@channel))

  @operation.addObserver(self, forKeyPath: "isFinished", options: NSKeyValueObservingOptionNew, context: nil)
  queue.addOperation(@operation)
  @active_tasks << self

  @args = args

  if interval = @definition.options[:timeout_interval]
    @timer = NSTimer.scheduledTimerWithTimeInterval(interval,
                                                    target: self,
                                                    selector: :"on_timeout:",
                                                    userInfo: nil,
                                                    repeats: false)
  end

  performSelectorOnMainThread(:on_start, withObject: nil, waitUntilDone: false)
end