0.1.3 • Published 9 years ago
memo-switch v0.1.3
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-switchUsage
const MemoSwitch = require('memo-switch')
const memoSwitch = MemoSwitch.initialize()
console.log(memoSwitch.isActivated) // false
memoSwitch.toggle()
console.log(memoSwitch.isActivated) // truefor 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)) // falseDefault state
Set default state to the 1st argument of MemoSwitch.initialize().
const memoSwitch = MemoSwitch.initialize(true)
console.log(memoSwitch.isActivated) // trueTime resolution
We can set time resolution of the events. Default resolution: MINUTE.
First, load the Resolutions constants.
const { Resolutions } = MemoSwitchIn 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