'use strict' ; import { Config } from '@Base/config' ; import { SessionEntity } from '@Entities/SessionEntity' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { VKeyedCollection } from '@Tools/vTypes' ; import { GenUUID , genRandomString , IsNullOrEmpty } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; let _currentSessions ; export function initSessions ( ) { _currentSessions = new Map < string , SessionEntity > ( ) ; setInterval ( async ( ) => { const timeoutTime = new Date ( Date . now ( ) - 1000 * 60 * Config [ "metaverse-server" ] [ "session-timeout-minutes" ] ) . valueOf ( ) ; const toDelete = [ ] ; _currentSessions . forEach ( sess => { if ( sess . timeOfLastReference . valueOf ( ) < timeoutTime ) { toDelete . push ( sess ) ; } } ) ; if ( toDelete . length > 0 ) { Logger . debug ( `Session.Expiration: expired ${ toDelete . length } sessions` ) ; toDelete . forEach ( sess => { _currentSessions . delete ( sess . senderKey ) ; } ) ; } ; } , 1000 * 60 * 2 ) ; } ; export const Sessions = { createSession ( pSenderKey ? ) { const aSession = new SessionEntity ( ) ; aSession . id = GenUUID ( ) ; aSession . senderKey = pSenderKey ; aSession . whenSessionCreated = new Date ( ) ; aSession . timeOfLastReference = new Date ( ) ; return aSession ; } , getSessionWithSessionId ( pSessionId ) { _currentSessions . forEach ( sess => { if ( pSessionId === sess . id ) { return sess ; } } ) ; return null ; } , getSessionWithSenderKey ( pSenderKey ) { return _currentSessions . get ( pSenderKey ) ; } , addSession ( pSessionEntity ) { _currentSessions . set ( pSessionEntity . senderKey , pSessionEntity ) ; return pSessionEntity ; } , removeSession ( pSessionEntity ) { _currentSessions . delete ( pSessionEntity . senderKey ) ; } , touchSession ( pSession ) { pSession . countReference ++ ; pSession . timeOfLastReference = new Date ( ) ; } , clearCounts ( pSession ) { pSession . countReference = 0 ; } , * enumerate ( pFilter ? ) { for ( const sess of _currentSessions . values ( ) ) { if ( pFilter ) { if ( pFilter . criteriaTest ( sess ) ) { yield sess ; } ; } else { yield sess ; } ; } ; } , } ;