Sha256: a8b45e1ef5d2d42d49badd2f7c56ed52f0e47902fc5fac85cd02a409d3914ba7

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module Elevate
  class ElevateOperation < NSOperation
    def initWithTarget(target, args:args)
      if init()
        @coordinator = IOCoordinator.new
        @context = TaskContext.new(args, &target)
        @update_callback = nil
        @finish_callback = nil

        setCompletionBlock(lambda do
          if @finish_callback
            @finish_callback.call(@result, @exception) unless isCancelled
          end

          Dispatch::Queue.main.sync do
            @context = nil
            @update_callback = nil
            @finish_callback = nil
          end
        end)
      end

      self
    end

    def cancel
      @coordinator.cancel

      super
    end

    def inspect
      details = []
      details << "<canceled>" if @coordinator.cancelled?

      "#<#{self.class.name}: #{details.join(" ")}>"
    end

    def log(line)
      puts line unless RUBYMOTION_ENV == "test"
    end

    def main
      log " START: #{inspect}"

      @coordinator.install

      begin
        unless @coordinator.cancelled?
          @result = @context.execute do |*args|
            @update_callback.call(*args) if @update_callback
          end
        end

      rescue Exception => e
        @exception = e
      end

      @coordinator.uninstall

      log "FINISH: #{inspect}"
    end

    attr_reader :exception
    attr_reader :result

    def on_finish=(callback)
      @finish_callback = callback
    end

    def on_start=(callback)
      start_callback = callback
      start_callback.retain

      Dispatch::Queue.main.async do
        start_callback.call unless isCancelled
        start_callback.release
      end
    end

    def on_update=(callback)
      @update_callback = callback
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
elevate-0.5.0 lib/elevate/operation.rb