@pawr/switcher v2.0.4
Switcher
A switcher is an object that can switch from an on
to an off
state.
It also has an availability attribute that can prevent the switcher from being switched.
It uses delegates to trigger events.
Install
npm i @pawr/switcher
Create a switcher
const { Switcher } = require("@pawr/switcher");
const mySwitcher = new Switcher({
name:"mySwitcher",
state:true,
availability:true
});
name
will identify the switcher.
state
is the initial state of the switcher.
availability
is the initial availability of the switcher.
Switch state to true
mySwitcher.on();
Switch state to false
mySwitcher.off();
Switch state to true if current state is false and vice versa
mySwitcher.switch();
Set availability to true
mySwitcher.enable();
Set availability to false
mySwitcher.disable();
Toggle availability
mySwitcher.toggle();
Check if switcher is on
mySwitcher.isOn();
Check if switcher is available
mySwitcher.isAvailable();
Delegates
A delegate stores and invokes methods as event handlers.
To learn how delegates work, see here.
onSwitch
is invoked at the end of switch(), on() and off().
onOn
is invoked at the end of on().
onOff
is invoked at the end of off().
onToggle
is invoked at the end of enable(), disable() and toggle().
onEnable
is invoked at the end of enable().
onDisable
is invoked at the end of disable().
All those delegates return the switcher itself as callback argument
SwitcherMap
A switcherMap stores switchers and can apply switch or toggle methods on all of them.
A switcherMap is a Switcher, so it has the same properties, methods and delegates
Create a switcherMap
const { SwitcherMap } = require("@pawr/switcher");
const mySwitcherMap = new SwitcherMap({
name:"mySwitcherMap",
state:true,
availability:true
});
Add a switcher in the map
mySwitcherMap.add(switcher);
The switcher will be stored in the map by its name property.
Remove a switcher in the map
mySwitcherMap.remove(switcher.name);
Set availability to true/false for all the switchers in the map
mySwitcherMap.enable/disable();
Set availability to true/false for a specific switcher in the map
mySwitcherMap.enable/disable(switcherName);
Toggle availability for all the switchers in the map
mySwitcherMap.toggle();
Toggle availability for a specific switcher in the map
mySwitcherMap.toggle(switcherName);
Delegates
onSwitcherEnable
is invoked when a switcher in the map is enabled
onSwitcherDisable
is invoked when a switcher in the map is disabled
onSwitcherToggle
is invoked when a switcher in the map is toggled
onSwitcherOn
is invoked when a switcher in the map is switched on
onSwitcherOff
is invoked when a switcher in the map is switched off
onSwitcherSwitch
is invoked when a switcher in the map is switched
All those delegates return the switcher that is enabled/disabled/toggled as callback argument
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago