james-bond
TypeScript icon, indicating that this package has built-in type declarations

0.7.3 • Public • Published

James Bond

Hire James Bond to observe your (object) targets.

Summary

Use james-bond to observe changes to properties in objects.

Usage

observe(obj)/unobserve(obj)

Observe particular properties of an object (non-deeply).

import * as bond from 'james-bond'

const janus = {
	name: 'Alec Trevelyan',
	stats: {
		kills: 8,
	},
	kill(someone) {
		console.log('killed ' + someone)
		this.stats.kills++
	},
}

const callback = (key, value) => {
	console.log(key, value)
}

bond.observe(janus, ['name'], callback)
bond.observe(janus.stats, ['kills'], callback)

setTimeout(() => {
	janus.name = 'Janus' // logs "name Janus"
	janus.kill('someone') // logs "kills 9"
}, 1000)

Later, unobserve when done:

// Unobserve all props for the given callback (in this case, just "kills")
bond.unobserve(janus.stats, callback)
// Alternatively, unobserve specific props for the given callback.
bond.unobserve(janus, ['name'], callback)

Roadmap

  • Add deferral options, f.e. to have updates batched into a microtask or animation frame.

Package Sidebar

Install

npm i james-bond

Weekly Downloads

15

Version

0.7.3

License

MIT

Unpacked Size

20.3 kB

Total Files

13

Last publish

Collaborators

  • trusktr