File

packages/core/src/lib/services/loader/eui-loader.service.ts

Description

A service for loading external dependencies dynamically to the DOM and keeping track of their status. Dependencies can be loaded in any order and can have prerequisites. The service will load the prerequisites first and then load the dependency. If a dependency fails to load, the service will retry loading it according to the retry policy. The service will emit the status of the dependency when it changes. This is useful for showing a loading indicator while the dependency is loading. In scenarios where the dependency is not required for the application to function, the service will not block the application from loading if the dependency fails to load. The service will also remove the dependency from the DOM if it is removed from the list. This is useful for scenarios where the dependency is only required for a specific component, might be removed from the list when the component is destroyed, and it will not affect the bundle size.

Example :
 // Add a dependency
 loader.addDependency('Quill', 'https://cdn.quilljs.com/1.3.6/quill.js', 'js');
 // Get the status of a dependency
 loader.getDependencyStatus('Quill');
 // Listen for status changes
 loader.statusChanges('Quill').subscribe((status: Status) => {
   console.log(status);
 });
 // Remove a dependency
 loader.removeDependency('Quill');
 // Set the retry policy
 loader.setRetryPolicy(3, 1000);
 // Set the timeout policy
 loader.setTimeoutPolicy(5000);
 // Load a dependency with prerequisites
 loader.addDependency('Quill', 'https://cdn.quilljs.com/1.3.6/quill.js', 'js', ['QuillStyle']);
 loader.addDependency('QuillStyle', 'https://cdn.quilljs.com/1.3.6/quill.snow.css', 'css');
 // Load a dependency with prerequisites and retry logic
 loader.setRetryPolicy(3, 1000);
 loader.setTimeoutPolicy(5000);
 loader.addDependency('Quill', 'https://cdn.quilljs.com/1.3.6/quill.js', 'js', ['QuillStyle']);
 loader.addDependency('QuillStyle', 'https://cdn.quilljs.com/1.3.6/quill.snow.css', 'css');

Index

Methods

Methods

addDependency
addDependency(name: string, url: string, type: "js" | "css", requires: string[])

Add a dependency with its type (JS or CSS) and dependencies

Parameters :
Name Type Optional Default value Description
name string No

The name of the dependency

url string No

The URL of the dependency

type "js" | "css" No

The type of the dependency (JS or CSS)

requires string[] No []

The dependencies that must be loaded before this one (optional)

Returns : void
getDependencyStatus
Prefer using statusChanges instead
getDependencyStatus(name: string)

Get the status of a dependency

See statusChanges

Parameters :
Name Type Optional Description
name string No

The name of the dependency

Returns : Status | null

The status of the dependency

removeDependency
removeDependency(name: string)

Remove a dependency from the DOM

Parameters :
Name Type Optional Description
name string No

The name of the dependency

Returns : void
setRetryPolicy
setRetryPolicy(maxAttempts: number, retryInterval: number)

Allow users to configure the retry policy

Parameters :
Name Type Optional Description
maxAttempts number No

The maximum number of retry attempts

retryInterval number No

The time between retries in milliseconds

Returns : void
setTimeoutPolicy
setTimeoutPolicy(time: number)

Allow users to configure the timeout policy

See setRetryPolicy See statusChanges

Parameters :
Name Type Optional Description
time number No

The time in milliseconds before considering a load attempt as failed

Returns : void
statusChanges
statusChanges(name: string)

Get the status of a dependency as an observable

Parameters :
Name Type Optional Description
name string No

The name of the dependency

Returns : Observable<Status>

results matching ""

    No results matching ""