Sha256: 208e9da88fcad760246e343f776c8dec31693ca2badaa14c5558116ea8831eb8

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

# backtick_javascript: true

class ::TracePoint
  # partial implementation of TracePoint
  # for the moment only supports the :class event
  def self.trace(event, &block)
    new(event, &block).enable
  end

  attr_reader :event

  def initialize(event, &block)
    ::Kernel.raise 'Only the :class event is supported' unless event == :class
    @event = event
    @block = block
    @trace_object = nil
    @trace_evt = "trace_#{@event}"
    @tracers_for_evt = "tracers_for_#{@event}"
  end

  def enable(*args, &enable_block)
    previous_state = enabled?
    %x{
      Opal[#{@tracers_for_evt}].push(self);
      Opal[#{@trace_evt}] = true;
    }
    if block_given?
      yield
      disable
    end
    previous_state
  end

  def enabled?
    `Opal[#{@trace_evt}] && Opal[#{@tracers_for_evt}].includes(self)`
  end

  def disable
    %x{
      var idx = Opal[#{@tracers_for_evt}].indexOf(self)

      if (idx > -1) {
        Opal[#{@tracers_for_evt}].splice(idx, 1);

        if (Opal[#{@tracers_for_evt}].length === 0) {
          Opal[#{@trace_evt}] = false;
        }

        return true;
      } else {
        return false;
      }
    }
  end

  def self
    @trace_object
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 opal/corelib/trace_point.rb
opal-1.8.2 opal/corelib/trace_point.rb
opal-1.8.1 opal/corelib/trace_point.rb
opal-1.8.0 opal/corelib/trace_point.rb
opal-1.8.0.beta1 opal/corelib/trace_point.rb
opal-1.8.0.alpha1 opal/corelib/trace_point.rb