Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aleclarson/spy-on


https://github.com/aleclarson/spy-on

Last synced: 19 days ago
JSON representation

Awesome Lists containing this project

README

        

# @alloc/spy-on

Observe assignments to any writable, configurable object property.

```js
import spyOn from '@alloc/spy-on'

const obj = { a: 0 }

// The same property can be spied on multiple times.
const logger = spyOn(obj, 'a', console.log)
const mirror = spyOn(obj, 'a', a => (obj.b = a))

obj.a++ // logs "1"
obj.a == obj.b // => true

// Spies can be disposed.
logger.dispose()
obj.a++ // logs nothing
obj.a == obj.b // => true

mirror.dispose()
obj.a++ // logs nothing
obj.a == obj.b // => false

// Once all spies are disposed, the original descriptor
// is restored.
Object.defineProperty(obj, 'a').value == 3 // => true
```