Class: Bluesky::Application

Inherits:
Object
  • Object
show all
Includes:
DOMHelper, TryHelper
Defined in:
lib/bluesky/application.rb

Overview

The application entry point

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



15
16
17
18
# File 'lib/bluesky/application.rb', line 15

def initialize
  @dispatch_queue = []
  @debug = false
end

Instance Attribute Details

#delegateObject

Receives all notifications



13
14
15
# File 'lib/bluesky/application.rb', line 13

def delegate
  @delegate
end

#root_view_controllerObject

Top level ViewController



10
11
12
# File 'lib/bluesky/application.rb', line 10

def root_view_controller
  @root_view_controller
end

Instance Method Details

#debug!Object

Turns on debug logging



25
26
27
28
# File 'lib/bluesky/application.rb', line 25

def debug!
  @debug = true
  self
end

#debug?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/bluesky/application.rb', line 20

def debug?
  @debug
end

#dispatch(target, action, *payload, &block) ⇒ Object

Dispatches an action on target

Attributes:

target:  (Object) The object that receives the send action
action   (Symbol) The method symbol on target
payload: (Array)  The arguments passed to send if any
block:   (Block)  Optional block that will be passed as argument to send


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bluesky/application.rb', line 37

def dispatch(target, action, *payload, &block)
  promise = Promise.new
  notify(self, :dispatch_requested, target, action, *payload)
  @dispatch_queue << lambda do
    begin
      result = target.send(action, *payload, &block)
      promise.resolve(result).then { refresh }
      notify(self, :dispatch_resolved, target, action, *payload, result)
    rescue => err
      promise.reject(err)
      notify(self, :dispatch_rejected, target, action, *payload, err)
    end
  end
  defer { process_dispatch_queue }
  promise
end

#notify(source, event, *payload) ⇒ Object

Notifies the delegate about an event

Attributes:

source:  (Object) The object that send the event
event:   (Symbol) The event symbol
payload: (Array)  Additional arguments to pass along


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bluesky/application.rb', line 61

def notify(source, event, *payload)
  try(@delegate, source, event, *payload)
  if debug?
    message = "#{event} #{payload}"
    if RUBY_ENGINE == 'opal'
      `console.log(message)`
    else
      puts message
    end
  end
end

#refresh(&block) ⇒ Object

Refreshes (runs render) on the root_view_controller and invokes the block (if any) when the render is complete.



75
76
77
78
79
# File 'lib/bluesky/application.rb', line 75

def refresh(&block)
  promise = Promise.new
  @clearwater.call { promise.resolve }
  block ? promise.then(&block) : promise
end

#runObject

Does the required wiring and runs the initial render



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bluesky/application.rb', line 82

def run
  raise 'root_view_controller must be defined in Application' unless root_view_controller
  PureComponent.install_hooks(debug?)
  root_view_controller.parent = self
  router = RUBY_ENGINE != 'opal' ?
    Clearwater::Router.new(location: 'http://localhost:9292/') :
    Clearwater::Router.new
  @clearwater = Clearwater::Application.new(
    component: root_view_controller,
    router: router
  )
  @clearwater.debug! if debug?
  root_view_controller.begin_appearance_transition(true)
  refresh { root_view_controller.end_appearance_transition() }
  self
end