All files / tokens window.token.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 1/1
100% Lines 5/5

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 262x 2x                                 2x     16x 16x      
import { isPlatformBrowser } from '@angular/common';
import { InjectionToken, inject, PLATFORM_ID } from '@angular/core';
 
/**
 * Injection token for the browser's Window object.
 *
 * Returns the actual window object in browser environment,
 * or undefined in SSR/Node.js environment.
 *
 * @example
 * ```typescript
 * private readonly window = inject(WINDOW);
 *
 * if (this.window) {
 *     this.window.addEventListener('storage', handler);
 * }
 * ```
 */
export const WINDOW = new InjectionToken<Window | undefined>('WindowToken', {
    providedIn: 'root',
    factory: () => {
        const platformId = inject(PLATFORM_ID);
        return isPlatformBrowser(platformId) ? window : undefined;
    },
});