الملفات
codepill-sfu/app/src/MutexManager.js
2025-04-23 21:57:23 +02:00

30 أسطر
646 B
JavaScript

'use strict';
const { Mutex } = require('async-mutex');
// In-memory file mutex registry
const fileLocks = new Map();
function getFileMutex(filePath) {
if (!fileLocks.has(filePath)) {
fileLocks.set(filePath, new Mutex());
}
return fileLocks.get(filePath);
}
async function withFileLock(filePath, fn) {
const mutex = getFileMutex(filePath);
const release = await mutex.acquire();
try {
return await fn();
} finally {
release();
if (!mutex.isLocked()) {
fileLocks.delete(filePath); // Clean up when no one is waiting
}
}
}
module.exports = { withFileLock };