import { makeObservable , observable } from 'mobx' ; import { injectable } from '@cloudbeaver/core-di' ; import { LocalStorageSaveService } from '@cloudbeaver/core-settings' ; import { UserInfoResource } from './UserInfoResource' ; @ injectable ( ) export class UserDataService { private userData : Map < string , Record < string , any > > ; constructor ( private readonly userInfoResource , private autoSaveService ) { this . userData = new Map ( ) ; makeObservable < this , 'userData' > ( this , { userData : observable , } ) ; this . autoSaveService . withAutoSave ( this . userData , 'user_data' ) ; } getUserData < T > ( key , defaultValue , validate ? ) { const userId = this . userInfoResource . getId ( ) ; if ( ! this . userData . has ( userId ) ) { this . userData . set ( userId , observable ( { } ) ) ; } const data = this . userData . get ( userId ) ! ; if ( key in data ) { if ( ! validate || validate ( data [ key ] ) ) { return data [ key ] ; } } data [ key ] = defaultValue ( ) ; return data [ key ] ; } }