Class: Lich::Common::GUI::TabCommunicator

Inherits:
Object
  • Object
show all
Defined in:
documented/common/gui/tab_communicator.rb

Overview

Handles communication for tab data changes. This class allows for registering callbacks that are invoked when data changes occur.

Examples:

Creating a TabCommunicator and registering a callback

communicator = Lich::Common::GUI::TabCommunicator.new
communicator.register_data_change_callback(lambda { |change_type, data| puts "Data changed: #{change_type}, #{data}" })

Instance Method Summary collapse

Constructor Details

#initializeTabCommunicator

Returns a new instance of TabCommunicator.



11
12
13
# File 'documented/common/gui/tab_communicator.rb', line 11

def initialize
  @data_change_callbacks = []
end

Instance Method Details

#clear_callbacksvoid

This method returns an undefined value.

Clears all registered data change callbacks.



48
49
50
# File 'documented/common/gui/tab_communicator.rb', line 48

def clear_callbacks
  @data_change_callbacks.clear
end

#notify_data_changed(change_type = :general, data = {}) ⇒ void

This method returns an undefined value.

Notifies all registered callbacks that data has changed.

Examples:

Notifying data change

communicator.notify_data_changed(:update, { key: "value" })

Parameters:

  • change_type (Symbol) (defaults to: :general)

    The type of change that occurred (default: :general).

  • data (Hash) (defaults to: {})

    The data associated with the change (default: {}).

Raises:

  • (StandardError)

    If an error occurs during callback execution.



29
30
31
32
33
34
35
36
37
# File 'documented/common/gui/tab_communicator.rb', line 29

def notify_data_changed(change_type = :general, data = {})
  @data_change_callbacks.each do |callback|
    begin
      callback.call(change_type, data)
    rescue StandardError => e
      Lich.log "error: Error in data change callback: #{e.message}"
    end
  end
end

#register_data_change_callback(callback) ⇒ void

This method returns an undefined value.

Registers a callback to be invoked when data changes.

Parameters:

  • callback (Proc)

    The callback to be invoked on data change.



18
19
20
# File 'documented/common/gui/tab_communicator.rb', line 18

def register_data_change_callback(callback)
  @data_change_callbacks << callback if callback.respond_to?(:call)
end

#unregister_data_change_callback(callback) ⇒ void

This method returns an undefined value.

Unregisters a previously registered callback.

Parameters:

  • callback (Proc)

    The callback to be removed.



42
43
44
# File 'documented/common/gui/tab_communicator.rb', line 42

def unregister_data_change_callback(callback)
  @data_change_callbacks.delete(callback)
end