Sistema de eventos

El sistema de eventos de Gaphor proporciona una API para manejar eventos y suscribirse a eventos.

En Gaphor gestionamos las suscripciones a los manejadores de eventos a través del servicio EventManager. Gaphor está altamente orientado a eventos:

  • Los cambios en el modelo cargado se emiten como eventos

  • Los cambios en los diagramas se emiten como eventos

  • Los cambios en la interfaz de usuario se emiten como eventos

Aunque Gaphor depende en gran medida de GTK para su interfaz de usuario, Gaphor usa su propio despachador de eventos. Los eventos se pueden estructurar en jerarquías. Por ejemplo, un evento AttributeUpdated es un subtipo de ElementUpdated. Si estamos interesados en todos los cambios en los elementos, también podemos registrar ElementUpdated y recibir todos los eventos AttributeUpdated también.

class gaphor.core.eventmanager.EventManager[fuente]

The Event Manager provides a flexible way to dispatch events.

Event dispatching is a central component in Gaphor. It allows components in Gaphor to react to changes in the application.

Events are dispatched by type.

class subscribe(self, handler: Callable[[object], None])

Registrar un manejador.

Handlers are triggered (executed) when events are emitted through the handle method.

class unsubscribe(self, handler: Callable[[object], None])

Anula el registro de un manipulador previamente registrado.

class handle(self, *events: object)

Enviar notificaciones de eventos a los manejadores registrados.

gaphor.core.event_handler(*event_types)[fuente]

Mark a function/method as an event handler for a particular type of event.

Given a custom event type:

>>> class CustomEvent:
...     def __str__(self):
...         return type(self).__name__

You can apply this to a handler method or function:

>>> @event_handler(CustomEvent)
... def custom_handler(event: CustomEvent):
...     print(event)

This will allow you to let the even be handled by an event manager:

>>> event_manager = EventManager()
>>> event_manager.subscribe(custom_handler)
>>> event_manager.handle(CustomEvent())
CustomEvent

Under the hood events are handled by the Generics library. For more information about how the Generic library handles events see the Generic documentation.