feat: add emitAsync support

This commit is contained in:
yassinedorbozgithub 2025-03-19 11:24:34 +01:00
parent affcd12b52
commit 6da932977f
3 changed files with 20 additions and 21 deletions

View File

@ -175,14 +175,12 @@ describe('SubscriberRepository', () => {
await subscriberRepository.updateOne(oldSubscriber.id, updates); await subscriberRepository.updateOne(oldSubscriber.id, updates);
expect(eventEmitter.emit).toHaveBeenNthCalledWith( expect(eventEmitter.emit).toHaveBeenCalledWith(
3,
'hook:subscriber:assign', 'hook:subscriber:assign',
expect.anything(), expect.anything(),
expect.anything(), expect.anything(),
); );
expect(eventEmitter.emit).toHaveBeenNthCalledWith( expect(eventEmitter.emit).toHaveBeenCalledWith(
4,
'hook:analytics:passation', 'hook:analytics:passation',
expect.anything(), expect.anything(),
true, // Because assignedTo has changed true, // Because assignedTo has changed

View File

@ -112,7 +112,7 @@ export abstract class BaseRepository<
hooks.validate.pre.execute(async function () { hooks.validate.pre.execute(async function () {
const doc = this as HydratedDocument<T>; const doc = this as HydratedDocument<T>;
await repository.preCreateValidate(doc); await repository.preCreateValidate(doc);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.preCreateValidate), repository.getEventName(EHook.preCreateValidate),
doc, doc,
); );
@ -120,7 +120,7 @@ export abstract class BaseRepository<
hooks.validate.post.execute(async function (created: HydratedDocument<T>) { hooks.validate.post.execute(async function (created: HydratedDocument<T>) {
await repository.postCreateValidate(created); await repository.postCreateValidate(created);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.postCreateValidate), repository.getEventName(EHook.postCreateValidate),
created, created,
); );
@ -129,12 +129,15 @@ export abstract class BaseRepository<
hooks.save.pre.execute(async function () { hooks.save.pre.execute(async function () {
const doc = this as HydratedDocument<T>; const doc = this as HydratedDocument<T>;
await repository.preCreate(doc); await repository.preCreate(doc);
repository.emitter.emit(repository.getEventName(EHook.preCreate), doc); await repository.emitter.emitAsync(
repository.getEventName(EHook.preCreate),
doc,
);
}); });
hooks.save.post.execute(async function (created: HydratedDocument<T>) { hooks.save.post.execute(async function (created: HydratedDocument<T>) {
await repository.postCreate(created); await repository.postCreate(created);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.postCreate), repository.getEventName(EHook.postCreate),
created, created,
); );
@ -144,7 +147,7 @@ export abstract class BaseRepository<
const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>; const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>;
const criteria = query.getQuery(); const criteria = query.getQuery();
await repository.preDelete(query, criteria); await repository.preDelete(query, criteria);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.preDelete), repository.getEventName(EHook.preDelete),
query, query,
criteria, criteria,
@ -154,7 +157,7 @@ export abstract class BaseRepository<
hooks?.deleteOne.post.execute(async function (result: DeleteResult) { hooks?.deleteOne.post.execute(async function (result: DeleteResult) {
const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>; const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>;
await repository.postDelete(query, result); await repository.postDelete(query, result);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.postDelete), repository.getEventName(EHook.postDelete),
query, query,
result, result,
@ -168,7 +171,7 @@ export abstract class BaseRepository<
}); });
hooks.deleteMany.post.execute(async function (result: DeleteResult) { hooks.deleteMany.post.execute(async function (result: DeleteResult) {
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.postDelete), repository.getEventName(EHook.postDelete),
result, result,
); );
@ -184,7 +187,7 @@ export abstract class BaseRepository<
throw new Error('Unable to run findOneAndUpdate pre hook'); throw new Error('Unable to run findOneAndUpdate pre hook');
} }
await repository.preUpdate(query, criteria, updates); await repository.preUpdate(query, criteria, updates);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.preUpdate), repository.getEventName(EHook.preUpdate),
criteria, criteria,
updates?.['$set'], updates?.['$set'],
@ -199,7 +202,7 @@ export abstract class BaseRepository<
throw new Error('Unable to execute updateMany() pre-hook'); throw new Error('Unable to execute updateMany() pre-hook');
} }
await repository.preUpdateMany(query, criteria, updates); await repository.preUpdateMany(query, criteria, updates);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.preUpdateMany), repository.getEventName(EHook.preUpdateMany),
criteria, criteria,
updates?.['$set'], updates?.['$set'],
@ -209,7 +212,7 @@ export abstract class BaseRepository<
hooks.updateMany.post.execute(async function (updated: any) { hooks.updateMany.post.execute(async function (updated: any) {
const query = this as Query<D, D, unknown, T, 'updateMany'>; const query = this as Query<D, D, unknown, T, 'updateMany'>;
await repository.postUpdateMany(query, updated); await repository.postUpdateMany(query, updated);
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.postUpdateMany), repository.getEventName(EHook.postUpdateMany),
updated, updated,
); );
@ -224,7 +227,7 @@ export abstract class BaseRepository<
query, query,
plainToClass(repository.cls, updated, repository.transformOpts), plainToClass(repository.cls, updated, repository.transformOpts),
); );
repository.emitter.emit( await repository.emitter.emitAsync(
repository.getEventName(EHook.postUpdate), repository.getEventName(EHook.postUpdate),
updated, updated,
); );
@ -500,14 +503,14 @@ export abstract class BaseRepository<
} }
await this.preUpdateValidate(filterCriteria, queryUpdates); await this.preUpdateValidate(filterCriteria, queryUpdates);
this.emitter.emit( await this.emitter.emitAsync(
this.getEventName(EHook.preUpdateValidate), this.getEventName(EHook.preUpdateValidate),
filterCriteria, filterCriteria,
queryUpdates, queryUpdates,
); );
await this.postUpdateValidate(filterCriteria, queryUpdates); await this.postUpdateValidate(filterCriteria, queryUpdates);
this.emitter.emit( await this.emitter.emitAsync(
this.getEventName(EHook.postUpdateValidate), this.getEventName(EHook.postUpdateValidate),
filterCriteria, filterCriteria,
queryUpdates, queryUpdates,

View File

@ -6,6 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { type OnEventOptions } from '@nestjs/event-emitter/dist/interfaces';
import { type Session as ExpressSession } from 'express-session'; import { type Session as ExpressSession } from 'express-session';
import type { Document, Query } from 'mongoose'; import type { Document, Query } from 'mongoose';
import { type Socket } from 'socket.io'; import { type Socket } from 'socket.io';
@ -411,8 +412,5 @@ declare module '@nestjs/event-emitter' {
declare function OnEvent< declare function OnEvent<
G extends EventNamespaces | ConstrainedString, G extends EventNamespaces | ConstrainedString,
H extends G, H extends G,
>( >(event: customEvent<G>, options?: OnEventOptions): OnEventMethodDecorator<H>;
event: customEvent<G>,
options?: OnEventOptions | undefined,
): OnEventMethodDecorator<H>;
} }