Switches
Switches is a distributed, eventually consistent, circuit breaker type system for applications. It enables you to turn features on and off while they're running without having to take them completely offline or do a complete deploy
What follows is a novel example where two Switches
instances are
synchronized via streams. Seeing how synchronization is done via streams
the same synchronization interface can be used to synchronize the instances
over TCP, HTTP, Websockets or any other transport with a streaming interface
var Switches = ;var defaults = ; // create two isolated switches instances with sane defaultsvar switches1 = defaults;var switches2 = defaults; // connect the two instances together via streamsvar s1 = switches1;var s2 = switches2;s1; // convenience print status functionvar { var status = switches ? "enabled" : "disabled"; console;} // check if registration is enabled in `switches2`; // disable registration in `switches1`switches1; process;
API
Switches
The Switches
class is what is directly exported by this library and is
responsible for maintaining and synchronizing the state of the switches
Switches::constructor(@defaults = {})
The constructor is responsible for reveiving an object of the sane
defaults for all the switches and setting up the initial state of
the switches. The @defaults
object should be a flat object where all
values are either boolean true to represent enabled or boolean false to
represent a disabled.
- @param {Object} defaults - The sane defaults object
Switches::enable(property)
This function is used to enable a parameter, causing remote synchronization and
subsequent calls to Switches::isEnabled()
with the same property name to
return boolean true. If the supplied property
parameter is not found in
the @defaults
map, it is considered to not exist, so no property will become
enabled and this function will return boolean false for failure
- @param {String} property - The property to enable
- @returns {Boolean} - True on success false on failure
Switches::disable(property)
This function is used to disable a parameter, causing remote synchronization and
subsequent calls to Switches::isEnabled()
with the same property name to
return boolean false. If the supplied property
parameter is not found in
the @defaults
map, it is considered to not exist, so no property will become
enabled and this function will return boolean false for failure
Switches::isEnabled(property)
This function returns boolean true if the supplied property
parameter is
enabled, boolean false otherwise. Also, if the property
parameter can not
be found in the @defaults
map, it is considered to not exist so false is
assumed
- @param {String} property - The property to check
- @returns {Boolean} - True on enabled false on disabled or missing
Switches::exists(property)
This function is used to determine the existence of a propety
. It returns
boolean true on existence, boolean false otherwise
- @param {String} property - The property to check
- @returns {Boolean} - True on existence false on non-existence
Switches::createStream()
This function returns a duplex stream which can be used to sync this Switches
instance with remote Switches
instances in an eventually consistent manner
- @returns {Stream} - The duplex stream to this object
Events 'update', (@self)
This event gets emitted from a Switches
instance every time a property value
is changed remotely or locally. The only argument passed to event handlers is
the Switches
instance itself