Module: Dommy::CallableInvoker

Defined in:
lib/dommy/callable_invoker.rb

Overview

Invokes a callback that may be a JS-bridged object (responds to __js_call__) or a plain Ruby callable (responds to call). Centralizes the dispatch used by promises, the scheduler, and streams so the JS/Ruby fork lives in one place.

Class Method Summary collapse

Class Method Details

.invoke(callback, *args) ⇒ Object

Invoke callback with args. A JS-bridged callable goes through __js_call__("call", args); a Ruby callable through call(*args). A nil or non-callable callback is a no-op (returns nil).



13
14
15
16
17
18
19
20
21
# File 'lib/dommy/callable_invoker.rb', line 13

def invoke(callback, *args)
  return if callback.nil?

  if callback.respond_to?(:__js_call__)
    callback.__js_call__("call", args)
  elsif callback.respond_to?(:call)
    callback.call(*args)
  end
end

.invoke_listener(listener, event, current_target = nil) ⇒ Object

Invoke a DOM event listener per the EventTarget rule: an object with handle_event, else a Ruby callable, else a JS-bridged callable (tried in that order). A JS function listener's this must be the event's currentTarget (the node the listener is attached to), so pass it through when the bridge supports an explicit receiver.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dommy/callable_invoker.rb', line 28

def invoke_listener(listener, event, current_target = nil)
  if listener.respond_to?(:handle_event)
    listener.handle_event(event)
  elsif listener.respond_to?(:call) && !listener.is_a?(Module)
    listener.call(event)
  elsif listener.respond_to?(:__js_call_with_this__)
    listener.__js_call_with_this__([event], current_target)
  elsif listener.respond_to?(:__js_call__)
    listener.__js_call__("call", [event])
  end
end