Class: Tasks
Overview
The tasks class provides an interface to call tasks on the backend server.
Instance Method Summary collapse
- #call(class_name, method_name, *args, &callback) ⇒ Object
 - 
  
    
      #initialize(page)  ⇒ Tasks 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of Tasks.
 - 
  
    
      #notify_query(method_name, collection, query, *args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
Called when the backend sends a notification to change the results of a query.
 - #received_message(name, callback_id, *args) ⇒ Object
 - #reload ⇒ Object
 - 
  
    
      #response(callback_id, result, error)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
When a request is sent to the backend, it can attach a callback, this is called from the backend to pass to the callback.
 
Constructor Details
#initialize(page) ⇒ Tasks
Returns a new instance of Tasks.
      4 5 6 7 8 9 10 11 12 13  | 
    
      # File 'lib/volt/page/tasks.rb', line 4 def initialize(page) @page = page @callback_id = 0 @callbacks = {} # TODORW: ... page.channel.on('message') do |*args| (*args) end end  | 
  
Instance Method Details
#call(class_name, method_name, *args, &callback) ⇒ Object
      15 16 17 18 19 20 21 22 23 24 25 26 27 28  | 
    
      # File 'lib/volt/page/tasks.rb', line 15 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.([callback_id, class_name, method_name, *args]) end  | 
  
#notify_query(method_name, collection, query, *args) ⇒ Object
Called when the backend sends a notification to change the results of a query.
      59 60 61 62  | 
    
      # File 'lib/volt/page/tasks.rb', line 59 def notify_query(method_name, collection, query, *args) query_obj = Persistors::ArrayStore.query_pool.lookup(collection, query) query_obj.send(method_name, *args) end  | 
  
#received_message(name, callback_id, *args) ⇒ Object
      31 32 33 34 35 36 37 38 39 40  | 
    
      # File 'lib/volt/page/tasks.rb', line 31 def (name, callback_id, *args) case name when 'added', 'removed', 'updated', 'changed' notify_query(name, *args) when 'response' response(callback_id, *args) when 'reload' reload end end  | 
  
#reload ⇒ Object
      64 65 66 67 68 69 70 71 72 73 74 75 76  | 
    
      # File 'lib/volt/page/tasks.rb', line 64 def reload # Stash the current page value value = JSON.dump($page.page.to_h) # If this browser supports session storage, store the page, so it will # be in the same state when we reload. if `sessionStorage` `sessionStorage.setItem('___page', value);` end $page.page._reloading = true `window.location.reload(false);` end  | 
  
#response(callback_id, result, error) ⇒ Object
When a request is sent to the backend, it can attach a callback, this is called from the backend to pass to the callback.
      44 45 46 47 48 49 50 51 52 53 54 55  | 
    
      # File 'lib/volt/page/tasks.rb', line 44 def response(callback_id, result, error) callback = @callbacks.delete(callback_id) if callback if error # TODO: full error handling puts "Task Response: #{error.inspect}" else callback.call(result) end end end  |