Files
cloudbeaver/webapp/packages/core-root/src/SessionActivityService.ts
T
Alexey 6ad148850d refactor: migrate to pure esm CB-5684 (#2929)
* refactor: migrate to pure esm

* chore: revert `useDefineForClassFields` tsconf

* chore: remove patch.js
2024-09-21 07:06:37 +03:00

43 lines
1.3 KiB
TypeScript

/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2024 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { ClientActivityService } from '@cloudbeaver/core-client-activity';
import { Dependency, injectable } from '@cloudbeaver/core-di';
import { SessionResource } from './SessionResource.js';
export const SESSION_TOUCH_TIME_PERIOD = 1000 * 60;
@injectable()
export class SessionActivityService extends Dependency {
private touchSessionTimer: ReturnType<typeof setTimeout> | null = null;
constructor(
private readonly clientActivityService: ClientActivityService,
private readonly sessionResource: SessionResource,
) {
super();
this.notifyClientActivity = this.notifyClientActivity.bind(this);
this.clientActivityService.onActiveStateChange.addHandler(this.notifyClientActivity);
}
private notifyClientActivity() {
if (this.touchSessionTimer || !this.clientActivityService.isActive) {
return;
}
this.sessionResource.pingSession();
this.touchSessionTimer = setTimeout(() => {
if (this.touchSessionTimer) {
clearTimeout(this.touchSessionTimer);
this.touchSessionTimer = null;
}
}, SESSION_TOUCH_TIME_PERIOD);
}
}