Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Store handlers in Hash; release resources before invoking handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgreen committed Sep 27, 2013
1 parent ff7905d commit 7e65997
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions lib/elevate/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ def initialize(controller, active_tasks, body)
@active_tasks = active_tasks
@operation = nil
@channel = Channel.new(method(:on_update))

@body = body
@on_start = nil
@on_finish = nil
@on_update = nil
@handlers = { body: body }
end

def cancel
Expand All @@ -19,19 +15,19 @@ def cancel
end

def on_finish=(block)
@on_finish = block
@handlers[:on_finish] = block
end

def on_start=(block)
@on_start = block
@handlers[:on_start] = block
end

def on_update=(block)
@on_update = block
@handlers[:on_update] = block
end

def start(args)
@operation = ElevateOperation.alloc.initWithTarget(@body, args: args, channel: @channel)
@operation = ElevateOperation.alloc.initWithTarget(@handlers[:body], args: args, channel: @channel)

@operation.addObserver(self, forKeyPath: "isFinished", options: NSKeyValueObservingOptionNew, context: nil)
queue.addOperation(@operation)
Expand Down Expand Up @@ -59,22 +55,22 @@ def observeValueForKeyPath(path, ofObject: operation, change: change, context: c
end

def on_start
if @on_start
@controller.instance_eval(&@on_start)
@on_start = nil
if handler = @handlers[:on_start]
@controller.instance_eval(&handler)
end
end

def on_finish
if @on_finish
@controller.instance_exec(@operation.result, @operation.exception, &@on_finish)
@on_finish = nil
end
operation = @operation

@operation.removeObserver(self, forKeyPath: "isFinished")
@operation = nil

@active_tasks.delete(self)

if handler = @handlers[:on_finish]
@controller.instance_exec(operation.result, operation.exception, &handler)
end
end

def on_update(args)
Expand All @@ -83,7 +79,9 @@ def on_update(args)
return
end

@controller.instance_exec(*args, &@on_update) if @on_update
if handler = @handlers[:on_update]
@controller.instance_exec(*args, &handler)
end
end
end
end

0 comments on commit 7e65997

Please sign in to comment.