Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modules-sdk): Module as singleton instances #4065

Merged
merged 8 commits into from
May 18, 2023
5 changes: 5 additions & 0 deletions .changeset/tough-ears-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/modules-sdk": patch
---

chore: module as singleton instances
61 changes: 61 additions & 0 deletions packages/modules-sdk/src/loaders/__tests__/medusa-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
InternalModuleDeclaration,
MODULE_RESOURCE_TYPE,
MODULE_SCOPE,
} from "@medusajs/types"
import { MedusaModule } from "../../medusa-module"

const mockRegisterMedusaModule = jest
.fn()
.mockImplementation(() => Promise.resolve([]))
const mockModuleLoader = jest.fn().mockImplementation(() => Promise.resolve({}))

jest.mock("./../../loaders", () => ({
registerMedusaModule: jest
.fn()
.mockImplementation((...args) => mockRegisterMedusaModule()),
moduleLoader: jest.fn().mockImplementation((...args) => mockModuleLoader()),
}))

describe("Medusa Module", () => {
beforeEach(() => {
jest.resetModules()
jest.clearAllMocks()
})

it("MedusaModule bootstrap - Singleton instances", async () => {
await MedusaModule.bootstrap(
"moduleKey",
"@path",
{
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
},
} as InternalModuleDeclaration,
{}
)

expect(mockRegisterMedusaModule).toBeCalledTimes(1)
expect(mockModuleLoader).toBeCalledTimes(1)

await MedusaModule.bootstrap(
"moduleKey",
"@path",
{
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.ISOLATED,
resolve: "@path",
options: {
abc: 123,
},
} as InternalModuleDeclaration,
{}
)

expect(mockRegisterMedusaModule).toBeCalledTimes(1)
expect(mockModuleLoader).toBeCalledTimes(1)
})
})
14 changes: 2 additions & 12 deletions packages/modules-sdk/src/loaders/__tests__/module-loader.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import {
ModuleResolution,
MODULE_RESOURCE_TYPE,
MODULE_SCOPE,
ModuleResolution,
} from "@medusajs/types"
import { AwilixContainer, ClassOrFunctionReturning, Resolver } from "awilix"
import { createMedusaContainer } from "medusa-core-utils"
import { EOL } from "os"
import { moduleLoader } from "../module-loader"
import { trackInstallation } from "../__mocks__/medusa-telemetry"

function asArray(
resolvers: (ClassOrFunctionReturning<unknown> | Resolver<unknown>)[]
): { resolve: (container: AwilixContainer) => unknown[] } {
return {
resolve: (container: AwilixContainer): unknown[] =>
resolvers.map((resolver) => container.build(resolver)),
}
}
import { moduleLoader } from "../module-loader"

const logger = {
warn: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions packages/modules-sdk/src/loaders/register-modules.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
ExternalModuleDeclaration,
InternalModuleDeclaration,
MODULE_SCOPE,
ModuleDefinition,
ModuleResolution,
MODULE_SCOPE,
} from "@medusajs/types"
import resolveCwd from "resolve-cwd"
import MODULE_DEFINITIONS from "../definitions"
Expand Down Expand Up @@ -49,7 +49,7 @@ export const registerMedusaModule = (
}

if (moduleDeclaration.scope === MODULE_SCOPE.EXTERNAL) {
// TODO: getExternalModuleResolution(...)a
// TODO: getExternalModuleResolution(...)
throw new Error("External Modules are not supported yet.")
}

Expand Down
7 changes: 7 additions & 0 deletions packages/modules-sdk/src/medusa-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const logger: any = {
}

export class MedusaModule {
private static instances_: Map<string, any> = new Map()
public static async bootstrap(
moduleKey: string,
defaultPath: string,
Expand All @@ -25,6 +26,10 @@ export class MedusaModule {
): Promise<{
[key: string]: any
}> {
if (MedusaModule.instances_.has(moduleKey)) {
return MedusaModule.instances_.get(moduleKey)
}

let modDeclaration = declaration
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) {
modDeclaration = {
Expand Down Expand Up @@ -56,6 +61,8 @@ export class MedusaModule {
services[keyName] = container.resolve(registrationName)
}

MedusaModule.instances_.set(moduleKey, services)

return services
}

Expand Down