Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aleclarson/spy-on
https://github.com/aleclarson/spy-on
Last synced: 19 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/aleclarson/spy-on
- Owner: aleclarson
- Created: 2020-11-19T21:47:56.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2020-11-19T22:43:22.000Z (almost 4 years ago)
- Last Synced: 2024-10-13T04:06:24.928Z (24 days ago)
- Language: TypeScript
- Homepage:
- Size: 42 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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 // => truemirror.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
```