Module: Lich::Common::GtkIdleRetention

Included in:
Lich::Common
Defined in:
documented/common/gtk.rb

Overview

Keep idle callbacks alive until they stop repeating.

Instance Method Summary collapse

Instance Method Details

#add(*args) {|Object| ... } ⇒ Integer?

Note:

Callbacks are retained in Lich::Common.gtk_idle_callbacks and automatically removed when they stop repeating (return false), raise, or are otherwise cleaned up

Adds an idle callback to GLib while retaining the Proc to prevent garbage collection.

GLib idle callbacks are stored in C-side structures that Ruby's GC cannot see. This method wraps GLib::Idle.add to retain the callback Proc on the Ruby side, keeping it alive until the callback stops repeating or raises an exception.

Parameters:

  • args (Array)

    arguments passed to GLib::Idle.add

Yields:

  • (Object)

    the callback block, invoked when the main loop is idle; return false to stop

Returns:

  • (Integer, nil)

    the idle source ID if successfully queued, or nil on failure

See Also:



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'documented/common/gtk.rb', line 349

def add(*args, &block)
  return super(*args, &block) unless block

  callback_id = nil
  retained = proc do |*callback_args|
    begin
      ret = block.call(*callback_args)
      Lich::Common.with_gtk_registry_lock { Lich::Common.gtk_idle_callbacks.delete(callback_id) } unless ret
      ret
    rescue StandardError
      Lich::Common.with_gtk_registry_lock { Lich::Common.gtk_idle_callbacks.delete(callback_id) }
      raise
    end
  end

  callback_id = super(*args, &retained)
  Lich::Common.with_gtk_registry_lock { Lich::Common.gtk_idle_callbacks[callback_id] = retained } if callback_id
  callback_id
end