Class: Volt::Tasks

Inherits:
Object show all
Defined in:
lib/volt/page/tasks.rb

Overview

The tasks class provides an interface to call tasks on the backend server. This class is setup as page.task (as a singleton)

Instance Method Summary collapse

Constructor Details

#initialize(volt_app) ⇒ Tasks

Returns a new instance of Tasks.



7
8
9
10
11
12
13
14
15
# File 'lib/volt/page/tasks.rb', line 7

def initialize(volt_app)
  @volt_app       = volt_app
  @promise_id = 0
  @promises   = {}

  volt_app.channel.on('message') do |*args|
    received_message(*args)
  end
end

Instance Method Details

#call(class_name, method_name, meta_data, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/volt/page/tasks.rb', line 17

def call(class_name, method_name, , *args)
  promise_id            = @promise_id
  @promise_id += 1

  # Track the callback
  promise               = Promise.new
  @promises[promise_id] = promise

  # TODO: Timeout on these callbacks
  @volt_app.channel.send_message([promise_id, class_name, method_name, , *args])

  promise
end

#notify_query(method_name, collection, query, *args) ⇒ Object

Called when the backend sends a notification to change the results of a query.



69
70
71
72
# File 'lib/volt/page/tasks.rb', line 69

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, promise_id, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/volt/page/tasks.rb', line 31

def received_message(name, promise_id, *args)
  case name
    when 'added', 'removed', 'updated', 'changed'
      notify_query(name, *args)
    when 'response'
      response(promise_id, *args)
    when 'reload'
      reload
  end
end

#reloadObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/volt/page/tasks.rb', line 74

def reload
  # Stash the current page value
  begin
    value = EJSON.stringify(Volt.current_app.page.to_h)

    # If this browser supports session storage, store the page, so it will
    # be in the same state when we reload.
    `sessionStorage.setItem('___page', value);` if `sessionStorage`
  rescue EJSON::NonEjsonType => e
    # Unable to serailize the page, ignore stashing it
    # clear the ___page stash
    `sessionStorage.removeItem('___page');`
  end

  Volt.current_app.page._reloading = true
  `window.location.reload(false);`
end

#response(promise_id, result, error, cookies) ⇒ 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
56
57
58
59
60
61
62
63
64
65
# File 'lib/volt/page/tasks.rb', line 44

def response(promise_id, result, error, cookies)
  # Set the cookies
  if cookies
    cookies.each do |key, value|
      @volt_app.cookies.set(key, value)
    end
  end

  promise = @promises.delete(promise_id)

  if promise
    if error
      # TODO: full error handling
      Volt.logger.error('Task Response:')
      Volt.logger.error(error)

      promise.reject(error)
    else
      promise.resolve(result)
    end
  end
end