1.0.3 • Published 8 years ago

persistent-state v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

persistent-state

Super simple persistent states with JSON files.

import PersistentState from 'persistent-state'

const stateFile = '/path/to/file.json'

async function prepareState () {
  const state = new PersistentState(stateFile)
  await state.load()
  state.get('abc') // undefined
  state.set('abc', 123) // 123
  state.get('abc') // 123
  await state.save()
})

async function getABC () {
  const state = new PersistentState(stateFile)
  await state.load()
  return state.get('abc')
}

prepareState().then(() => {
  return getABC()
}).then((abc) => {
  console.log(abc) // 123
})

PersistentState(path)

Creates a persistent state instance.

  • path - Path to the state JSON file.

PersistentState.load()

Loads the persistent state from file. Returns a promise.

PersistentState.get(name)

Gets a field's value.

PersistentState.set(name, value, save = false)

Sets a field's value. If save is truthy, then this function returns a promise returned from .save().

PersistentState.save()

Saves the persistent state to file.

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago