Class: Scarpe::Webview::ControlInterface
- Inherits:
-
Object
- Object
- Scarpe::Webview::ControlInterface
- Includes:
- Shoes::Log
- Defined in:
- lib/scarpe/wv/control_interface.rb
Constant Summary collapse
- SUBSCRIBE_EVENTS =
[:init, :shutdown, :next_redraw, :every_redraw, :next_heartbeat, :every_heartbeat]
- DISPATCH_EVENTS =
[:init, :shutdown, :redraw, :heartbeat]
- INVALID_SYSTEM_COMPONENTS_MESSAGE =
"Must pass non-nil app and wrangler to ControlInterface#set_system_components!"
- CONTROL_INTERFACE_INIT_MESSAGE =
"ControlInterface code needs to be wrapped in handlers like on_event(:init) " + "to make sure they have access to app, doc_root, wrangler, etc!"
Constants included from Shoes::Log
Shoes::Log::DEFAULT_COMPONENT, Shoes::Log::DEFAULT_DEBUG_LOG_CONFIG, Shoes::Log::DEFAULT_LOG_CONFIG
Instance Attribute Summary collapse
-
#do_shutdown ⇒ Object
readonly
Returns the value of attribute do_shutdown.
- #doc_root ⇒ Object
Instance Method Summary collapse
- #app ⇒ Object
-
#dispatch_event(event, *args, **keywords) ⇒ Object
Send out the specified event.
-
#initialize ⇒ ControlInterface
constructor
The control interface needs to see major system components to hook into their events.
- #inspect ⇒ Object
-
#on_event(event, &block) ⇒ Object
On recognised events, this sets a handler for that event.
-
#set_system_components(app:, doc_root:, wrangler:) ⇒ Object
This should get called once, from Shoes::App.
- #wrangler ⇒ Object
Methods included from Shoes::Log
configure_logger, #log_init, logger
Constructor Details
#initialize ⇒ ControlInterface
The control interface needs to see major system components to hook into their events
19 20 21 22 23 24 25 |
# File 'lib/scarpe/wv/control_interface.rb', line 19 def initialize log_init("Webview::ControlInterface") @do_shutdown = false @event_handlers = {} (SUBSCRIBE_EVENTS | DISPATCH_EVENTS).each { |e| @event_handlers[e] = [] } end |
Instance Attribute Details
#do_shutdown ⇒ Object (readonly)
Returns the value of attribute do_shutdown.
16 17 18 |
# File 'lib/scarpe/wv/control_interface.rb', line 16 def do_shutdown @do_shutdown end |
#doc_root ⇒ Object
57 58 59 60 61 |
# File 'lib/scarpe/wv/control_interface.rb', line 57 def doc_root raise Scarpe::MissingDocRootError, CONTROL_INTERFACE_INIT_MESSAGE unless @doc_root @doc_root end |
Instance Method Details
#app ⇒ Object
51 52 53 54 55 |
# File 'lib/scarpe/wv/control_interface.rb', line 51 def app raise Scarpe::MissingAppError, CONTROL_INTERFACE_INIT_MESSAGE unless @app @app end |
#dispatch_event(event, *args, **keywords) ⇒ Object
Send out the specified event
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/scarpe/wv/control_interface.rb', line 86 def dispatch_event(event, *args, **keywords) @log.debug("CTL event #{event.inspect} #{args.inspect} #{keywords.inspect}") unless DISPATCH_EVENTS.include?(event) raise Scarpe::IllegalDispatchEventError, "Illegal dispatch of event #{event.inspect}! Valid values are: #{DISPATCH_EVENTS.inspect}" end if @do_shutdown @log.debug("CTL: Shutting down - not dispatching #{event}!") return end if event == :redraw dumb_dispatch_event(:every_redraw, *args, **keywords) # Next redraw is interesting. We can add new handlers # when dispatching a next_redraw handler. But we want # each handler to run only once. handlers = @event_handlers[:next_redraw] dumb_dispatch_event(:next_redraw, *args, **keywords) @event_handlers[:next_redraw] -= handlers return end if event == :heartbeat dumb_dispatch_event(:every_heartbeat, *args, **keywords) # Next heartbeat is interesting. We can add new handlers # when dispatching a next_heartbeat handler. But we want # each handler to run only once. handlers = @event_handlers[:next_heartbeat] dumb_dispatch_event(:next_heartbeat, *args, **keywords) @event_handlers[:next_heartbeat] -= handlers return end if event == :shutdown @do_shutdown = true end dumb_dispatch_event(event, *args, **keywords) end |
#inspect ⇒ Object
27 28 29 |
# File 'lib/scarpe/wv/control_interface.rb', line 27 def inspect "<#ControlInterface>" end |
#on_event(event, &block) ⇒ Object
On recognised events, this sets a handler for that event
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/scarpe/wv/control_interface.rb', line 73 def on_event(event, &block) unless SUBSCRIBE_EVENTS.include?(event) raise Scarpe::IllegalSubscribeEventError, "Illegal subscribe to event #{event.inspect}! Valid values are: #{SUBSCRIBE_EVENTS.inspect}" end @unsub_id ||= 0 @unsub_id += 1 @event_handlers[event] << { handler: block, unsub: @unsub_id } @unsub_id end |
#set_system_components(app:, doc_root:, wrangler:) ⇒ Object
This should get called once, from Shoes::App
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/scarpe/wv/control_interface.rb', line 32 def set_system_components(app:, doc_root:, wrangler:) unless app @log.error("False app passed to set_system_components!") raise Scarpe::MissingAppError, INVALID_SYSTEM_COMPONENTS_MESSAGE end unless wrangler @log.error("False wrangler passed to set_system_components!") raise Scarpe::MissingWranglerError, INVALID_SYSTEM_COMPONENTS_MESSAGE end @app = app @doc_root = doc_root # May be nil at this point @wrangler = wrangler @wrangler.control_interface = self @wrangler.on_every_redraw { self.dispatch_event(:redraw) } end |
#wrangler ⇒ Object
63 64 65 66 67 |
# File 'lib/scarpe/wv/control_interface.rb', line 63 def wrangler raise Scarpe::MissingWranglerError, CONTROL_INTERFACE_INIT_MESSAGE unless @wrangler @wrangler end |