-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const locks = new Set<string>() | ||
export async function lock( | ||
filePath: string, | ||
_options: { | ||
onCompromised: (err: Error) => void | ||
} | ||
): Promise<() => Promise<void>> { | ||
await sleep(1) | ||
|
||
if (locks.has(filePath)) { | ||
const err = new Error('ELOCKED: File is already locked') | ||
;(err as any).code = 'ELOCKED' | ||
throw err | ||
} else { | ||
locks.add(filePath) | ||
} | ||
|
||
return async () => { | ||
// release lock | ||
|
||
if (!locks.has(filePath)) { | ||
const err = new Error('ELOCKED: File is already released') | ||
;(err as any).code = 'ERELEASED' | ||
throw err | ||
} else { | ||
locks.delete(filePath) | ||
} | ||
} | ||
} | ||
|
||
async function sleep(duration: number): Promise<void> { | ||
return new Promise((r) => setTimeout(r, duration)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters