Commit 4645088e authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate SessionService to TS

parent 8e30765f
import type { Profile } from '../../types/api';
import type { SidebarSettings } from '../../types/config';
import { serviceConfig } from '../config/service-config'; import { serviceConfig } from '../config/service-config';
import type { SidebarStore } from '../store';
import { retryPromiseOperation } from '../util/retry'; import { retryPromiseOperation } from '../util/retry';
import * as sentry from '../util/sentry'; import * as sentry from '../util/sentry';
import type { APIService } from './api';
import type { AuthService } from './auth';
import type { ToastMessengerService } from './toast-messenger';
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
/** @typedef {import('../../types/api').Profile} Profile */
/** /**
* This service handles fetching the user's profile, updating profile settings * This service handles fetching the user's profile, updating profile settings
* and logging out. * and logging out.
...@@ -13,25 +17,28 @@ const CACHE_TTL = 5 * 60 * 1000; // 5 minutes ...@@ -13,25 +17,28 @@ const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
* @inject * @inject
*/ */
export class SessionService { export class SessionService {
/** private _store: SidebarStore;
* @param {import('../store').SidebarStore} store private _api: APIService;
* @param {import('./api').APIService} api private _auth: AuthService;
* @param {import('./auth').AuthService} auth private _toastMessenger: ToastMessengerService;
* @param {import('../../types/config').SidebarSettings} settings private _authority: string | null;
* @param {import('./toast-messenger').ToastMessengerService} toastMessenger private _lastLoad: Promise<Profile> | null;
*/ private _lastLoadTime: number | null;
constructor(store, api, auth, settings, toastMessenger) {
constructor(
store: SidebarStore,
api: APIService,
auth: AuthService,
settings: SidebarSettings,
toastMessenger: ToastMessengerService
) {
this._api = api; this._api = api;
this._auth = auth; this._auth = auth;
this._store = store; this._store = store;
this._toastMessenger = toastMessenger; this._toastMessenger = toastMessenger;
this._authority = serviceConfig(settings)?.authority ?? null; this._authority = serviceConfig(settings)?.authority ?? null;
/** @type {Promise<Profile>|null} */
this._lastLoad = null; this._lastLoad = null;
/** @type {number|null} */
this._lastLoadTime = null; this._lastLoadTime = null;
// Re-fetch profile when user logs in or out in another tab. // Re-fetch profile when user logs in or out in another tab.
...@@ -44,9 +51,9 @@ export class SessionService { ...@@ -44,9 +51,9 @@ export class SessionService {
* If the profile has been previously fetched within `CACHE_TTL` ms, then this * If the profile has been previously fetched within `CACHE_TTL` ms, then this
* method returns a cached profile instead of triggering another fetch. * method returns a cached profile instead of triggering another fetch.
* *
* @return {Promise<Profile>} A promise for the user's profile data. * @return A promise for the user's profile data.
*/ */
load() { load(): Promise<Profile> {
if ( if (
!this._lastLoad || !this._lastLoad ||
!this._lastLoadTime || !this._lastLoadTime ||
...@@ -93,10 +100,9 @@ export class SessionService { ...@@ -93,10 +100,9 @@ export class SessionService {
* This method can be used to update the profile data in the client when new * This method can be used to update the profile data in the client when new
* data is pushed from the server via the real-time API. * data is pushed from the server via the real-time API.
* *
* @param {Profile} profile * @return The updated profile data
* @return {Profile} The updated profile data
*/ */
update(profile) { update(profile: Profile): Profile {
const prevProfile = this._store.profile(); const prevProfile = this._store.profile();
const userChanged = profile.userid !== prevProfile.userid; const userChanged = profile.userid !== prevProfile.userid;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment