2.0.4 • Published 4 years ago

@pawr/switcher v2.0.4

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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

2.0.3

4 years ago

2.0.2

4 years ago

2.0.4

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.14

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago