An EnvironmentSource is a service from which to fetch environment variables into a browser synchronously or asynchronously. It can be a constant, a file, a browser storage, a property server, a WebSocket stream or any other type of source that the browser has access to.
An application can have as many sources as it needs, and they all have access to the EnvironmentState to build calls, wait for specific properties or other sources, etc. How these sources are resolved or how they add the properties to the environment can be defined by the source properties.
The EnvironmentSource is an interface that must be implemented to obtain environment properties.
This property marks the properties loaded by the EnvironmentSource as required. So must be loaded before the application load and should stop the load if there is an error.
constsource1 = { isRequired:true, load: () =>of({ a:0 }).pipe(delay(10)) }; constsource2 = { isRequired:true, load: () => interval(10).pipe( map((v) => ({ b:v })), take(3) ) }; loader.load(); // resolves at 30ms // sets the source1 properties at 10ms // sets the source2 properties at 10ms, 20ms & 30ms
Resolves immedialely if there is no required sources.
constsource1 = { load: () =>of({ a:0 }).pipe(delay(10)) }; loader.load(); // resolves immedialely // sets the source1 properties at 10ms
Never resolves if a required source doesn't complete.
constsource1 = { isRequired:true, load: () =>interval(10).pipe(map((v) => ({ a:v }))) }; loader.load(); // will never resolve // sets the source1 properties every 10ms
Sometimes is needed to provide a fallback source if the first one fails. This can be done easily in the original source with the catch method or the catchError operator function. This condition can be chained as many times as necessary.
Environment Source
An EnvironmentSource is a service from which to fetch environment variables into a browser synchronously or asynchronously. It can be a constant, a file, a browser storage, a property server, a WebSocket stream or any other type of source that the browser has access to.
An application can have as many sources as it needs, and they all have access to the EnvironmentState to build calls, wait for specific properties or other sources, etc. How these sources are resolved or how they add the properties to the environment can be defined by the source properties.
The EnvironmentSource is an interface that must be implemented to obtain environment properties.
Use cases
Below are examples of the expected behavior with the default environment loader implementation.
Table of Contents
isRequired
This property marks the properties loaded by the EnvironmentSource as required. So must be loaded before the application load and should stop the load if there is an error.
Resolves immedialely if there is no required sources.
Never resolves if a required source doesn't complete.
Rejects after a required source error.
Resolves after a no required source error.
isOrdered
Forces the checked properties to load in order, waiting for one to finish loading before starting the next.
Unordered sources add all properties at once.
Never loads if previous ordered source doesn't complete.
Ignore errors and continues with the next ordered source.
ignoreError
Ignores required source errors so that you can continue to load the application.
path
The EnvironmentState path in which to save the properties of this source.
load()
The method that gets the sources.
mapFn()
A function that's executed before
EnvironmentLoader#preAddProperties()
that allows to modify the loaded properties.errorHandler()
Returns an EnvironmentState on error.
Execute aside code and return the same error.
Return a custom error.
Fallback sources
Sometimes is needed to provide a fallback source if the first one fails. This can be done easily in the original source with the
catch
method or thecatchError
operator function. This condition can be chained as many times as necessary.catch
withPromise
.catchError
withObservable
.Use values from other sources
Errors