1.0.0 • Published 9 years ago

see-change v1.0.0

Weekly downloads
15
License
MIT
Repository
github
Last release
9 years ago

see-change

npm.io npm.io npm.io npm.io

Detect changes in the return values of a list of functions. Useful for memoizing functions with multiple dependencies: e.g. only updating a visualization when specific values have changed.

Usage

NPM

change = seechange(functions)

Takes an array of functions that return a value and returns a change function.

change()

Returns true if the return values of any of the previously supplied functions have changed since it was last called.

arrayChanged = seechange.array(array)

Takes an array of values and returns a change function.

arrayChanged()

Returns true if any elements in the array have changed since the function was last called. May also be supplied as a function in seechange, and changes will propogate upwards as expected.

Example

const seechange = require('see-change')

var highlighted = 0
var viewport = [0, 0, 0, 0]
var selected = 0

const change = seechange([
  () => highlighted,
  () => selected,
  seechange.array(viewport)
])

// When any of the functions return a different value,
// `change()` will return `true`:
change() // false
selected = 20
change() // true
change() // false

// seechange.array will check to see if an array has changed
viewport[0] = 2
change() // true
change() // false

This makes it easy to transparently check for changes at the beginning of each frame in a render loop, e.g.:

function render () {
  if (!change()) return
  // shiny render code...
}

License

MIT. See LICENSE.md for details.