New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

memo-switch

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

memo-switch

A switch that memorizes state transition events.

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

memo-switch

A switch that memorizes state transition events.

CircleCI npm version David

API Documentation

concept.png

  • 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

Keywords

event sourcing

FAQs

Package last updated on 15 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts