feat(locking): Locking module (#9524)
**What** - Locking Module to manage concurrency - Default `in-memory` provider
This commit is contained in:
committed by
GitHub
parent
5c9e289c4d
commit
c8b375ae2d
40
packages/modules/locking/src/services/locking-provider.ts
Normal file
40
packages/modules/locking/src/services/locking-provider.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Constructor, ILockingProvider } from "@medusajs/framework/types"
|
||||
import { MedusaError } from "@medusajs/framework/utils"
|
||||
import { LockingProviderRegistrationPrefix } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
[key: `lp_${string}`]: ILockingProvider
|
||||
}
|
||||
|
||||
export default class LockingProviderService {
|
||||
protected __container__: InjectedDependencies
|
||||
|
||||
constructor(container: InjectedDependencies) {
|
||||
this.__container__ = container
|
||||
}
|
||||
|
||||
static getRegistrationIdentifier(
|
||||
providerClass: Constructor<ILockingProvider>
|
||||
) {
|
||||
if (!(providerClass as any).identifier) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
`Trying to register a locking provider without an identifier.`
|
||||
)
|
||||
}
|
||||
return `${(providerClass as any).identifier}`
|
||||
}
|
||||
|
||||
public retrieveProviderRegistration(providerId: string): ILockingProvider {
|
||||
try {
|
||||
return this.__container__[
|
||||
`${LockingProviderRegistrationPrefix}${providerId}`
|
||||
]
|
||||
} catch (err) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Could not find a locking provider with id: ${providerId}`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user