Sha256: 2a009c158614044049944f705eb7bb094c9bcd6fdff2da52da9d49de47f73e6e

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

require 'singleton'

GLib.load_class :MainLoop

module GLib
  # Overrides for GMainLoop, GLib's event loop
  class MainLoop
    # Class encapsulationg logic for running an idle handler to make Ruby code
    # run during GLib's event loop.
    class ThreadEnabler
      include Singleton

      FRAMERATE = 25
      DEFAULT_TIMEOUT = 1000 / FRAMERATE

      def initialize(timeout = DEFAULT_TIMEOUT)
        @timeout = timeout
      end

      def setup_idle_handler
        @handler_id ||= GLib.timeout_add(GLib::PRIORITY_DEFAULT,
                                         @timeout, handler_proc,
                                         nil, nil)
      end

      private

      def handler_proc
        proc do
          Thread.pass
          true
        end
      end
    end

    def run_with_thread_enabler
      case RUBY_ENGINE
      when 'jruby'
      when 'rbx'
      else # 'ruby' most likely
        ThreadEnabler.instance.setup_idle_handler
      end
      run_without_thread_enabler
    end

    alias_method :run_without_thread_enabler, :run
    alias_method :run, :run_with_thread_enabler
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gir_ffi-0.8.6 lib/ffi-glib/main_loop.rb
gir_ffi-0.8.5 lib/ffi-glib/main_loop.rb
gir_ffi-0.8.4 lib/ffi-glib/main_loop.rb
gir_ffi-0.8.3 lib/ffi-glib/main_loop.rb
gir_ffi-0.8.2 lib/ffi-glib/main_loop.rb
gir_ffi-0.8.1 lib/ffi-glib/main_loop.rb
gir_ffi-0.8.0 lib/ffi-glib/main_loop.rb