https://github.com/avoidwork/tiny-observable
Tiny Observable for Client or Server, to be used with DOM elements or custom objects.
https://github.com/avoidwork/tiny-observable
Last synced: 10 months ago
JSON representation
Tiny Observable for Client or Server, to be used with DOM elements or custom objects.
- Host: GitHub
- URL: https://github.com/avoidwork/tiny-observable
- Owner: avoidwork
- License: bsd-3-clause
- Created: 2015-12-03T23:29:36.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2025-04-01T00:17:20.000Z (10 months ago)
- Last Synced: 2025-04-01T01:26:11.054Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 737 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Tiny Observable
Tiny Observable for Client or Server, to be used with DOM elements or custom objects.
## Using the factory
```javascript
import {observable} from "tiny-observable";
const observer = observable();
// Hooking into every click event
observer.hook(document.querySelector("body"), "click");
// Capturing events, and redirecting
observer.on("click", ev => customFunction(ev));
```
## Using the Class
```javascript
import {Observable} from "tiny-observable";
const observer = Observable();
```
```javascript
import {Observable} from "tiny-observable";
class MyObservable extends Observable {}
```
## Testing
Tiny Observable has 100% code coverage with its tests.
```console
> nyc mocha test/*.js
Observable
√ Will create a new observable
√ Will reassign limit with setMaxListeners()
√ Will dispatch events when empty
√ Will hook element dispatch of event
√ Will dispatch event
√ Will unhook element dispatch of event
√ Will add & remove event handlers to a hooked input
√ Will throw errors with bad inputs
8 passing (6ms)
---------------------|---------|----------|---------|---------|---------------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------|---------|----------|---------|---------|---------------------------------------
All files | 95.83 | 76.71 | 86.95 | 100 |
tiny-observable.cjs | 95.83 | 76.71 | 86.95 | 100 | 18-24,38,58,63,89,114,127-139,154-155
---------------------|---------|----------|---------|---------|---------------------------------------
```
## API
### addListener(event, handler[, id, scope])
Adds a listener for an event.
### dispatch(event [, ...]);
Dispatches an event, with optional arguments.
### emit(event [, ...]);
Dispatches an event, with optional arguments.
### eventNames();
Returns an Array of event names.
### getMaxListeners();
Returns the limit of listeners per event.
### listenerCount(event);
Returns the count of listeners per event; this does not relate to hooked objects or elements.
### hook(object, event);
Hooks an event on an object or element.
### id();
Returns a random ID.
### off(event, id);
Removes all, or a specific listener for an event.
### on(event, handler[, id, scope])
Adds a listener for an event.
### once(event, handler[, id, scope])
Adds a single execution event listener for an event.
### rawListeners(event)
Returns an Array of raw functions for an event.
### removeAllListeners(event)
Removes all event listeners for an event.
### removeListener(event, id)
Removes an event listener by id.
### setMaxListeners(n);
Sets the limit of listeners per event.
### unhook(object, event);
Unhooks an event from an object or element.
## License
Copyright (c) 2023 Jason Mulligan
Licensed under the BSD-3 license