@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
41 lines • 1.37 kB
JavaScript
/**
* @module SharedLock
*/
import { v4 } from "uuid";
import {} from "../../../middleware/contracts/_module.js";
import {} from "../../../shared-lock/contracts/_module.js";
import {} from "../../../time-span/contracts/time-span.contract.js";
import { callInvokable } from "../../../utilities/_module.js";
/**
* @group Middleware
*/
export const SHARED_LOCK_WHEN = {
READER: "READER",
WRITER: "WRITER",
};
/**
* IMPORT_PATH: `"@daiso-tech/core/shared-lock/middlewares"`
* @group Middleware
*/
export function sharedLockMiddlewareFactory(sharedLockFactory) {
return (settings) => {
const { key = (...args) => JSON.stringify(args), lockId = () => v4(), ...rest } = settings;
return ({ next, args }) => {
if (settings.when === SHARED_LOCK_WHEN.READER) {
return sharedLockFactory
.create(callInvokable(key, ...args), {
...rest,
lockId: callInvokable(lockId, ...args),
})
.runReaderOrFail(next);
}
return sharedLockFactory
.create(callInvokable(key, ...args), {
...rest,
lockId: callInvokable(lockId, ...args),
})
.runWriterOrFail(next);
};
};
}
//# sourceMappingURL=shared-lock-middleware-factory.js.map