All files / src/libs/LifecycleManager LifecycleManager.ts

87.17% Statements 34/39
43.9% Branches 18/41
80% Functions 12/15
86.48% Lines 32/37

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  1x 1x               1x                           1x 1x 1x 1x           5x             2x             2x             2x             2x             2x     1x                                     9x   9x 6x                             9x         3x   6x                                                                                                           1x 1x 1x 1x             1x 1x 1x 1x             1x 1x 1x 1x      
/* eslint-disable jsdoc/require-description */
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { Singleton } from 'src/classes/decorators/Singleton';
import {
    ILifecycleCallback,
    ILifecycleManager,
    ILifecycleManager_,
    ILifecycleState,
    ILifecycleTime,
} from './interfaces/ILifecycleManager';
import { Register } from '../DependencyInjection/decorators/Register';
 
/**
 * Lifecycle manager class.
 * @singleton See {@link Singleton|@Singleton} for more details.
 * @remarks
 * - Register callbacks to be called when the application is initialized, loaded, or unloaded.
 * - The `registerOn` method is used to register callbacks.
 * - Call the `onInit`, `onLoad`, and `onUnload` methods when the application is initialized, loaded, or unloaded.
 * - The `LifecycleManager` class is a singleton: you get the same instance every time you call the constructor.
 */
@Register('ILifecycleManager_')
@ImplementsStatic<ILifecycleManager_>()
@Singleton
export class LifecycleManager implements ILifecycleManager {
    private _isInitPerformed = false;
    private _isLoadPerformed = false;
    private _isUnloadPerformed = false;
 
    /**
     * Gets whether the initialization has been performed.
     */
    private get isInitPerformed(): boolean {
        return this._isInitPerformed;
    }
 
    /**
     * Sets the initialization as performed.
     */
    private initPerfomed(): void {
        this._isInitPerformed = true;
    }
 
    /**
     * Gets whether the loading has been performed.
     */
    private get isLoadPerformed(): boolean {
        return this._isLoadPerformed;
    }
 
    /**
     * Sets the loading as performed.
     */
    private loadPerformed(): void {
        this._isLoadPerformed = true;
    }
 
    /**
     * Gets whether the unloading has been performed.
     */
    private get isUnloadPerformed(): boolean {
        return this._isUnloadPerformed;
    }
 
    /**
     * Sets the unloading as performed.
     */
    private unloadPerformed(): void {
        this._isUnloadPerformed = true;
    }
 
    private readonly _callbacks: {
        [key in ILifecycleState]?: {
            [key in ILifecycleTime]?: Array<ILifecycleCallback>;
        };
    } = {
        init: { before: [], on: [], after: [] },
        load: { before: [], on: [], after: [] },
        unload: { before: [], on: [], after: [] },
    };
 
    /**
     * Executes the registered callbacks for the initialization, loading, or unloading state and time.
     * @param state - The lifecycle state (init, load, unload).
     * @param time - The lifecycle time (before, on, after).
     */
    private async executeCallbacks(
        state: ILifecycleState,
        time: ILifecycleTime,
    ): Promise<void> {
        const callbacks = this._callbacks[state]?.[time] || [];
 
        for (const callback of callbacks) {
            await callback();
        }
    }
 
    /**
     * Registers a callback to be executed on a specific lifecycle time and state.
     * @param time - The lifecycle time (before, on, after).
     * @param state - The lifecycle state (init, load, unload).
     * @param callback - The callback function to be executed.
     */
    private async registerOn(
        time: ILifecycleTime,
        state: ILifecycleState,
        callback: ILifecycleCallback,
    ): Promise<void> {
        if (
            (state === 'init' && this.isInitPerformed) ||
            (state === 'load' && this.isLoadPerformed) ||
            (state === 'unload' && this.isUnloadPerformed)
        ) {
            await callback();
        } else {
            this._callbacks[state]?.[time]?.push(callback);
        }
    }
 
    /**
     * Unregisters a callback to be executed on a specific lifecycle time and state.
     * @param time  The lifecycle time (before, on, after).
     * @param state  The lifecycle state (init, load, unload).
     * @param callback  The callback function to be executed.
     */
    private registerOff(
        time: ILifecycleTime,
        state: ILifecycleState,
        callback: ILifecycleCallback,
    ): void {
        const index = this._callbacks[state]?.[time]?.indexOf(callback);
 
        Iif (index !== undefined && index !== -1) {
            this._callbacks[state]?.[time]?.splice(index, 1);
        }
    }
 
    /**
     * Registers a callback to be executed on a specific lifecycle time and state.
     * @param time - The lifecycle time (before, on, after).
     * @param state - The lifecycle state (init, load, unload).
     * @param callback - The callback function to be executed.
     */
    public static async register(
        time: ILifecycleTime,
        state: ILifecycleState,
        callback: ILifecycleCallback,
    ): Promise<void> {
        await new LifecycleManager().registerOn(time, state, callback);
    }
 
    /**
     * Unregisters a callback to be executed on a specific lifecycle time and state.
     * @param time - The lifecycle time (before, on, after).
     * @param state - The lifecycle state (init, load, unload).
     * @param callback - The callback function to be executed.
     */
    public static unregister(
        time: ILifecycleTime,
        state: ILifecycleState,
        callback: ILifecycleCallback,
    ): void {
        new LifecycleManager().registerOff(time, state, callback);
    }
 
    /**
     * Executes the registered callbacks for the initialization state.
     */
    async onInit(): Promise<void> {
        await this.executeCallbacks('init', 'before');
        await this.executeCallbacks('init', 'on');
        await this.executeCallbacks('init', 'after');
        this.initPerfomed();
    }
 
    /**
     * Executes the registered callbacks for the loading state.
     */
    async onLoad(): Promise<void> {
        await this.executeCallbacks('load', 'before');
        await this.executeCallbacks('load', 'on');
        await this.executeCallbacks('load', 'after');
        this.loadPerformed();
    }
 
    /**
     * Executes the registered callbacks for the unloading state.
     */
    async onUnload(): Promise<void> {
        await this.executeCallbacks('unload', 'before');
        await this.executeCallbacks('unload', 'on');
        await this.executeCallbacks('unload', 'after');
        this.unloadPerformed();
    }
}
 
Zur TypeDoc-Dokumentation