diff --git a/packages/core/admin/server/services/passport.js b/packages/core/admin/server/services/passport.js index c0d767a2bc..3681218a5b 100644 --- a/packages/core/admin/server/services/passport.js +++ b/packages/core/admin/server/services/passport.js @@ -24,11 +24,7 @@ const registerAuthEvents = () => { const eventList = Object.entries(events).filter(keyIsValidEventName).filter(valueIsFunctionType); for (const [eventName, handler] of eventList) { - strapi.eventHub.addSubscriber((name, ...args) => { - if (name === authEventsMapper[eventName]) { - handler(...args); - } - }); + strapi.eventHub.on(authEventsMapper[eventName], handler); } }; diff --git a/packages/core/strapi/lib/Strapi.js b/packages/core/strapi/lib/Strapi.js index 39b2697738..5cfbdd5094 100644 --- a/packages/core/strapi/lib/Strapi.js +++ b/packages/core/strapi/lib/Strapi.js @@ -225,7 +225,7 @@ class Strapi { await this.runLifecyclesFunctions(LIFECYCLES.DESTROY); - this.eventHub.removeAllSubscribers(); + this.eventHub.destroy(); if (_.has(this, 'db')) { await this.db.destroy(); diff --git a/packages/core/strapi/lib/services/event-hub.js b/packages/core/strapi/lib/services/event-hub.js index 17c01949c8..dcaab6a9e2 100644 --- a/packages/core/strapi/lib/services/event-hub.js +++ b/packages/core/strapi/lib/services/event-hub.js @@ -4,17 +4,24 @@ * The event hub is Strapi's event control center. */ module.exports = function createEventHub() { + const listeners = new Map(); + + // Default subscriber to easily add listeners with the on() method + const defaultSubscriber = async (eventName, ...args) => { + if (listeners.has(eventName)) { + return Promise.all(listeners.get(eventName).map((listener) => listener(...args))); + } + }; + // Store of subscribers that will be called when an event is emitted - const subscribers = []; + const subscribers = [defaultSubscriber]; return { - emit(eventName, ...args) { - subscribers.forEach((subscriber) => { - subscriber(eventName, ...args); - }); + async emit(eventName, ...args) { + await Promise.all(subscribers.map((subscriber) => subscriber(eventName, ...args))); }, - addSubscriber(subscriber) { + subscribe(subscriber) { subscribers.push(subscriber); // Return a function to remove the subscriber @@ -23,7 +30,20 @@ module.exports = function createEventHub() { }; }, - removeAllSubscribers() { + on(eventName, listener) { + if (!listeners.has(eventName)) { + listeners.set(eventName, []); + } + + listeners.get(eventName).push(listener); + + // Return a function to remove the listener + return () => { + listeners.get(eventName).splice(listeners.get(eventName).indexOf(listener), 1); + }; + }, + + destroy() { subscribers.length = 0; }, }; diff --git a/packages/core/strapi/lib/services/webhook-runner.js b/packages/core/strapi/lib/services/webhook-runner.js index 7a4033185e..1a4015b0f2 100644 --- a/packages/core/strapi/lib/services/webhook-runner.js +++ b/packages/core/strapi/lib/services/webhook-runner.js @@ -33,7 +33,7 @@ class WebhookRunner { this.queue = new WorkerQueue({ logger, concurrency: 5 }); this.queue.subscribe(this.executeListener.bind(this)); - strapi.eventHub.addSubscriber((eventName, ...args) => { + strapi.eventHub.subscribe((eventName, ...args) => { if (this.listeners.has(eventName)) { const listener = this.listeners.get(eventName); listener(...args);