Class: 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.

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Tasks

Returns a new instance of Tasks.



4
5
6
7
8
9
10
11
12
# File 'lib/volt/page/tasks.rb', line 4

def initialize(page)
  @page = page
  @callback_id = 0
  @callbacks = {}
  
  page.channel.on('message') do |_, *args|
    received_message(*args)
  end
end

Instance Method Details

#added(path, data) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/volt/page/tasks.rb', line 65

def added(path, data)
  $loading_models = true
  
  # Don't add if already in there
  # TODO: shouldn't send twice
  unless Persistors::ModelStore.from_id(data[:_id])
  
    _, parent_id = data.find {|k,v| k != :_id && k[-3..-1] == '_id'}
    if parent_id
      parent_collection = Persistors::ModelStore.from_id(parent_id).model
    else
      # On the root
      parent_collection = $page.store
    end
  
    puts "From Backend: Add: #{path.inspect} - #{data.inspect}"
    parent_collection.send(path) << data
  end
  $loading_models = false
end

#call(class_name, method_name, *args, &callback) ⇒ Object



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

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

#changed(model_id, data) ⇒ Object



58
59
60
61
62
63
# File 'lib/volt/page/tasks.rb', line 58

def changed(model_id, data)
  $loading_models = true
  puts "From Backend: UPDATE: #{model_id} with #{data.inspect}"
  Persistors::ModelStore.update(model_id, data)
  $loading_models = false
end

#received_message(name, callback_id, *args) ⇒ Object



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

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

#reloadObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/volt/page/tasks.rb', line 94

def reload
  puts "RELOAD"
  # Stash the current page value
  value = JSON.dump($page.page.cur.to_h.reject {|k,v| v.reactive? })
  
  # 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

#removed(id) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/volt/page/tasks.rb', line 86

def removed(id)
  puts "From Backend: Remove: #{id}"
  $loading_models = true
  model = Persistors::ModelStore.from_id(id)
  model.delete!
  $loading_models = false
end

#response(callback_id, result, error) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/volt/page/tasks.rb', line 45

def response(callback_id, result, error)
  callback = @callbacks.delete(callback_id)
  
  if callback
    if error
      # TODO: full error handling
      puts "Error: #{error.inspect}"
    else
      callback.call(result)
    end
  end
end