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) ⇒ 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).



26
27
28
29
30
31
32
33
34
# File 'lib/dommy/callable_invoker.rb', line 26

def invoke_listener(listener, event)
  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__)
    listener.__js_call__("call", [event])
  end
end