Class: Puppeteer::DOMWorld::BindingFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/dom_world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, proc:) ⇒ BindingFunction

Returns a new instance of BindingFunction.



8
9
10
11
# File 'lib/puppeteer/dom_world.rb', line 8

def initialize(name:, proc:)
  @name = name
  @proc = proc
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/puppeteer/dom_world.rb', line 17

def name
  @name
end

Instance Method Details

#call(*args) ⇒ Object



13
14
15
# File 'lib/puppeteer/dom_world.rb', line 13

def call(*args)
  @proc.call(*args)
end

#page_binding_init_stringObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppeteer/dom_world.rb', line 19

def page_binding_init_string
  <<~JAVASCRIPT
  (type, bindingName) => {
    /* Cast window to any here as we're about to add properties to it
     * via win[bindingName] which TypeScript doesn't like.
     */
    const win = window;
    const binding = win[bindingName];

    win[bindingName] = (...args) => {
      const me = window[bindingName];
      let callbacks = me.callbacks;
      if (!callbacks) {
        callbacks = new Map();
        me.callbacks = callbacks;
      }
      const seq = (me.lastSeq || 0) + 1;
      me.lastSeq = seq;
      const promise = new Promise((resolve, reject) =>
        callbacks.set(seq, { resolve, reject })
      );
      binding(JSON.stringify({ type, name: bindingName, seq, args }));
      return promise;
    };
  }
  JAVASCRIPT
end