Skip to content

Commit

Permalink
Feat(modules-sdk): Run hooks on application start (#6111)
Browse files Browse the repository at this point in the history
**What**
- run `onApplicationStart` hook when starting modules
  • Loading branch information
pKorsholm authored Jan 17, 2024
1 parent 571b64c commit 2b9f988
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-seals-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/modules-sdk": patch
---

feat(modules-sdk): run hooks on application start
18 changes: 11 additions & 7 deletions packages/modules-sdk/src/medusa-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,16 @@ export async function MedusaApp(
}))
}

return {
modules: allModules,
link: remoteLink,
query,
entitiesMap: schema.getTypeMap(),
notFound,
runMigrations,
try {
return {
modules: allModules,
link: remoteLink,
query,
entitiesMap: schema.getTypeMap(),
notFound,
runMigrations,
}
} finally {
await MedusaModule.onApplicationStart()
}
}
16 changes: 16 additions & 0 deletions packages/modules-sdk/src/medusa-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ExternalModuleDeclaration,
IModuleService,
InternalModuleDeclaration,
LinkModuleDefinition,
LoadedModule,
Expand Down Expand Up @@ -85,6 +86,21 @@ export class MedusaModule {
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 {
MedusaModule.instances_.clear()
Expand Down

1 comment on commit 2b9f988

@vercel
Copy link

@vercel vercel bot commented on 2b9f988 Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

medusa-dashboard – ./packages/admin-next/dashboard

admin-preview.medusajs.com
medusa-dashboard.vercel.app
medusa-dashboard-git-develop-medusajs.vercel.app
medusa-dashboard-medusajs.vercel.app

Please sign in to comment.