Sha256: 1ed2b03be0d20d81f57c71d5ee3839c0445ded912ab6eb0e8c7ae000f0330453

Contents?: true

Size: 1.91 KB

Versions: 34

Compression:

Stored size: 1.91 KB

Contents

require 'native'

# Manipulate the browser console.
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/console
class Console
  include Native::Wrapper

  # Clear the console.
  def clear
    `#{@native}.clear()`
  end

  # Print a stacktrace from the call site.
  def trace
    `#{@native}.trace()`
  end

  # Log the passed objects based on an optional initial format.
  def log(*args)
    `#{@native}.log.apply(#{@native}, args)`
  end

  # Log the passed objects based on an optional initial format as informational
  # log.
  def info(*args)
    `#{@native}.info.apply(#{@native}, args)`
  end

  # Log the passed objects based on an optional initial format as warning.
  def warn(*args)
    `#{@native}.warn.apply(#{@native}, args)`
  end

  # Log the passed objects based on an optional initial format as error.
  def error(*args)
    `#{@native}.error.apply(#{@native}, args)`
  end

  # Time the given block with the given label.
  def time(label, &block)
    raise ArgumentError, 'no block given' unless block

    `#{@native}.time(label)`

    begin
      if block.arity == 0
        instance_exec(&block)
      else
        yield(self)
      end
    ensure
      `#{@native}.timeEnd()`
    end
  end

  # Group the given block.
  def group(*args, &block)
    raise ArgumentError, 'no block given' unless block

    `#{@native}.group.apply(#{@native}, args)`

    begin
      if block.arity == 0
        instance_exec(&block)
      else
        yield(self)
      end
    ensure
      `#{@native}.groupEnd()`
    end
  end

  # Group the given block but collapse it.
  def group!(*args, &block)
    return unless block_given?

    `#{@native}.groupCollapsed.apply(#{@native}, args)`

    begin
      if block.arity == 0
        instance_exec(&block)
      else
        yield(self)
      end
    ensure
      `#{@native}.groupEnd()`
    end
  end
end

if defined?(`Opal.global.console`)
  $console = Console.new(`Opal.global.console`)
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
opal-1.7.4 stdlib/console.rb
opal-1.7.3 stdlib/console.rb
opal-1.7.2 stdlib/console.rb
opal-1.7.1 stdlib/console.rb
opal-1.7.0 stdlib/console.rb
opal-1.7.0.rc1 stdlib/console.rb
opal-1.6.1 stdlib/console.rb
opal-1.6.0 stdlib/console.rb
opal-1.6.0.rc1 stdlib/console.rb
opal-1.6.0.alpha1 stdlib/console.rb
opal-1.5.1 stdlib/console.rb
opal-1.5.0 stdlib/console.rb
opal-1.5.0.rc1 stdlib/console.rb
opal-1.4.1 stdlib/console.rb
opal-1.4.0 stdlib/console.rb
opal-1.4.0.alpha1 stdlib/console.rb
opal-1.3.2 stdlib/console.rb
opal-1.3.1 stdlib/console.rb
opal-1.3.0 stdlib/console.rb
opal-1.3.0.rc1 stdlib/console.rb