Description

Provides quick access to platform information.

Index

universal/src/services/universal.service.ts

Properties
Methods

Constructor

constructor(platformId: any)

Creates an instance of UniversalService.

Parameters:
Name Type Optional Description
platformId any No

The id of the current platform. This always equals to PLATFORM_ID.

Methods

Public onBrowser
onBrowser(execute: () => void)
Type parameters:
  • T

Executes the specified function only on browser platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on browser platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onNonBrowser
onNonBrowser(execute: () => void)
Type parameters:
  • T

Executes the specified function only on browser platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on browser platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onNonServer
onNonServer(execute: () => void)
Type parameters:
  • T

Executes the specified function only on server platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on server platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onNonWorkerApp
onNonWorkerApp(execute: () => void)
Type parameters:
  • T

Executes the specified function only on worker app platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on worker app platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onNonWorkerUi
onNonWorkerUi(execute: () => void)
Type parameters:
  • T

Executes the specified function only on worker UI platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on worker UI platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onServer
onServer(execute: () => void)
Type parameters:
  • T

Executes the specified function only on server platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on server platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onWorkerApp
onWorkerApp(execute: () => void)
Type parameters:
  • T

Executes the specified function only on worker app platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on worker app platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Public onWorkerUi
onWorkerUi(execute: () => void)
Type parameters:
  • T

Executes the specified function only on worker UI platfroms.

Parameters:
Name Type Optional Description
execute function No

The function to execute only on worker UI platforms.

Returns: T | undefined

The value returned by the execute() function. If the funtion was not executed, this will be undefined.

Properties

Public Readonly isPlatformBrowser
Type: boolean

true if the app is currently running on a browser; otherwise false.

Public Readonly isPlatformServer
Type: boolean

true if the app is currently running on a server; otherwise false.

Public Readonly isPlatformWorkerApp
Type: boolean

true if the app is currently running on a worker app; otherwise false.

Public Readonly isPlatformWorkerUi
Type: boolean

true if the app is currently running on a worker UI; otherwise false.

Public Readonly platformId
Type: any
Decorators:
@Inject(PLATFORM_ID)
The id of the current platform. This always equals to `PLATFORM_ID`.
import { Injectable, PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser, isPlatformWorkerApp, isPlatformWorkerUi, isPlatformServer } from '@angular/common';

/**
 * Provides quick access to platform information.
 *
 * @export
 * @class UniversalService
 */
@Injectable({
    providedIn: 'root'
})
export class UniversalService
{
    /**
     * `true` if the app is currently running on a browser; otherwise `false`.
     */
    public readonly isPlatformBrowser: boolean;
     /**
     * `true` if the app is currently running on a server; otherwise `false`.
     */
    public readonly isPlatformServer: boolean;
     /**
     * `true` if the app is currently running on a worker app; otherwise `false`.
     */
    public readonly isPlatformWorkerApp: boolean;
     /**
     * `true` if the app is currently running on a worker UI; otherwise `false`.
     */
    public readonly isPlatformWorkerUi: boolean;

    /**
     * Creates an instance of UniversalService.
     * 
     * @param {*} platformId The id of the current platform. This always equals to `PLATFORM_ID`.
     */
    constructor(@Inject(PLATFORM_ID) public readonly platformId: any)
    {
        this.isPlatformBrowser   = isPlatformBrowser(this.platformId);
        this.isPlatformServer    = isPlatformServer(this.platformId);
        this.isPlatformWorkerApp = isPlatformWorkerApp(this.platformId);
        this.isPlatformWorkerUi  = isPlatformWorkerUi(this.platformId);
    }

    /**
     * Executes the specified function only on browser platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on browser platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    public onBrowser<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(this.isPlatformBrowser, execute);
    }
    
    /**
     * Executes the specified function only on server platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on server platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    public onServer<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(this.isPlatformServer, execute);
    }
    
    /**
     * Executes the specified function only on worker app platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on worker app platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    
    public onWorkerApp<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(this.isPlatformWorkerApp, execute);
    }
        
    /**
     * Executes the specified function only on worker UI platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on worker UI platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    public onWorkerUi<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(this.isPlatformWorkerUi, execute);
    }

    /**
     * Executes the specified function only on browser platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on browser platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    public onNonBrowser<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(!this.isPlatformBrowser, execute);
    }
    
    /**
     * Executes the specified function only on server platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on server platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    public onNonServer<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(!this.isPlatformServer, execute);
    }
    
    /**
     * Executes the specified function only on worker app platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on worker app platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    
    public onNonWorkerApp<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(!this.isPlatformWorkerApp, execute);
    }
        
    /**
     * Executes the specified function only on worker UI platfroms.
     *
     * @template T The type of value returned by the `execute()` function.
     * @param {() => T} execute The function to execute only on worker UI platforms.
     * @returns {T} The value returned by the `execute()` function. If the funtion was not executed, this will be `undefined`.
     */
    public onNonWorkerUi<T>(execute: () => T): T | undefined
    {
        return this.onPlatform(!this.isPlatformWorkerUi, execute);
    }
    
    private onPlatform<T>(isPlatform: boolean, execute: () => T): T | undefined
    {
        return isPlatform ? execute() : undefined;
    }
}

results matching ""

    No results matching ""