mmir.ConfigurationManager
A class for managing the configuration.
It's purpose is to load the configuration and settings automatically. This "class" is structured as a singleton - so that only one instance is in use.
It's purpose is to load the configuration and settings automatically. This "class" is structured as a singleton - so that only one instance is in use.
Requires
- module:mmir.require
Members
-
private_confConfigurationManagerModuleConfig
-
-
privateconfigDataObject
-
the configuration data (i.e. properties)
-
privateinstanceObject
-
Object containing the instance of the class
mmir.ConfigurationManager
. -
privatelistenersmmir.tools.EventEmitter
-
EventEmitter for change-listeners that will be notified on changes on specific configurtion paths (dot-speparated property path)
-
privateloggermmir.tools.Logger
-
Methods
-
private_emitChange(newValue, oldValue, path)
-
HELPER for emitting on-change events to listeners
Name Type Description newValue
any the new configuration value oldValue
any the old configuration value path
Array.<string> the configuration path, i.e. list of segements of dot-separated path -
private_getAsPath(propertyName)
-
"Normalizes" a string or an array into a path: the result is a single, flat array where each string has been separated at dots (i.e. each path component is a separate entry).
Name Type Description propertyName
String | Array.<String> resolves a dot-separated property-name into an array. If propertyName
is an Array, all contained String entries will be resolved, if necessaryExample
//result is ['dot', 'separated', 'list'] _getAsPath('dot.separated.list'); _getAsPath(['dot', 'separated.list']); _getAsPath(['dot', 'separated', 'list']);
-
private_loadConfigFile()
-
Helper that loads configuration file synchronously.
-
private_offChange(propertyName, listener){boolean}
-
Remove listener for configuration changes: if listener was registered multiple times, the first one is removed.
Name Type Description propertyName
String | Array.<String> optional OPTIONAL The name of the property, to listen for changes: if unspecified, listener will be removed from list of listeners for all configuration changes, otherwise it will be removed from listeners for the specified property-path. listener
function the listener function that will be invoked when a configuration value is changed: listener(newValue: any, oldValue: any, propertyName: string)
Returns:
Type Description boolean true
if a listener was removed, otherwisefalse
. -
private_onChange(propertyName, listener)
-
Register listener for configuration changes.
Name Type Description propertyName
String | Array.<String> optional OPTIONAL The name of the property, to listen for changes: if unspecified, listener will be invoked on all configuration changes. If propertyName
is an Array, it will be treated as if its entries were path-elements analogous to a dot-separated String propertyName. NOTE: dot-separated names will be resolved into object-structures, e.g.some.property
will be resolved so that thevalue
will set to:some: {property: <value>}
listener
function the listener function that will be invoked when a configuration value is changed: listener(newValue: any, oldValue: any, propertyName: string)
-
private_toPathArray(pathList)
-
"Normalizes" an array of Strings by separating each String at dots and creating one single (flat) array where each path-component is a single entry.
Name Type Description pathList
Array.<String> resolves an array with paths, i.e. dot-separated property-names into a single, flat array where each path component is a separate Strings -
addListener(propertyName, listener)
-
Register listener for configuration changes.
Name Type Description propertyName
String | Array.<String> optional OPTIONAL The name of the property, to listen for changes: if unspecified, listener will be invoked on all configuration changes. If propertyName
is an Array, it will be treated as if its entries were path-elements analogous to a dot-separated String propertyName. NOTE: dot-separated names will be resolved into object-structures, e.g.some.property
will be resolved so that thevalue
will set to:some: {property: <value>}
listener
function the listener function that will be invoked when a configuration value is changed: listener(newValue: any, oldValue: any, propertyName: string)
- See:
-
privateconstructor()
-
Constructor-Method of Singleton mmir.ConfigurationManager.
-
get(propertyName, defaultValue, useSafeAccess){any}
-
Returns the value of a property.
Name Type Description propertyName
String | Array.<String> if String: The name of the property. NOTE: If the property does not exists at the root-level, dot-separated names will be resolved into object-structures, e.g. some.property
will be resolved so that thevalue
at:some: {property: <value>}
will be returned if String array: each entry corresponds to component in a dot-separated path (see above)defaultValue
any optional OPTIONAL a default value that will be returned, in case there is no property propertyName
.useSafeAccess
Boolean optional OPTIONAL if true
, resolution of dot-separated paths will be done "safely", i.e. if a path-element does not exists, noerror
will be thrown, but instead the function will return thedefaultValue
(which will beundefined
if the argument is not given).
DEFAULT:true
NOTE: if this argument is used, paramdefaultValue
must also be given!Returns:
Type Description any The value of the property -
getBoolean(defaultValue)
-
Uses
get
. If the propertyName does not exists, returnsundefined
, otherwise values will be converted into Boolean values. Special case for Strings: the String"false"
will be converted to Boolean valuefalse
.Name Type Description defaultValue
any optional OPTIONAL if a default value is specified and there exists no property propertyName
, the specified default value will be returned. NOTE: if this argument is used,useSafeAccess
must also be given! NOTE: the default value will also be converted to a Boolean value, if necessary.- See:
-
getString(defaultValue)
-
Uses
get
. If the property does not exists, returnsundefined
, otherwise values will be converted into String values. If the value has not the type"string"
, it will be converted byJSON.stringify
.Name Type Description defaultValue
any optional OPTIONAL if a default value is specified and there exists no property propertyName
, the specified default value will be returned. NOTE: if this argument is used,useSafeAccess
must also be given! NOTE: the default value will also be converted to a String value, if necessary.- See:
-
off(propertyName, listener){boolean}
-
Remove listener for configuration changes: if listener was registered multiple times, the first one is removed.
Name Type Description propertyName
String | Array.<String> optional OPTIONAL The name of the property, to listen for changes: if unspecified, listener will be removed from list of listeners for all configuration changes, otherwise it will be removed from listeners for the specified property-path. listener
function the listener function that will be invoked when a configuration value is changed: listener(newValue: any, oldValue: any, propertyName: string)
- See:
Returns:
Type Description boolean true
if a listener was removed, otherwisefalse
. -
on(propertyName, listener)
-
Register listener for configuration changes.
Name Type Description propertyName
String | Array.<String> optional OPTIONAL The name of the property, to listen for changes: if unspecified, listener will be invoked on all configuration changes. If propertyName
is an Array, it will be treated as if its entries were path-elements analogous to a dot-separated String propertyName. NOTE: dot-separated names will be resolved into object-structures, e.g.some.property
will be resolved so that thevalue
will set to:some: {property: <value>}
listener
function the listener function that will be invoked when a configuration value is changed: listener(newValue: any, oldValue: any, propertyName: string)
- See:
-
removeListener(propertyName, listener){boolean}
-
Remove listener for configuration changes: if listener was registered multiple times, the first one is removed.
Name Type Description propertyName
String | Array.<String> optional OPTIONAL The name of the property, to listen for changes: if unspecified, listener will be removed from list of listeners for all configuration changes, otherwise it will be removed from listeners for the specified property-path. listener
function the listener function that will be invoked when a configuration value is changed: listener(newValue: any, oldValue: any, propertyName: string)
- See:
Returns:
Type Description boolean true
if a listener was removed, otherwisefalse
. -
set(propertyName, value)
-
Sets a property to a given value.
Name Type Description propertyName
String | Array.<String> The name of the property. If propertyName
is an Array, it will be treated as if its entries were path-elements analogous to a dot-separated String propertyName. NOTE: dot-separated names will be resolved into object-structures, e.g.some.property
will be resolved so that thevalue
will set to:some: {property: <value>}
value
any The value of the property Throws:
-
if the propertyName is dot-separated AND one of its path-elements (except for the last) already exists AND its type is not 'object'
- Type
- Error
-