@@ -2,7 +2,7 @@ import * as lockfile from "lockfile";
22import * as path from "path" ;
33import { cache } from "../decorators" ;
44
5- export class LockFile implements ILockFile {
5+ export class LockService implements ILockService {
66 private currentlyLockedFiles : string [ ] = [ ] ;
77
88 @cache ( )
@@ -14,10 +14,10 @@ export class LockFile implements ILockFile {
1414 return path . join ( this . $settingsService . getProfileDir ( ) , relativeLockFilePath ) ;
1515 }
1616
17- private get defaultLockParams ( ) : ILockFileOptions {
17+ private get defaultLockParams ( ) : ILockOptions {
1818 // We'll retry 100 times and time between retries is 100ms, i.e. full wait in case we are unable to get lock will be 10 seconds.
1919 // In case lock is older than the `stale` value, consider it stale and try to get a new lock.
20- const lockParams : ILockFileOptions = {
20+ const lockParams : ILockOptions = {
2121 retryWait : 100 ,
2222 retries : 100 ,
2323 stale : 30 * 1000 ,
@@ -37,8 +37,19 @@ export class LockFile implements ILockFile {
3737 } ) ;
3838 }
3939
40- public lock ( lockFilePath ?: string , lockFileOpts ?: ILockFileOptions ) : Promise < string > {
41- const { filePath, fileOpts } = this . getLockFileSettings ( lockFilePath , lockFileOpts ) ;
40+ public async executeActionWithLock < T > ( action : ( ) => Promise < T > , lockFilePath ?: string , lockOpts ?: ILockOptions ) : Promise < T > {
41+ const resolvedLockFilePath = await this . lock ( lockFilePath , lockOpts ) ;
42+
43+ try {
44+ const result = await action ( ) ;
45+ return result ;
46+ } finally {
47+ this . unlock ( resolvedLockFilePath ) ;
48+ }
49+ }
50+
51+ private lock ( lockFilePath ?: string , lockOpts ?: ILockOptions ) : Promise < string > {
52+ const { filePath, fileOpts } = this . getLockFileSettings ( lockFilePath , lockOpts ) ;
4253 this . currentlyLockedFiles . push ( filePath ) ;
4354
4455 // Prevent ENOENT error when the dir, where lock should be created, does not exist.
@@ -51,30 +62,13 @@ export class LockFile implements ILockFile {
5162 } ) ;
5263 }
5364
54- public async executeActionWithLock < T > ( action : ( ) => Promise < T > , lockFilePath ?: string , lockFileOpts ?: ILockFileOptions ) : Promise < T > {
55- const resolvedLockFilePath = await this . lock ( lockFilePath , lockFileOpts ) ;
56-
57- try {
58- const result = await action ( ) ;
59- return result ;
60- } finally {
61- this . unlock ( resolvedLockFilePath ) ;
62- }
63- }
64-
65- public unlock ( lockFilePath ?: string ) : void {
65+ private unlock ( lockFilePath ?: string ) : void {
6666 const { filePath } = this . getLockFileSettings ( lockFilePath ) ;
6767 _ . remove ( this . currentlyLockedFiles , e => e === lockFilePath ) ;
6868 lockfile . unlockSync ( filePath ) ;
6969 }
7070
71- public check ( lockFilePath ?: string , lockFileOpts ?: ILockFileOptions ) : boolean {
72- const { filePath, fileOpts } = this . getLockFileSettings ( lockFilePath , lockFileOpts ) ;
73-
74- return lockfile . checkSync ( filePath , fileOpts ) ;
75- }
76-
77- private getLockFileSettings ( filePath ?: string , fileOpts ?: ILockFileOptions ) : { filePath : string , fileOpts : ILockFileOptions } {
71+ private getLockFileSettings ( filePath ?: string , fileOpts ?: ILockOptions ) : { filePath : string , fileOpts : ILockOptions } {
7872 if ( filePath && ! path . isAbsolute ( filePath ) ) {
7973 filePath = this . getAbsoluteLockFilePath ( filePath ) ;
8074 }
@@ -89,4 +83,6 @@ export class LockFile implements ILockFile {
8983 }
9084}
9185
92- $injector . register ( "lockfile" , LockFile ) ;
86+ $injector . register ( "lockService" , LockService ) ;
87+ // backwards compatibility
88+ $injector . register ( "lockfile" , LockService ) ;
0 commit comments