Sha256: bb5edcc632dd090dc0f3770e97c631fd317f178829e97ddfb1f98bb3d625bacc

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 KB

Contents

# The tasks class provides an interface to call tasks on
# the backend server.
class Tasks
  def initialize(page)
    @page = page
    @callback_id = 0
    @callbacks = {}
    
    page.channel.on('message') do |_, *args|
      received_message(*args)
    end
  end
  
  def call(class_name, method_name, *args, &callback)
    if callback
      callback_id = @callback_id
      @callback_id += 1

      # Track the callback
      # TODO: Timeout on these callbacks
      @callbacks[callback_id] = callback
    else
      callback_id = nil
    end
    
    @page.channel.send_message([callback_id, class_name, method_name, *args])
  end
  
  
  def received_message(name, callback_id, *args)
    case name
    when 'response'
      response(callback_id, *args)
    when 'update'
      update(*args)
    when 'reload'
      reload
    end
  end
  
  def response(callback_id, *args)
    callback = @callbacks.delete(callback_id)
    
    if callback
      callback.call(*args)
    end
  end
  
  def update(model_id, data)
    $loading_models = true
    puts "UPDATE: #{model_id} with #{data.inspect}"
    Store.update(model_id, data)
    $loading_models = false
  end
  
  def reload
    puts "RELOAD"
    $page.page._reloading = true
    `window.location.reload(false);`
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
volt-0.4.7 lib/volt/page/tasks.rb
volt-0.4.5 lib/volt/page/tasks.rb
volt-0.4.4 lib/volt/page/tasks.rb
volt-0.4.3 lib/volt/page/tasks.rb
volt-0.4.2 lib/volt/page/tasks.rb
volt-0.4.1 lib/volt/page/tasks.rb
volt-0.4.0 lib/volt/page/tasks.rb
volt-0.3.9 lib/volt/page/tasks.rb
volt-0.3.8 lib/volt/page/tasks.rb