js/listener.md

33 lines
940 B
Markdown
Raw Permalink Normal View History

# Listener
A generator for proxy objects that run callbacks when properties are changed.
## Interface
```js
2022-03-17 08:49:57 +00:00
listener()
// Creates a new listener object
listener(some_object)
// Creates a new listener object that acts as a proxy for some_object
listener.listen(prop, callback)
2022-03-17 08:49:57 +00:00
// Adds a callback on a specific property change
listener.listen(null, callback)
// Adds a callback on any property change
listener.listen(prop, callback, {once: true})
// Adds a one-time callback
```
2021-10-19 16:08:48 +00:00
2021-12-26 16:45:17 +00:00
```js
2022-03-17 08:49:57 +00:00
listener.forget(prop, callback)
// Removes a specific callback on a specific property
listener.forget(prop, null)
// Removes all callbacks from a property
// Note that undefined won't work, to avoid accidents
// it really has to be null
listener.forget(null, callback)
// This is not a special case, it simply removes a
// callback that was registered with lisetner.listen(null, callback)
2021-12-26 16:45:17 +00:00
```
2022-03-17 08:49:57 +00:00
Note: Forgetting one-time callbacks is not (yet) possible.