Module: Redcar::Observable

Included in:
Application, ApplicationSWT::Notebook, Clipboard, ClipboardViewer::ClipboardBar, Command, Declarations::OutlineViewDialog, Document, Document::Mirror, EditView, EditView, EditViewSWT, EditViewSWT::Document, EditViewSWT::Tab, FilterListDialog, HtmlController, ModelessListDialog, Notebook, Sensitivity, Sensitivity, Tab, Textmate::Bundle, Tree, Tree::Mirror, Tree::Mirror::NodeMirror, Treebook, Window
Defined in:
plugins/core/lib/core/observable.rb

Overview

When mixed into a class this module gives an interface for registering listeners to particular events. Most Redcar models include this interface.

## Examples

Adding a listener

@obj.add_listener :MyHook do
  puts "code to run when MyHook is triggered"
end

Triggering a hook (from within the observed class)

notify_listeners(:MyHook)

## Before and After Hooks

You can attach code to run before and after an event:

@obj.add_listener :after => "MyHook" do
  puts "code to run after MyHook"
end

@obj.add_listener :before => "MyHook" do
  puts "code to run before MyHook"
end

And then the :before blocks are guaranteed to run before the after blocks and the blocks attached without specifying before and after.

The event can also be triggered with a block:

notify_listeners :MyHook do
  puts "stuff that happens"
end

then the output will be:

code to run before MyHook
stuff that happens
code to run after MyHook

## Triggering with Objects

An object may pass an object or objects to its listeners:

@obj.add_listener :NewTab do |new_tab|
  puts "do some stuff with the new tab: " + new_tab.to_s
end

notify_listeners(:NewTab, new_tab)

Defined Under Namespace

Classes: UnknownEvent

Constant Summary

ASPECTS =
{
  :before => 0,
  :after => 1
}

Instance Method Summary (collapse)

Instance Method Details

- (Handler) add_listener(*event_names, &block)

Attach a block to be called when any of the hooks in event_names are called.

Parameters:

  • names (Array<Symbol>)

    of the events to attach to

Returns:

  • (Handler)

    event handler for this object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'plugins/core/lib/core/observable.rb', line 66

def add_listener(*event_names, &block)
  if event_names.first.is_a?(Hash)
    event_names.first.each do |aspect, event_name|
      observable_events(event_name.to_s)[ASPECTS[aspect]] << block
    end
  else
    event_names.each do |event_name|
      observable_events(event_name.to_s)[ASPECTS[:after]] << block
    end
  end
  block
end

- (Object) notify_listeners(event_name, *args)

Run all the listeners attached to this event.

Parameters:

  • name (Symbol)

    of event being run

  • event (*Object)

    parameters



93
94
95
96
97
# File 'plugins/core/lib/core/observable.rb', line 93

def notify_listeners(event_name, *args)
  observable_run_blocks(event_name, :before, args)
  yield if block_given?
  observable_run_blocks(event_name, :after, args)
end

- (Object) remove_listener(handler)

Remove a listener from this object.

Parameters:

  • an (Handler)

    event handler as returned by add_listener



82
83
84
85
86
87
# File 'plugins/core/lib/core/observable.rb', line 82

def remove_listener(handler)
  @events.each do |_, a|
    a[0].delete(handler)
    a[1].delete(handler)
  end
end