Feat(modules-sdk): Run hooks on application start (#6111)
**What** - run `onApplicationStart` hook when starting modules
This commit is contained in:
1 parent
571b64cd16
commit
2b9f98895e
3 files changed
+32
-7
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(modules-sdk): run hooks on application start
|
||||||
@@ -308,12 +308,16 @@ export async function MedusaApp(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
try {
|
||||||
modules: allModules,
|
return {
|
||||||
link: remoteLink,
|
modules: allModules,
|
||||||
query,
|
link: remoteLink,
|
||||||
entitiesMap: schema.getTypeMap(),
|
query,
|
||||||
notFound,
|
entitiesMap: schema.getTypeMap(),
|
||||||
runMigrations,
|
notFound,
|
||||||
|
runMigrations,
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await MedusaModule.onApplicationStart()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ExternalModuleDeclaration,
|
ExternalModuleDeclaration,
|
||||||
|
IModuleService,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
LinkModuleDefinition,
|
LinkModuleDefinition,
|
||||||
LoadedModule,
|
LoadedModule,
|
||||||
@@ -85,6 +86,21 @@ export class MedusaModule {
|
|||||||
return MedusaModule.getModuleInstance(key)
|
return MedusaModule.getModuleInstance(key)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static onApplicationStart(): void {
|
||||||
|
for (const instances of MedusaModule.instances_.values()) {
|
||||||
|
for (const instance of Object.values(instances) as IModuleService[]) {
|
||||||
|
if (instance?.__hooks) {
|
||||||
|
instance.__hooks?.onApplicationStart
|
||||||
|
?.bind(instance)()
|
||||||
|
.catch(() => {
|
||||||
|
// The module should handle this and log it
|
||||||
|
return void 0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static clearInstances(): void {
|
public static clearInstances(): void {
|
||||||
MedusaModule.instances_.clear()
|
MedusaModule.instances_.clear()
|
||||||
|
|||||||
Reference in new issue
Block a user