Sha256: 9ab8838214479290e67e81806eb2d22f230ca7ce521ce38f3dfdc1ee06151baa

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

module Puppeteer::AsyncAwaitBehavior
  refine Class do
    # wrap with Concurrent::Promises.future
    def async(method_name)
      original_method = instance_method(method_name)

      unless method_name.to_s.start_with?('async_')
        puts "async method should start with 'async_': #{self.name}##{method_name}"
      end

      define_method(method_name) do |*args|
        Concurrent::Promises.future do
          original_method.bind(self).call(*args)
        rescue => err
          Logger.new(STDERR).warn(err)
          raise err
        end
      end
    rescue NameError
      if respond_to?(method_name)
        original_method = singleton_method(method_name)

        unless method_name.to_s.start_with?('async_')
          puts "async method should start with 'async_': #{method_name}"
        end

        define_singleton_method(method_name) do |*args|
          Concurrent::Promises.future do
            original_method.call(*args)
          rescue => err
            Logger.new(STDERR).warn(err)
            raise err
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
puppeteer-ruby-0.0.13 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.12 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.11 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.10 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.9 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.8 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.6 lib/puppeteer/async_await_behavior.rb
puppeteer-ruby-0.0.5 lib/puppeteer/async_await_behavior.rb