vulcain-corejs
Version:
Vulcain micro-service framework
100 lines (98 loc) • 3.57 kB
JavaScript
const dynamicProperties_1 = require("./properties/dynamicProperties");
/**
*
* Provides dynamic properties updated when config change.
* Accessing a dynamic property is very fast. The last value is cached and updated on the fly
* from a <b>ConfigurationSource</b> at fixed interval.
* Updates are made using polling requests on a list of sources.
* <p>
* Dynamic properties are read only. You can set a value but it will be valid only as a default value.
* </p>
* <code>
* DynamicConfiguration.init().addRest("http....").startPollingAsync();
* let i:number = DynamicConfiguration.getProperty("prop1");
* let i2:number = DynamicConfiguration.getOrDefaultProperty("prop1", 1);
* </code>
*/
class DynamicConfiguration {
/**
* subscribe on a property changed
*/
static onPropertyChanged(handler, propertyName) {
if (propertyName) {
let prop = dynamicProperties_1.DynamicProperties.instance.getProperty(propertyName);
if (!prop)
throw new Error("Property not found : " + propertyName);
prop.propertyChanged.subscribe(handler);
}
else
dynamicProperties_1.DynamicProperties.instance.propertyChanged.subscribe(handler);
}
/**
* Create a chained property
*
*/
static asChainedProperty(defaultValue, name, ...fallbackPropertyNames) {
return dynamicProperties_1.DynamicProperties.factory.asChainedProperty(defaultValue, name, fallbackPropertyNames);
}
/**
* Create a new property
*/
static asProperty(value, name, onPropertyChanged) {
let prop = dynamicProperties_1.DynamicProperties.factory.asProperty(value, name);
if (onPropertyChanged)
prop.propertyChanged.subscribe(onPropertyChanged);
return prop;
}
/**
* Get a property value by name
*
* @static
* @template T
* @param {string} name
* @returns
*
* @memberOf DynamicConfiguration
*/
static get(name) {
let p = dynamicProperties_1.DynamicProperties.instance.getProperty(name);
return p && p.value;
}
/**
* Get a dynamic property
*/
static getProperty(name) {
return dynamicProperties_1.DynamicProperties.instance.getProperty(name);
}
/**
* Get or create a dynamic property
* defaultValue can be a value or a factory
*/
static getOrCreateProperty(name, defaultValue) {
return dynamicProperties_1.DynamicProperties.instance.getOrCreateProperty(name, defaultValue);
}
/**
* Update a property value or create a new one if not exists
*/
static setOrCreateProperty(name, defaultValue) {
return dynamicProperties_1.DynamicProperties.instance.createOrUpdateProperty(name, defaultValue);
}
/**
* Init polling informations. This function can be call only once before any use of a dynamic property.
*/
static init(pollingIntervalInSeconds, sourceTimeoutInMs) {
return dynamicProperties_1.DynamicProperties.init(pollingIntervalInSeconds, sourceTimeoutInMs);
}
static reset(pollingIntervalInSeconds, sourceTimeoutInMs) {
return dynamicProperties_1.DynamicProperties.instance.reset(pollingIntervalInSeconds, sourceTimeoutInMs);
}
/**
* Get the underlying dynamic properties manager instance
*/
static get instance() {
return dynamicProperties_1.DynamicProperties.instance;
}
}
exports.DynamicConfiguration = DynamicConfiguration;
//# sourceMappingURL=dynamicConfiguration.js.map
;