Class EventBus
- Since:
- 6.2.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classOne handlerreleaseForSupersede(UltiToolsPlugin)removed, and where it stood. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ExecutorServiceprivate final Map<Class<? extends ModuleEvent>, CopyOnWriteArrayList<HandlerEntry>> private static final Loggerprivate final ThreadLocal<UltiToolsPlugin> The module instance the framework is loading on this thread, if any (#506): set bybeginRegistrationScope(UltiToolsPlugin)while a module's container refreshes and itsregisterSelf()runs, and recorded on every handler registered meanwhile without an explicit owner instance. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAttributes every handler registered on this thread, untilendRegistrationScope(), toownerwhen the registration names no owner instance itself (#506).private List<HandlerEntry> collectHandlers(Class<? extends ModuleEvent> eventType) Collect all handlers matching the event type (including superclass handlers), sorted by priority.voidEnds the scope started bybeginRegistrationScope(UltiToolsPlugin)on this thread.private voidinvokeHandler(HandlerEntry entry, ModuleEvent event) private UltiToolsPluginownerInstanceOrScope(UltiToolsPlugin ownerInstance, String ownerModule) The module instance a registration is recorded against: the one it names; otherwise the module the framework is loading on this thread; otherwise the copy listed as loaded underownerModule(#562 item 1); otherwise none.voidpublish(ModuleEvent event) Publish an event synchronously.voidpublishAsync(ModuleEvent event) Publish an event asynchronously.voidregister(Class<? extends ModuleEvent> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, UltiToolsPlugin ownerInstance, Method method, Object instance) Register an annotation-based handler and record the module instance that owns it (#506).voidregister(Class<? extends ModuleEvent> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, Method method, Object instance) Register an annotation-based handler.releaseForSupersede(UltiToolsPlugin ownerInstance) Releases every handler recorded againstownerInstanceand returns the action that gives them back (#562).private voidrestore(List<EventBus.ReleasedHandler> released) voidshutdown()Shutdown the async thread pool.<T extends ModuleEvent>
Subscriptionsubscribe(Class<T> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, UltiToolsPlugin ownerInstance, Consumer<T> consumer) Register a programmatic handler and record the module instance that owns it (#506), sounregisterByOwnerInstance(UltiToolsPlugin)releases it whatever name it was filed under.<T extends ModuleEvent>
Subscriptionsubscribe(Class<T> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, Consumer<T> consumer) Register a programmatic handler with full options.<T extends ModuleEvent>
SubscriptionRegister a programmatic handler.voidunregisterAll(String moduleName) Unregister all handlers owned by a module.voidunregisterByOwnerInstance(UltiToolsPlugin ownerInstance) Unregister every handler registered withownerInstanceas its owner (#506), whatever module name it was filed under.
-
Field Details
-
LOGGER
-
handlers
-
asyncPool
-
registrationScopeOwner
The module instance the framework is loading on this thread, if any (#506): set bybeginRegistrationScope(UltiToolsPlugin)while a module's container refreshes and itsregisterSelf()runs, and recorded on every handler registered meanwhile without an explicit owner instance. AThreadLocal, likeTabCompletionManager's scope, so a registration another thread makes during that window is not attributed to the module.
-
-
Constructor Details
-
EventBus
public EventBus()
-
-
Method Details
-
register
public void register(Class<? extends ModuleEvent> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, Method method, Object instance) Register an annotation-based handler. -
register
public void register(Class<? extends ModuleEvent> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, UltiToolsPlugin ownerInstance, Method method, Object instance) Register an annotation-based handler and record the module instance that owns it (#506).unregisterByOwnerInstance(UltiToolsPlugin)then releases exactly this instance's handlers, whatever name they were filed under. The framework registers every module's@ModuleEventHandlermethods this way, so unloading a superseded copy of a module cannot release its replacement's handlers, which share its name.- Parameters:
eventType- the event type handledpriority- the dispatch priorityignoreCancelled- whether a cancelled event skips this handlerownerModule- the owning module's name, used byunregisterAll(String)ownerInstance- the owning module instance;nullrecords nonemethod- the handler methodinstance- the bean the method is invoked on- Since:
- 6.3.0
-
subscribe
Register a programmatic handler. Returns a Subscription for manual unsubscribe. -
subscribe
public <T extends ModuleEvent> Subscription subscribe(Class<T> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, Consumer<T> consumer) Register a programmatic handler with full options.As of 6.3.0 the handler is recorded against a module instance (#506, #562 item 1): the module the framework is loading on this thread, if any; otherwise the copy listed as loaded under
ownerModule(the first listed when more than one is); otherwise none, and it is filed under the name only. Unloading or superseding that copy releases it. Prefersubscribe(Class, EventPriority, boolean, String, UltiToolsPlugin, Consumer)when the module instance is at hand. -
subscribe
public <T extends ModuleEvent> Subscription subscribe(Class<T> eventType, EventPriority priority, boolean ignoreCancelled, String ownerModule, UltiToolsPlugin ownerInstance, Consumer<T> consumer) Register a programmatic handler and record the module instance that owns it (#506), sounregisterByOwnerInstance(UltiToolsPlugin)releases it whatever name it was filed under. Without an instance, a handler subscribed while the framework loads a module (seebeginRegistrationScope(UltiToolsPlugin)) is recorded against that module; one subscribed later is recorded against the copy listed as loaded underownerModule(PluginManager.findRegistrationOwner(String)), and filed under the name only when no copy is listed under it (#562 item 1).- Type Parameters:
T- the event type- Parameters:
eventType- the event type handledpriority- the dispatch priorityignoreCancelled- whether a cancelled event skips this handlerownerModule- the owning module's name, used byunregisterAll(String)ownerInstance- the owning module instance;nullrecords the loading module, if any, otherwise the copy listed underownerModule, if anyconsumer- the handler- Returns:
- a subscription for manual unsubscribe
- Since:
- 6.3.0
-
unregisterAll
Unregister all handlers owned by a module. -
unregisterByOwnerInstance
Unregister every handler registered withownerInstanceas its owner (#506), whatever module name it was filed under. Handlers registered with a name only are not matched here;unregisterAll(String)releases those.- Parameters:
ownerInstance- the module instance being unloaded;nullmatches nothing- Since:
- 6.3.0
-
releaseForSupersede
Releases every handler recorded againstownerInstanceand returns the action that gives them back (#562).PluginManagercalls this for a loaded copy of a module just before a newer copy of the same module builds its container and runsregisterSelf(): from then on the older copy receives no event, so nothing the newer copy publishes while it loads reaches code of the copy it replaces. When the newer copy fails to load, the framework first releases what the failed copy registered and then runs the returned action, which puts each released handler back at its place in its event type's list, so it is dispatched as before and aSubscriptionthe older copy holds still removes it; when the newer copy loads, the action is dropped and the older copy is unloaded. Running the action more than once restores nothing more. A handler recorded against another copy is never released or restored here: only entries recorded againstownerInstanceare matched, whichever copy's code registered them. Intended forPluginManager, not for module authors.- Parameters:
ownerInstance- the loaded copy being superseded;nullreleases nothing- Returns:
- the action that restores what this call released
- Since:
- 6.3.0
-
restore
-
beginRegistrationScope
Attributes every handler registered on this thread, untilendRegistrationScope(), toownerwhen the registration names no owner instance itself (#506). The framework opens this scope while it loads a module -- around the module's container refresh, where@PostConstructruns, and around itsregisterSelf()-- so that a module's programmatic subscriptions made while it loads are released with that module instance. Intended forPluginManager, not for module authors. Scopes do not nest.- Parameters:
owner- the module instance being loaded- Since:
- 6.3.0
-
endRegistrationScope
public void endRegistrationScope()Ends the scope started bybeginRegistrationScope(UltiToolsPlugin)on this thread.- Since:
- 6.3.0
-
ownerInstanceOrScope
The module instance a registration is recorded against: the one it names; otherwise the module the framework is loading on this thread; otherwise the copy listed as loaded underownerModule(#562 item 1); otherwise none. -
publish
Publish an event synchronously. Handlers run on the calling thread in priority order. -
publishAsync
Publish an event asynchronously. Handlers run on a worker thread. Cancellable events are rejected — cancellation is only meaningful for sync dispatch. -
shutdown
public void shutdown()Shutdown the async thread pool. Called on plugin disable. -
collectHandlers
Collect all handlers matching the event type (including superclass handlers), sorted by priority. -
invokeHandler
-