/ login-server / source / cache / AccountUpdateManager.ts
AccountUpdateManager.ts
 1  import { L2AccountsAccessTime } from '../database/interface/AccountsTableApi'
 2  import { DatabaseManager } from '../database/manager'
 3  import _, { DebouncedFunc } from 'lodash'
 4  
 5  class Manager {
 6      lastAccessTimes: Map<string, number> = new Map<string, number>()
 7      debounceLastAccessTimes: DebouncedFunc<any>
 8  
 9      constructor() {
10          this.debounceLastAccessTimes = _.debounce( this.runUpdateLastAccessTimes.bind( this ), 30000, {
11              maxWait: 5000
12          } )
13      }
14      refreshLastAccess( accountName: string ) : void {
15          this.lastAccessTimes.set( accountName, Date.now() )
16          this.debounceLastAccessTimes()
17      }
18  
19      runUpdateLastAccessTimes() : Promise<void> {
20          let records : Array<L2AccountsAccessTime> = []
21          this.lastAccessTimes.forEach( ( time: number, name: string ) : void => {
22              records.push( {
23                  name,
24                  time
25              } )
26          } )
27  
28          if ( records.length === 0 ) {
29              return
30          }
31  
32          this.lastAccessTimes.clear()
33          return DatabaseManager.getAccounts().updateLastAccess( records )
34      }
35  }
36  
37  export const AccountUpdateManager = new Manager()