state-monitor

1.0.0 • Public • Published

state-monitor

Caveman approach to state monitoring. Doesn't use proxies & does not modify the objects/values in any way. It's just a periodic diff of values returned by provided resolver function.

In short

const monitor = stateMonitor(resolverFunction, refreshRateInMilliseconds)
monitor.on('change', changeHandler) // changeHandler receives structural difference info returned from 'deep-diff' npm module
// to stop
monitor.emit(‘stop’)

Note: in the usage example below you can see the usage of describeDiff helper function for human friendly change logs

Usage example

import stateMonitor from 'state-monitor'

let a = {
  name: 'my object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'elements']
  }
}

const mon = stateMonitor(() => a, 10) // check for difference every 10 milliseconds
mon.on('change', stateMonitor.describeDiff) // helper function for logging changes

a = {
  name: 'updated object',
  description: 'it\'s an object!',
  details: {
    it: 'has',
    an: 'array',
    with: ['a', 'few', 'more', 'elements', { than: 'before' }]
  }
}

// this should output something like:

/* change
EDITED 'name': my object -> updated object
EDITED 'details.with.2': elements -> more
ALTERED at index: 3
NEW: undefined -> elements
ALTERED at index: 4
NEW: undefined -> {"than":"before"}
*/

// stop monitoring
monitor.emit(‘stop’)

Libraries used

  • immutablejs for taking state "snapshots"
  • deep-diff for diffing snapshots
  • eventemitter2 to emit changes

Dependents (0)

Package Sidebar

Install

npm i state-monitor

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • popc0rn