@@ -4,38 +4,52 @@ import { Lock } from './lock.js'
44import { resolveDuration } from './helpers.js'
55import type {
66 Duration ,
7- LockFactoryConfig ,
7+ ResolvedLockConfig ,
88 LockFactoryOptions ,
99 LockStore ,
1010 SerializedLock ,
1111} from './types/main.js'
1212
1313export class LockFactory {
1414 /**
15- * Default TTL for locks. 30 seconds.
15+ * Default configuration values
1616 */
17- static #kDefaultTtl = 30_000
17+ static #kDefaults = {
18+ ttl : 30_000 ,
19+ retry : {
20+ attempts : Number . POSITIVE_INFINITY ,
21+ delay : 250 ,
22+ timeout : undefined ,
23+ } ,
24+ }
25+
26+ /**
27+ * The store used to persist locks
28+ */
29+ #store: LockStore
1830
1931 /**
2032 * Resolved LockFactory configuration
2133 */
22- #config: LockFactoryConfig
34+ #config: ResolvedLockConfig
2335
24- constructor (
25- protected readonly store : LockStore ,
26- options : LockFactoryOptions = { } ,
27- ) {
36+ constructor ( store : LockStore , options : LockFactoryOptions = { } ) {
37+ this . #store = store
2838 this . #config = {
29- retry : { attempts : null , delay : 250 , ...options . retry } ,
39+ retry : {
40+ attempts : options . retry ?. attempts ?? LockFactory . #kDefaults. retry . attempts ,
41+ delay : resolveDuration ( options . retry ?. delay , null ) ?? LockFactory . #kDefaults. retry . delay ,
42+ timeout : resolveDuration ( options . retry ?. timeout , null ) ,
43+ } ,
3044 logger : ( options . logger ?? noopLogger ( ) ) . child ( { pkg : 'verrou' } ) ,
3145 }
3246 }
3347
3448 /**
3549 * Create a new lock
3650 */
37- createLock ( name : string , ttl : Duration = LockFactory . #kDefaultTtl ) {
38- return new Lock ( name , this . store , this . #config, undefined , resolveDuration ( ttl ) )
51+ createLock ( name : string , ttl : Duration = LockFactory . #kDefaults . ttl ) {
52+ return new Lock ( name , this . # store, this . #config, undefined , resolveDuration ( ttl ) )
3953 }
4054
4155 /**
@@ -44,13 +58,13 @@ export class LockFactory {
4458 * that acquired it.
4559 */
4660 restoreLock ( lock : SerializedLock ) {
47- return new Lock ( lock . key , this . store , this . #config, lock . owner , lock . ttl , lock . expirationTime )
61+ return new Lock ( lock . key , this . # store, this . #config, lock . owner , lock . ttl , lock . expirationTime )
4862 }
4963
5064 /**
5165 * Disconnect the store ( if applicable )
5266 */
5367 disconnect ( ) {
54- return this . store . disconnect ( )
68+ return this . # store. disconnect ( )
5569 }
5670}
0 commit comments