Bugfix: fix race conditions
This commit is contained in:
+17
-34
@@ -3,15 +3,29 @@ import delay from "../../utils/delay";
|
||||
import { SyncFilesFnParams } from "../../types";
|
||||
|
||||
import sync from "../../utils/sync";
|
||||
import SyncScheduler from "../../utils/sync-scheduler";
|
||||
|
||||
export default async function watchFiles({
|
||||
files,
|
||||
options,
|
||||
}: SyncFilesFnParams) {
|
||||
let timeout: any;
|
||||
const UPDATE_TIMEOUT = 1000;
|
||||
|
||||
try {
|
||||
const INTERVAL = options?.interval ? options.interval : UPDATE_TIMEOUT;
|
||||
|
||||
const scheduler = new SyncScheduler(
|
||||
(filePath, firstRun) =>
|
||||
sync({
|
||||
options,
|
||||
dirPath: filePath,
|
||||
dirs: files,
|
||||
isFiles: true,
|
||||
firstRun,
|
||||
}),
|
||||
INTERVAL
|
||||
);
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const filePath =
|
||||
@@ -61,26 +75,7 @@ export default async function watchFiles({
|
||||
interval: interval || 200,
|
||||
},
|
||||
(curr, prev) => {
|
||||
if (global.SYNCING) return;
|
||||
|
||||
const INTERVAL = options?.interval
|
||||
? options.interval
|
||||
: UPDATE_TIMEOUT;
|
||||
|
||||
clearTimeout(timeout);
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
global.SYNCING = true;
|
||||
|
||||
sync({
|
||||
options,
|
||||
dirPath: filePath,
|
||||
dirs: files,
|
||||
isFiles: true,
|
||||
}).finally(() => {
|
||||
process.exit(global.SYNC_SUCCESS_EXIT_CODE);
|
||||
});
|
||||
}, INTERVAL);
|
||||
scheduler.schedule(filePath);
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -92,19 +87,7 @@ export default async function watchFiles({
|
||||
? lastUpdatedFile
|
||||
: lastUpdatedFile.path;
|
||||
|
||||
global.SYNCING = true;
|
||||
|
||||
await sync({
|
||||
dirPath: lastUpdatedFilePath,
|
||||
dirs: files,
|
||||
options,
|
||||
isFiles: true,
|
||||
firstRun: true,
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
global.SYNCING = false;
|
||||
}, UPDATE_TIMEOUT);
|
||||
scheduler.enqueue(lastUpdatedFilePath, true);
|
||||
} catch (error: any) {
|
||||
console.log("ERROR:", error.message);
|
||||
process.exit(0);
|
||||
|
||||
Reference in New Issue
Block a user