Sha256: 40a32dce0176e2e08349220a80db0e344b85dad63e817ceea04e44b4ef362fd2

Contents?: true

Size: 1.14 KB

Versions: 16

Compression:

Stored size: 1.14 KB

Contents

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

16 entries across 16 versions & 1 rubygems

Version Path
opal-1.7.4 opal/corelib/trace_point.rb
opal-1.7.3 opal/corelib/trace_point.rb
opal-1.7.2 opal/corelib/trace_point.rb
opal-1.7.1 opal/corelib/trace_point.rb
opal-1.7.0 opal/corelib/trace_point.rb
opal-1.7.0.rc1 opal/corelib/trace_point.rb
opal-1.6.1 opal/corelib/trace_point.rb
opal-1.6.0 opal/corelib/trace_point.rb
opal-1.6.0.rc1 opal/corelib/trace_point.rb
opal-1.6.0.alpha1 opal/corelib/trace_point.rb
opal-1.5.1 opal/corelib/trace_point.rb
opal-1.5.0 opal/corelib/trace_point.rb
opal-1.5.0.rc1 opal/corelib/trace_point.rb
opal-1.4.1 opal/corelib/trace_point.rb
opal-1.4.0 opal/corelib/trace_point.rb
opal-1.4.0.alpha1 opal/corelib/trace_point.rb