import { VaultSourceID } from "buttercup" ; import { getAllSourceIDs , lockVault , onVaultSourcesUpdated } from "./buttercup" ; import { registerActivityCallback } from "./activity" ; import { getEmitter as getConfigEmitter , getVaultConfig } from "./config" ; import { CURRENT_SOURCE } from "../state/vault" ; import { navigateBackToRoot } from "../state/navigation" ; let __listeners = { } ; export function initialise ( ) { onVaultSourcesUpdated ( ( ) => { updateVaultConfigurations ( ) ; } ) ; registerActivityCallback ( ( inactivity ) => { for ( const sourceID in __listeners ) { __listeners [ sourceID ] ( inactivity ) . catch ( err => { console . error ( err ) ; } ) ; } } ) ; const configEmitter = getConfigEmitter ( ) ; configEmitter . on ( "update" , ( sourceID ) => { updateListenerForSource ( sourceID ) ; } ) ; updateVaultConfigurations ( ) ; } function updateVaultConfigurations ( ) { const sourceIDs = getAllSourceIDs ( ) ; for ( const sourceID of sourceIDs ) { updateListenerForSource ( sourceID ) ; } } function updateListenerForSource ( sourceID ) { const config = getVaultConfig ( sourceID ) ; if ( config ?. autoLockEnabled ) { __listeners [ sourceID ] = async ( inactivity ) => { if ( inactivity >= config . autoLockTime ) { if ( CURRENT_SOURCE . get ( ) === sourceID ) { navigateBackToRoot ( ) ; } await lockVault ( sourceID ) ; } } ; } else { delete __listeners [ sourceID ] ; } }