Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"eslint": "9.20.1",
"eslint-config-prettier": "10.0.1",
"eslint-plugin-prettier": "5.2.3",
"expect-type": "1.1.0",
"globals": "15.15.0",
"husky": "9.1.7",
"jest": "29.7.0",
Expand Down
47 changes: 30 additions & 17 deletions src/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
IEventHandler,
IEventPublisher,
ISaga,
PublisherPublishAllResult,
PublisherPublishResult,
UnhandledExceptionInfo,
} from './interfaces';
import { AsyncContext } from './scopes';
Expand All @@ -39,14 +41,23 @@ export type EventHandlerType<EventBase extends IEvent = IEvent> = Type<
>;

@Injectable()
export class EventBus<EventBase extends IEvent = IEvent>
export class EventBus<
EventBase extends IEvent = IEvent,
Publisher extends IEventPublisher<
EventBase,
PublishResult,
PublishAllResult
> = IEventPublisher<EventBase, any, any>,
PublishResult = PublisherPublishResult<Publisher>,
PublishAllResult = PublisherPublishAllResult<Publisher>,
>
extends ObservableBus<EventBase>
implements IEventBus<EventBase>, OnModuleDestroy
{
protected eventIdProvider: EventIdProvider<EventBase>;
protected readonly subscriptions: Subscription[];

private _publisher: IEventPublisher<EventBase>;
private _publisher: Publisher;
private readonly _logger = new Logger(EventBus.name);

constructor(
Expand All @@ -63,7 +74,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
this.options?.eventIdProvider ?? defaultEventIdProvider;

if (this.options?.eventPublisher) {
this._publisher = this.options.eventPublisher;
this._publisher = this.options.eventPublisher as Publisher;
} else {
this.useDefaultPublisher();
}
Expand All @@ -73,7 +84,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
* Returns the publisher.
* Default publisher is `DefaultPubSub` (in memory).
*/
get publisher(): IEventPublisher<EventBase> {
get publisher(): Publisher {
return this._publisher;
}

Expand All @@ -82,7 +93,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
* Default publisher is `DefaultPubSub` (in memory).
* @param _publisher The publisher to set.
*/
set publisher(_publisher: IEventPublisher<EventBase>) {
set publisher(_publisher: Publisher) {
this._publisher = _publisher;
}

Expand All @@ -94,7 +105,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
* Publishes an event.
* @param event The event to publish.
*/
publish<TEvent extends EventBase>(event: TEvent): any;
publish<TEvent extends EventBase>(event: TEvent): PublishResult;
/**
* Publishes an event.
* @param event The event to publish.
Expand All @@ -103,7 +114,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
publish<TEvent extends EventBase>(
event: TEvent,
asyncContext: AsyncContext,
): any;
): PublishResult;
/**
* Publishes an event.
* @param event The event to publish.
Expand All @@ -112,7 +123,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
publish<TEvent extends EventBase, TContext = unknown>(
event: TEvent,
dispatcherContext: TContext,
): any;
): PublishResult;
/**
* Publishes an event.
* @param event The event to publish.
Expand All @@ -123,7 +134,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
event: TEvent,
dispatcherContext: TContext,
asyncContext: AsyncContext,
): any;
): PublishResult;
/**
* Publishes an event.
* @param event The event to publish.
Expand All @@ -134,7 +145,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
event: TEvent,
dispatcherOrAsyncContext?: TContext | AsyncContext,
asyncContext?: AsyncContext,
) {
): PublishResult {
if (!asyncContext && dispatcherOrAsyncContext instanceof AsyncContext) {
asyncContext = dispatcherOrAsyncContext;
dispatcherOrAsyncContext = undefined;
Expand All @@ -155,7 +166,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
* Publishes multiple events.
* @param events The events to publish.
*/
publishAll<TEvent extends EventBase>(events: TEvent[]): any;
publishAll<TEvent extends EventBase>(events: TEvent[]): PublishAllResult;
/**
* Publishes multiple events.
* @param events The events to publish.
Expand All @@ -164,7 +175,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
publishAll<TEvent extends EventBase>(
events: TEvent[],
asyncContext: AsyncContext,
): any;
): PublishAllResult;
/**
* Publishes multiple events.
* @param events The events to publish.
Expand All @@ -173,7 +184,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
publishAll<TEvent extends EventBase, TContext = unknown>(
events: TEvent[],
dispatcherContext: TContext,
): any;
): PublishAllResult;
/**
* Publishes multiple events.
* @param events The events to publish.
Expand All @@ -184,7 +195,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
events: TEvent[],
dispatcherContext: TContext,
asyncContext: AsyncContext,
): any;
): PublishAllResult;
/**
* Publishes multiple events.
* @param events The events to publish.
Expand All @@ -195,7 +206,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
events: TEvent[],
dispatcherOrAsyncContext?: TContext | AsyncContext,
asyncContext?: AsyncContext,
) {
): PublishAllResult {
if (!asyncContext && dispatcherOrAsyncContext instanceof AsyncContext) {
asyncContext = dispatcherOrAsyncContext;
dispatcherOrAsyncContext = undefined;
Expand All @@ -219,7 +230,7 @@ export class EventBus<EventBase extends IEvent = IEvent>
}
return (events || []).map((event) =>
this._publisher.publish(event, dispatcherOrAsyncContext, asyncContext),
);
) as PublishAllResult;
}

bind(handler: InstanceWrapper<IEventHandler<EventBase>>, id: string) {
Expand Down Expand Up @@ -380,7 +391,9 @@ export class EventBus<EventBase extends IEvent = IEvent>
}

private useDefaultPublisher() {
this._publisher = new DefaultPubSub<EventBase>(this.subject$);
this._publisher = new DefaultPubSub<EventBase>(
this.subject$,
) as unknown as Publisher;
}

private mapToUnhandledErrorInfo(
Expand Down
20 changes: 17 additions & 3 deletions src/interfaces/events/event-publisher.interface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { AsyncContext } from '../../scopes';
import { IEvent } from './event.interface';

export interface IEventPublisher<EventBase extends IEvent = IEvent> {
export interface IEventPublisher<
EventBase extends IEvent = IEvent,
PublishResult = any,
PublishAllResult = any,
> {
/**
* Publishes an event.
* @param event The event to publish.
Expand All @@ -12,7 +16,7 @@ export interface IEventPublisher<EventBase extends IEvent = IEvent> {
event: TEvent,
dispatcherContext?: unknown,
asyncContext?: AsyncContext,
): any;
): PublishResult;

/**
* Publishes multiple events.
Expand All @@ -24,5 +28,15 @@ export interface IEventPublisher<EventBase extends IEvent = IEvent> {
events: TEvent[],
dispatcherContext?: unknown,
asyncContext?: AsyncContext,
): any;
): PublishAllResult;
}

export type PublisherPublishResult<P extends IEventPublisher> =
P extends IEventPublisher<any, infer PublishResult> ? PublishResult : never;

export type PublisherPublishAllResult<P extends IEventPublisher> =
P extends IEventPublisher<any, infer PublishResult, infer PublishAllResult>
? P['publishAll'] extends Function
? PublishAllResult
: PublishResult[]
: never;
Loading