Skip to main content

Class: ScopedPluginEventBus

Defined in: src/plugins/core/scoped-event-bus.ts:66

Scoped plugin event bus with permission enforcement.

Remarks

Each plugin receives a scoped event bus that:

  • Only allows subscription to hooks declared in manifest.permissions.hooks
  • Supports wildcard patterns (e.g., 'preprint.*' in manifest allows 'preprint.indexed')
  • Tracks all registered handlers for cleanup on plugin unload
  • Delegates actual event handling to the main PluginEventBus

Example

// Plugin manifest declares allowed hooks
const manifest = {
permissions: {
hooks: ['preprint.indexed', 'preprint.updated', 'system.*']
}
};

const scopedBus = new ScopedPluginEventBus(mainEventBus, manifest);

// Allowed (matches declared hook)
scopedBus.on('preprint.indexed', handler);

// Allowed (matches wildcard 'system.*')
scopedBus.on('system.startup', handler);

// Throws PluginPermissionError (not declared)
scopedBus.on('review.created', handler);

Implements

Constructors

new ScopedPluginEventBus()

new ScopedPluginEventBus(delegate, manifest): ScopedPluginEventBus

Defined in: src/plugins/core/scoped-event-bus.ts:93

Creates a new ScopedPluginEventBus.

Parameters

delegate

IPluginEventBus

Main event bus to delegate to

manifest

IPluginManifest

Plugin manifest with permission declarations

Returns

ScopedPluginEventBus

Methods

cleanup()

cleanup(): void

Defined in: src/plugins/core/scoped-event-bus.ts:257

Unregisters all handlers registered through this scoped bus.

Returns

void

Remarks

Called during plugin unload to ensure all handlers are properly cleaned up. This prevents memory leaks and orphaned handlers.

Example

// During plugin shutdown
scopedEventBus.cleanup();

emit()

emit(event, ...args): void

Defined in: src/plugins/core/scoped-event-bus.ts:136

Emits an event if the plugin has permission.

Parameters

event

string

Event name

args

...readonly unknown[]

Event arguments

Returns

void

Throws

If hook not declared in manifest

Remarks

Plugins can only emit events they have permission to subscribe to. This prevents plugins from spoofing events they shouldn't have access to.

Implementation of

IPluginEventBus.emit


emitAsync()

emitAsync(event, ...args): Promise<void>

Defined in: src/plugins/core/scoped-event-bus.ts:152

Emits an event asynchronously and waits for all handlers.

Parameters

event

string

Event name

args

...readonly unknown[]

Event arguments

Returns

Promise<void>

Promise that resolves when all handlers complete

Throws

If hook not declared in manifest

Implementation of

IPluginEventBus.emitAsync


eventNames()

eventNames(): string[]

Defined in: src/plugins/core/scoped-event-bus.ts:225

Gets the names of all events with registered listeners.

Returns

string[]

Array of event names

Implementation of

IPluginEventBus.eventNames


getAllowedHooks()

getAllowedHooks(): readonly string[]

Defined in: src/plugins/core/scoped-event-bus.ts:282

Gets the list of allowed hooks for this plugin.

Returns

readonly string[]

Array of allowed hook patterns


getHandlerCount()

getHandlerCount(): number

Defined in: src/plugins/core/scoped-event-bus.ts:271

Gets the number of handlers registered through this scoped bus.

Returns

number

Number of registered handlers


isHookAllowed()

isHookAllowed(hookName): boolean

Defined in: src/plugins/core/scoped-event-bus.ts:294

Checks if a hook is allowed.

Parameters

hookName

string

Hook to check

Returns

boolean

True if hook is allowed


listenerCount()

listenerCount(event): number

Defined in: src/plugins/core/scoped-event-bus.ts:214

Gets the number of listeners for an event.

Parameters

event

string

Event name

Returns

number

Number of registered listeners

Implementation of

IPluginEventBus.listenerCount


off()

off(event, handler): void

Defined in: src/plugins/core/scoped-event-bus.ts:194

Unsubscribes from an event.

Parameters

event

string

Event name

handler

(...args) => void

Handler to remove

Returns

void

Implementation of

IPluginEventBus.off


on()

on(event, handler): void

Defined in: src/plugins/core/scoped-event-bus.ts:116

Subscribes to an event if the plugin has permission.

Parameters

event

string

Event name or pattern

handler

(...args) => void

Event handler function

Returns

void

Throws

If hook not declared in manifest

Example

// Plugin with hooks: ['preprint.*']
scopedBus.on('preprint.indexed', (data) => {
console.log('Indexed:', data.uri);
});

Implementation of

IPluginEventBus.on


once()

once(event, handler): void

Defined in: src/plugins/core/scoped-event-bus.ts:167

Subscribes to an event for one-time execution if the plugin has permission.

Parameters

event

string

Event name or pattern

handler

(...args) => void

Event handler function (called once then removed)

Returns

void

Throws

If hook not declared in manifest

Implementation of

IPluginEventBus.once


removeAllListeners()

removeAllListeners(): void

Defined in: src/plugins/core/scoped-event-bus.ts:238

Removes all listeners registered through this scoped bus.

Returns

void

Remarks

This only removes handlers registered through this scoped bus, not all handlers on the delegate.

Implementation of

IPluginEventBus.removeAllListeners