feat: Add support for exporting products in backend (#8214)

CLOSES CC-221
CLOSES CC-223
CLOSES CC-224
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-07-22 13:40:04 +00:00
1 parent e9f1aafbb1
commit 0d2e7befbd
29 files changed
+615 -690

No files matched your search

+22 -8
View File
@@ -1,4 +1,5 @@
import { IEventBusModuleService } from "@medusajs/types"
import { EventEmitter } from "events"
// Allows you to wait for all subscribers to execute for a given event. Only works with the local event bus.
export const waitSubscribersExecution = (
@@ -6,23 +7,36 @@ export const waitSubscribersExecution = (
eventBus: IEventBusModuleService
) => {
const subscriberPromises: Promise<any>[] = []
const eventEmitter: EventEmitter = (eventBus as any).eventEmitter_
;(eventBus as any).eventEmitter_.listeners(eventName).forEach((listener) => {
;(eventBus as any).eventEmitter_.removeListener("order.created", listener)
// If there are no existing listeners, resolve once the event happens. Otherwise, wrap the existing subscribers in a promise and resolve once they are done.
if (!eventEmitter.listeners(eventName).length) {
let ok, nok
const promise = new Promise((resolve, reject) => {
ok = resolve
nok = reject
})
subscriberPromises.push(promise)
eventEmitter.on(eventName, ok)
} else {
eventEmitter.listeners(eventName).forEach((listener: any) => {
eventEmitter.removeListener(eventName, listener)
const newListener = async (...args2) => {
return await listener.apply(eventBus, args2).then(ok).catch(nok)
}
let ok, nok
const promise = new Promise((resolve, reject) => {
ok = resolve
nok = reject
})
subscriberPromises.push(promise)
;(eventBus as any).eventEmitter_.on("order.created", newListener)
})
const newListener = async (...args2) => {
return await listener.apply(eventBus, args2).then(ok).catch(nok)
}
eventEmitter.on(eventName, newListener)
})
}
return Promise.all(subscriberPromises)
}