memo-switch
v0.1.3
Published
A switch that memorizes state transition events.
Downloads
5
Maintainers
Readme
memo-switch
A switch that memorizes state transition events.
- The switch memorizes all the state transition events.
- From the events, we get state of any time point.
Install
npm install memo-switch
Usage
const MemoSwitch = require('memo-switch')
const memoSwitch = MemoSwitch.initialize()
console.log(memoSwitch.isActivated) // false
memoSwitch.toggle()
console.log(memoSwitch.isActivated) // true
for babel|flow users
// @flow
import MemoSwitch from 'memo-switch/jsnext'
This way, we can import the module and their type.
Get state of a time point
const memoSwitch = MemoSwitch.initialize()
const timestamp = new Date().getTime()
console.log(memoSwitch.isActivatedAt(timestamp)) // true
const minuteAgo = new Date().getTime() - 360000
console.log(memoSwitch.isActivatedAt(minuteAgo)) // false
Default state
Set default state to the 1st argument of MemoSwitch.initialize()
.
const memoSwitch = MemoSwitch.initialize(true)
console.log(memoSwitch.isActivated) // true
Time resolution
We can set time resolution of the events. Default resolution: MINUTE
.
First, load the Resolutions constants.
const { Resolutions } = MemoSwitch
In ES modules,
import MemoSwitch, { Resolutions } from 'memo-switch'
Then, set at 2nd argument of MemoSwitch.initialize()
.
const memoSwitch = MemoSwitch.initialize(false, Resolutions.MILLISECOND)
Available resolutions
- MILLISECOND
- SECOND
- MINUTE
- HOUR
- DAY
- MONTH
- YEAR
Save/Restore the switch
Just stringify it.
const str = JSON.stringify(memoSwitch)
To restore, give the object to constructor.
const obj = JSON.parse(str)
const memoSwitch2 = new MemoSwitch(obj)
LICENSE
MIT