@@ -82,6 +82,12 @@ pub struct DeviceLock {
8282}
8383
8484impl DeviceLock {
85+ pub fn remaining_secs ( & self ) -> u64 {
86+ let now = now ( ) . as_secs ( ) ;
87+ let diff = now. saturating_sub ( self . time as u64 ) ;
88+ ( DEVICE_LOCK_INTERVAL_SECS * 2 ) . saturating_sub ( diff)
89+ }
90+
8591 /// Check if the device is locked
8692 /// This is determined if the time is less than 2 minutes ago
8793 pub fn is_locked ( & self , id : & str ) -> bool {
@@ -385,7 +391,7 @@ pub trait MutinyStorage: Clone + Sized + 'static {
385391 self . get_data ( DEVICE_LOCK_KEY )
386392 }
387393
388- fn set_device_lock ( & self ) -> Result < ( ) , MutinyError > {
394+ async fn set_device_lock ( & self ) -> Result < ( ) , MutinyError > {
389395 let device = self . get_device_id ( ) ?;
390396 if let Some ( lock) = self . get_device_lock ( ) ? {
391397 if lock. is_locked ( & device) {
@@ -395,7 +401,7 @@ pub trait MutinyStorage: Clone + Sized + 'static {
395401
396402 let time = now ( ) . as_secs ( ) as u32 ;
397403 let lock = DeviceLock { time, device } ;
398- self . set_data ( DEVICE_LOCK_KEY , lock, Some ( time) )
404+ self . set_data_async ( DEVICE_LOCK_KEY , lock, Some ( time) ) . await
399405 }
400406
401407 async fn fetch_device_lock ( & self ) -> Result < Option < DeviceLock > , MutinyError > ;
@@ -714,7 +720,7 @@ mod tests {
714720 let lock = storage. get_device_lock ( ) . unwrap ( ) ;
715721 assert_eq ! ( None , lock) ;
716722
717- storage. set_device_lock ( ) . unwrap ( ) ;
723+ storage. set_device_lock ( ) . await . unwrap ( ) ;
718724 // sleep 1 second to make sure it writes to VSS
719725 sleep ( 1_000 ) . await ;
720726
@@ -725,7 +731,7 @@ mod tests {
725731 assert_eq ! ( lock. unwrap( ) . device, id) ;
726732
727733 // make sure we can set lock again, should work because same device id
728- storage. set_device_lock ( ) . unwrap ( ) ;
734+ storage. set_device_lock ( ) . await . unwrap ( ) ;
729735 // sleep 1 second to make sure it writes to VSS
730736 sleep ( 1_000 ) . await ;
731737
@@ -744,6 +750,9 @@ mod tests {
744750 assert ! ( lock. clone( ) . unwrap( ) . is_locked( & new_id) ) ;
745751 assert_eq ! ( lock. unwrap( ) . device, id) ;
746752
747- assert ! ( storage. set_device_lock( ) . is_err( ) )
753+ assert_eq ! (
754+ storage. set_device_lock( ) . await ,
755+ Err ( crate :: MutinyError :: AlreadyRunning )
756+ ) ;
748757 }
749758}
0 commit comments