@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
59 lines (56 loc) • 1.91 kB
JavaScript
;
const AUTO_DTO_CONTEXT_QUEUE = new WeakMap();
const AUTO_DTO_CONTEXT_LISTENERS = new WeakMap();
/**
* Executes and clears all queued handlers that were waiting for the DTO context on the specified prototype.
* @param {object} target - DTO prototype whose queued handlers should run.
*/
function FlushAutoDtoContextExecutions(target) {
const queue = AUTO_DTO_CONTEXT_QUEUE.get(target);
if (queue) {
AUTO_DTO_CONTEXT_QUEUE.delete(target);
for (const handler of queue.values()) {
try {
handler();
}
catch {
// ignore to avoid breaking other handlers
}
}
}
const listeners = AUTO_DTO_CONTEXT_LISTENERS.get(target);
if (!listeners)
return;
for (const listener of listeners) {
try {
const shouldKeepListener = listener();
if (!shouldKeepListener) {
listeners.delete(listener);
}
}
catch {
listeners.delete(listener);
}
}
if (listeners.size === 0) {
AUTO_DTO_CONTEXT_LISTENERS.delete(target);
}
}
/**
* Queues a handler to run once the DTO context becomes available for the specified prototype.
* @param {object} target - DTO prototype awaiting context.
* @param {() => void} handler - Callback to execute after context is defined.
*/
function QueueAutoDtoContextExecution(target, handler) {
if (!target || typeof handler !== "function")
return;
let queue = AUTO_DTO_CONTEXT_QUEUE.get(target);
if (!queue) {
queue = new Set();
AUTO_DTO_CONTEXT_QUEUE.set(target, queue);
}
queue.add(handler);
}
exports.FlushAutoDtoContextExecutions = FlushAutoDtoContextExecutions;
exports.QueueAutoDtoContextExecution = QueueAutoDtoContextExecution;
//# sourceMappingURL=auto-dto-context-queue.utility.js.map