mirror of
https://github.com/directus/directus.git
synced 2026-01-29 02:27:56 -05:00
Clean up emitting "items" events (#11163)
This commit is contained in:
committed by
GitHub
parent
5cf57b4e2d
commit
b96fb77cbf
@@ -22,9 +22,14 @@ export class Emitter {
|
||||
this.initEmitter = new EventEmitter2(emitterOptions);
|
||||
}
|
||||
|
||||
public async emitFilter<T>(event: string, payload: T, meta: Record<string, any>, context: HookContext): Promise<T> {
|
||||
const events = this.eventsToEmit(event, meta);
|
||||
const listeners = events.flatMap((event) => this.filterEmitter.listeners(event)) as FilterHandler[];
|
||||
public async emitFilter<T>(
|
||||
event: string | string[],
|
||||
payload: T,
|
||||
meta: Record<string, any>,
|
||||
context: HookContext
|
||||
): Promise<T> {
|
||||
const events = Array.isArray(event) ? event : [event];
|
||||
const listeners: FilterHandler[] = events.flatMap((event) => this.filterEmitter.listeners(event));
|
||||
|
||||
let updatedPayload = payload;
|
||||
for (const listener of listeners) {
|
||||
@@ -38,8 +43,9 @@ export class Emitter {
|
||||
return updatedPayload;
|
||||
}
|
||||
|
||||
public emitAction(event: string, meta: Record<string, any>, context: HookContext): void {
|
||||
const events = this.eventsToEmit(event, meta);
|
||||
public emitAction(event: string | string[], meta: Record<string, any>, context: HookContext): void {
|
||||
const events = Array.isArray(event) ? event : [event];
|
||||
|
||||
for (const event of events) {
|
||||
this.actionEmitter.emitAsync(event, meta, context).catch((err) => {
|
||||
logger.warn(`An error was thrown while executing action "${event}"`);
|
||||
@@ -86,13 +92,6 @@ export class Emitter {
|
||||
this.actionEmitter.removeAllListeners();
|
||||
this.initEmitter.removeAllListeners();
|
||||
}
|
||||
|
||||
private eventsToEmit(event: string, meta: Record<string, any>) {
|
||||
if (event.startsWith('items')) {
|
||||
return [event, `${meta.collection}.${event}`];
|
||||
}
|
||||
return [event];
|
||||
}
|
||||
}
|
||||
|
||||
const emitter = new Emitter();
|
||||
|
||||
@@ -99,7 +99,9 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
const payloadAfterHooks =
|
||||
opts?.emitEvents !== false
|
||||
? await emitter.emitFilter(
|
||||
`${this.eventScope}.create`,
|
||||
this.eventScope === 'items'
|
||||
? ['items.create', `${this.collection}.items.create`]
|
||||
: `${this.eventScope}.create`,
|
||||
payload,
|
||||
{
|
||||
collection: this.collection,
|
||||
@@ -195,7 +197,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
|
||||
if (opts?.emitEvents !== false) {
|
||||
emitter.emitAction(
|
||||
`${this.eventScope}.create`,
|
||||
this.eventScope === 'items' ? ['items.create', `${this.collection}.items.create`] : `${this.eventScope}.create`,
|
||||
{
|
||||
payload,
|
||||
key: primaryKey,
|
||||
@@ -280,7 +282,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
}
|
||||
|
||||
const filteredRecords = await emitter.emitFilter(
|
||||
`${this.eventScope}.read`,
|
||||
this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`,
|
||||
records,
|
||||
{
|
||||
query,
|
||||
@@ -294,7 +296,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
);
|
||||
|
||||
emitter.emitAction(
|
||||
`${this.eventScope}.read`,
|
||||
this.eventScope === 'items' ? ['items.read', `${this.collection}.items.read`] : `${this.eventScope}.read`,
|
||||
{
|
||||
payload: filteredRecords,
|
||||
query,
|
||||
@@ -381,7 +383,9 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
const payloadAfterHooks =
|
||||
opts?.emitEvents !== false
|
||||
? await emitter.emitFilter(
|
||||
`${this.eventScope}.update`,
|
||||
this.eventScope === 'items'
|
||||
? ['items.update', `${this.collection}.items.update`]
|
||||
: `${this.eventScope}.update`,
|
||||
payload,
|
||||
{
|
||||
keys,
|
||||
@@ -502,7 +506,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
|
||||
if (opts?.emitEvents !== false) {
|
||||
emitter.emitAction(
|
||||
`${this.eventScope}.update`,
|
||||
this.eventScope === 'items' ? ['items.update', `${this.collection}.items.update`] : `${this.eventScope}.update`,
|
||||
{
|
||||
payload,
|
||||
keys,
|
||||
@@ -604,7 +608,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
|
||||
if (opts?.emitEvents !== false) {
|
||||
await emitter.emitFilter(
|
||||
`${this.eventScope}.delete`,
|
||||
this.eventScope === 'items' ? ['items.delete', `${this.collection}.items.delete`] : `${this.eventScope}.delete`,
|
||||
keys,
|
||||
{
|
||||
collection: this.collection,
|
||||
@@ -645,7 +649,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
|
||||
|
||||
if (opts?.emitEvents !== false) {
|
||||
emitter.emitAction(
|
||||
`${this.eventScope}.delete`,
|
||||
this.eventScope === 'items' ? ['items.delete', `${this.collection}.items.delete`] : `${this.eventScope}.delete`,
|
||||
{
|
||||
payload: keys,
|
||||
collection: this.collection,
|
||||
|
||||
Reference in New Issue
Block a user