Class: Dommy::Bridge::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/bridge.rb

Overview

Wraps an external callback handle (registered in a host-side callback table) so the JS bridge can resolve / invoke it. The external host that creates these is responsible for honoring ‘invoke_callback(callback_id, args)`.

The ‘callback_id` key on this object exposes the integer id to JS-side code that needs to round-trip it (e.g. for release / introspection).

Constant Summary collapse

ID_KEY =
"__callback_id__"

Instance Method Summary collapse

Constructor Details

#initialize(host, callback_id) ⇒ Callback

Returns a new instance of Callback.



123
124
125
126
127
# File 'lib/dommy/bridge.rb', line 123

def initialize(host, callback_id)
  @host = host
  @callback_id = callback_id
  @props = {}
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



142
143
144
145
146
147
# File 'lib/dommy/bridge.rb', line 142

def __js_call__(method, args)
  case method
  when "call"
    @host.invoke_callback(@callback_id, args)
  end
end

#__js_get__(key) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/dommy/bridge.rb', line 129

def __js_get__(key)
  if key == ID_KEY
    @props.fetch(key, @callback_id)
  else
    @props[key]
  end
end

#__js_set__(key, value) ⇒ Object



137
138
139
140
# File 'lib/dommy/bridge.rb', line 137

def __js_set__(key, value)
  @props[key] = value
  nil
end