All files / modules state.ts

91.11% Statements 41/45
66.66% Branches 16/24
85.71% Functions 12/14
90.69% Lines 39/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2198x   8x                                                                 8x           18x             2x       2x             18x 18x           18x           18x   18x                   1x               4x 4x                   3x 3x 3x                 4x 4x                   3x 3x 3x               18x                                           18x           18x   18x               9x       9x 7x 5x     7x               18x           18x   18x               9x       9x 7x 5x     7x      
import { Injectable } from '@angular/core';
 
import { Subject } from 'rxjs';
 
/**
 * @typedef UserMode
 * @description Represents the different access modes available to a user.
 * - GUEST: Guest access (anonymous, via QR code)
 * - LOGIN: Login access (via username and password)
 */
export type UserMode = 'GUEST' | 'LOGIN';
 
/**
 * @typedef UserSession
 * @description Represents the user's session information.
 * @property {string | null} session - The unique identifier for the user's session, or `null` if no session is active.
 * @property {string | null} device - The unique identifier for the user's device, or `null` if no device is set.
 */
export type UserSession = {
    session: string | null,
    device: string | null
};
 
/************************************/
/* STATE SERVICE                     */
/************************************/
 
/**
 * @class stateService
 * @description Service for managing the user's access mode and session expiration status.
 * It provides mechanisms to set and get the current user mode and notify when the session has expired.
 */
@Injectable({
    providedIn: 'root'
})
export class stateService {
 
    /******************************/
    /* ACCESS MODE                */
    /******************************/
 
    private _access: UserMode | null = null;
 
    /**
     * @description Gets or sets the current user access mode.
     * @type {UserMode | null}
     */    
    get Access(): UserMode | null {
        return this._access
    }
 
    set Access(value: UserMode){
        this._access = value;
    }
 
    /******************************/
    /* SESSION CONTROL            */
    /******************************/
 
    private _onDevice = new Subject<void>();
    private _onSession = new Subject<void>();
 
    /**
     * @description Observable that emits when the device ID is updated in the user session.
     * @type {Observable<void>}
     */    
    public OnDevice = this._onDevice.asObservable();
        
    /**
     * @description Observable that emits when the session ID is updated in the user session.
     * @type {Observable<void>}
     */
    public OnSession = this._onSession.asObservable();
 
    private _userSession: UserSession = {
        device: null,
        session: null
    };
 
    /**
     * @description Gets or sets the current user session information.
     * @type {UserSession | null}
     */    
    get userSession() : UserSession | null {
        return this._userSession;
    }
 
    /**
     * @description Retrieves the session ID from the current user session.
     * @returns {string | null} The session ID, or `null` if no session is set.
     */    
    get session(): string | null {
        if (this._userSession){
            return encodeURIComponent(this._userSession.session ?? '');
        }
        return null;
    }
 
    /**
     * @description Sets a new session ID. Triggers the session update event if the value changes.
     * @param {string} value - The new session ID.
     */    
    set session(value: string | null){
        if (this.session != value){
            this._userSession.session = value;
            this._onSession.next();
        }
    }
 
    /**
     * @description Retrieves the device ID from the current user session.
     * @returns {string | null} The device ID, or `null` if no session is set.
     */    
    get device(): string | null {
        if (this._userSession){
            return encodeURIComponent(this._userSession.device ?? '');
        }
        return null;
    }
 
    /**
     * @description Sets a new device ID. Triggers the device update event if the value changes.
     * @param {string} value - The new device ID.
     */    
    set device(value: string){
        if (this.device != value){
            this._userSession.device = value;
            this._onDevice.next();
        }
    }
 
    /******************************/
    /* PLACE (for URL params)     */
    /******************************/
 
    private _place: string | null = null;
 
    /**
     * @description Retrieves the current place objid (chosen after login or from QR scan). Used by adhocService for URL parameters.
     * @returns {string | null} The place objid, or `null` if no place is set.
     */
    get place(): string | null {
        return encodeURIComponent(this._place ?? '');
    }
 
    /**
     * @description Sets the current place objid.
     * @param {string | null} value - The place objid, or `null` to clear.
     */
    set place(value: string | null){
        this._place = value ?? null;
    }
 
    /*********************************/
    /* SESSION EXPIRATION EVENT      */
    /*********************************/
 
    private _onExpired = new Subject<void>();
 
    /**
     * @description Observable that emits when the session expires, allowing subscribers to react to expiration events.
     * @type {Observable<void>}
     */    
    public OnExpired = this._onExpired.asObservable();
 
    private _isexpired = false;
 
    /**
     * @description Gets or sets the expiration status of the session. 
     * If set to `true`, it emits an expiration event.
     * @type {boolean}
     */    
    get IsExpired(): boolean{
        return this._isexpired;
    }
 
    set IsExpired(value: boolean){
        if (this._isexpired !== value){
            if (value){
                this._onExpired.next();
            }
            
            this._isexpired = value;
        }
    }
 
    /*********************************/
    /* LOADING READY EVENT           */
    /*********************************/
 
    private _onReady = new Subject<void>();
 
    /**
     * @description Observable that emits when the system is ready, allowing subscribers to react when everything is loaded.
     * @type {Observable<void>}
     */    
    public OnReady = this._onReady.asObservable();
 
    private _isready = false;
 
    /**
     * @description Gets or sets the ready status of the session. 
     * If set to `true`, it emits a ready event.
     * @type {boolean}
     */    
    get IsReady(): boolean{
        return this._isready;
    }
 
    set IsReady(value: boolean){
        if (this._isready !== value){
            if (value){
                this._onReady.next();
            }
            
            this._isready = value;
        }
    }
}