1.2.2 • Published 6 years ago

@denlb/timer-state-machine v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

timer-state-machine

Build Status npm version

Simple self-transitioning state machine implementation. Automatically changes state when the lifetime of a current state is over.

diagram

Usage

Create a timerStateMachine instance:

const timerStateMachine = new TimerStateMachine([
  {
    name: 'study',
    duration: 2700000 // 45 minutes in ms
  },
  {
    name: 'break',
    duration: 900000 // 15 minutes in ms
  },
])
timerStateMachine.start()

Start with the first state:

timerStateMachine.start()

Or resume with any state if you want to continue:

timerStateMachine.start({
  name: 'break',
  duration: 540000 // 9 minutes left
})

Subscribe to state changed event:

const onStateChanged = (newState) => {
  console.log(newState)
}
const unsubscribe = timerStateMachine.subscribe(onStateChanged)

Unubscribe specified subscription:

unsubscribe()

// or

timerStateMachine.unsubscribe(onStateChanged)

Unubscribe all subscriptions:

timerStateMachine.unsubscribe()
1.2.2

6 years ago

1.2.1

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago