Skip to content

Commit a69ef4a

Browse files
authored
Fix several timing issues by ordering files with timestamps (#314)
1 parent 9e64a43 commit a69ef4a

2 files changed

Lines changed: 127 additions & 154 deletions

File tree

src/main/clipboard.ts

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type ClipboardType = "text" | "image" | "files";
3030

3131
export type ParsedClipboardFileName = {
3232
file: string;
33-
number: number;
33+
beat: number;
3434
clipboardType: ClipboardType;
3535
from: "myself" | "others";
3636
filesCount?: number;
@@ -43,7 +43,7 @@ export const parseClipboardFileName = (
4343
syncFolder: string,
4444
filter: "none" | "from-others" | "from-myself" = "none",
4545
): ParsedClipboardFileName | undefined => {
46-
let number = 0;
46+
let beat = 0;
4747
let clipboardType: ClipboardType;
4848
let from: "myself" | "others";
4949
let filesCount: number | undefined;
@@ -69,74 +69,43 @@ export const parseClipboardFileName = (
6969
switch (filter) {
7070
case "from-myself": {
7171
if (from === "myself") {
72-
number = Number.parseInt(match[1]);
72+
beat = Number.parseInt(match[1]);
7373
}
7474
break;
7575
}
7676
case "from-others": {
7777
if (from === "others") {
78-
number = Number.parseInt(match[1]);
78+
beat = Number.parseInt(match[1]);
7979
}
8080
break;
8181
}
8282
default: {
83-
number = Number.parseInt(match[1]);
83+
beat = Number.parseInt(match[1]);
8484
break;
8585
}
8686
}
8787
}
8888

89-
if (number === 0) {
89+
if (beat === 0) {
9090
return undefined;
9191
}
9292

9393
return {
9494
file: path.join(syncFolder, baseFile),
95-
number,
95+
beat,
9696
clipboardType,
9797
from,
9898
filesCount,
9999
};
100100
};
101101

102-
export async function getNextFileNumber(syncFolder: string): Promise<number> {
103-
const numbers: number[] = [];
104-
const files = await fs.readdir(syncFolder);
105-
for (const file of files) {
106-
const filePath = path.join(syncFolder, file);
107-
const parsedFile = parseClipboardFileName(filePath, syncFolder);
108-
if (parsedFile) {
109-
numbers.push(parsedFile.number);
110-
}
111-
}
112-
if (numbers.length > 0) {
113-
return Math.max(...numbers) + 1;
114-
}
115-
return 1;
116-
}
117-
118-
export async function isThereMoreThanOneClipboardFile(
119-
syncFolder: string,
120-
filter: "none" | "from-others" | "from-myself" = "none",
121-
): Promise<boolean> {
122-
const files = await fs.readdir(syncFolder);
123-
for (const file of files) {
124-
if (
125-
parseClipboardFileName(path.join(syncFolder, file), syncFolder, filter)
126-
) {
127-
return true;
128-
}
129-
}
130-
return false;
131-
}
132-
133102
export function isIsReceivingFile(file: string): boolean {
134103
return file.endsWith(isReceivingFileNameSuffix);
135104
}
136105

137106
export async function noComputersReceiving(
138107
syncFolder: string,
139-
currentTime: number,
108+
now: number,
140109
): Promise<boolean> {
141110
const directoryMembers = await fs.readdir(syncFolder);
142111
const computersReceiving = directoryMembers.filter(
@@ -145,10 +114,10 @@ export async function noComputersReceiving(
145114

146115
// This file will be renewed on every 4 minutes, this will conside stale
147116
// any files older than 10 minutes
148-
const tenMinutesAgo = new Date(currentTime - 600_000);
117+
const tenMinutesAgo = now - 600_000;
149118
for (const computerReceiving of computersReceiving) {
150119
const fileStat = await fs.stat(path.join(syncFolder, computerReceiving));
151-
if (fileStat.mtime >= tenMinutesAgo) {
120+
if (fileStat.ctimeMs >= tenMinutesAgo) {
152121
return false;
153122
}
154123
}
@@ -203,7 +172,7 @@ export async function cleanFiles(syncFolder: string): Promise<void> {
203172
throw error;
204173
}
205174

206-
if (fileStat.ctime.getTime() <= timeThreshold) {
175+
if (fileStat.ctimeMs <= timeThreshold) {
207176
log.info(`Deleting: ${filePath}`);
208177
await deleteFileOrFolderRecursively(filePath);
209178
continue;
@@ -213,7 +182,7 @@ export async function cleanFiles(syncFolder: string): Promise<void> {
213182
if (
214183
process.platform === "win32" &&
215184
parsedFile.from === "others" &&
216-
fileStat.ctime.getTime() <= now - 60_000
185+
fileStat.ctimeMs <= now - 60_000
217186
) {
218187
await unsyncFileOrFolderRecursively(filePath);
219188
}
@@ -272,6 +241,9 @@ export function isClipboardTextEquals(
272241
text1: ClipboardText,
273242
text2: ClipboardText,
274243
): boolean {
244+
if (text1 === undefined || text2 === undefined) {
245+
return false;
246+
}
275247
if (text1.text && text2.text) {
276248
return text1.text === text2.text;
277249
}

0 commit comments

Comments
 (0)