packages/core/src/lib/services/loader/eui-loader.service.ts
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');
Methods |
| addDependency | |||||||||||||||||||||||||
addDependency(name: string, url: string, type: "js" | "css", requires: string[])
|
|||||||||||||||||||||||||
|
Add a dependency with its type (JS or CSS) and dependencies
Parameters :
Returns :
void
|
| getDependencyStatus | ||||||||
| Prefer using statusChanges instead | ||||||||
getDependencyStatus(name: string)
|
||||||||
|
Get the status of a dependency See statusChanges
Parameters :
Returns :
Status | null
The status of the dependency |
| removeDependency | ||||||||
removeDependency(name: string)
|
||||||||
|
Remove a dependency from the DOM
Parameters :
Returns :
void
|
| setRetryPolicy | ||||||||||||
setRetryPolicy(maxAttempts: number, retryInterval: number)
|
||||||||||||
|
Allow users to configure the retry policy
Parameters :
Returns :
void
|
| setTimeoutPolicy | ||||||||
setTimeoutPolicy(time: number)
|
||||||||
|
Allow users to configure the timeout policy See setRetryPolicy See statusChanges
Parameters :
Returns :
void
|
| statusChanges | ||||||||
statusChanges(name: string)
|
||||||||
|
Get the status of a dependency as an observable
Parameters :
Returns :
Observable<Status>
|