Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sauldoescode/eventilator
cute event manager with super powers
https://github.com/sauldoescode/eventilator
addlistener dom dom-manipulation event-handling event-management javascript-library proxy utility-library
Last synced: about 2 months ago
JSON representation
cute event manager with super powers
- Host: GitHub
- URL: https://github.com/sauldoescode/eventilator
- Owner: SaulDoesCode
- Created: 2018-05-19T09:03:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-03T17:25:21.000Z (about 2 years ago)
- Last Synced: 2024-10-30T05:25:35.945Z (3 months ago)
- Topics: addlistener, dom, dom-manipulation, event-handling, event-management, javascript-library, proxy, utility-library
- Language: JavaScript
- Size: 447 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Eventilator: but can your event manager do this?
## API:
```js
eventilator(
once Boolean,
target EventTarget|String|Array,
type String|Object,
=handle Function,
=options Boolean|Object = false
)
```
* ``.on/once['any-event-name'] | .on/once(target, event, handle, options)`` - add listener
* ``.on/once(target, { event: handle })`` - add listeners
* ``.off(target, event, handle) | handle.off`` - remove listener
* ``.emit(target, type, detail) | handle.emit(type, detail)`` - emit event
* ``.curry`` - extra (used internally but why not share)```js
const {on, once} = eventilatorlet count = 0
const handle = on.anyEvent('div.target', (event, target) => {
count++
})console.log(handle.target instanceof Element)
handle.emit('anyEvent')
handle.off() // same as handle == handle.off
console.log(`
The handler is currently ${handle.ison ? 'on' : 'off'}.
`)handle.emit('anyEvent')
handle.on()console.log(`
The handler is currently ${handle.ison ? 'on' : 'off'}.
`)handle.emit('anyEvent')
setTimeout(() => {
console.log(`The count should be 2 it is ${count}`)
}, 5)
``````js
const {on, once} = eventilatorconst handles = once('div.fancy-element', {
click (event, fancyElement) {},
keydown (event, fancyElement) {}
})const {
click: {off: clickOff},
keydown: {off: keydownOff}
} = handlestry {
await someUnavailableOperation()
} catch (e) {
handles.click.off()
}
```Arrays or Selectors finding multiple elements works as well.
```js
const [aHndl, bHndl, cHndl] = on.pointerover([a, b, c], (e, elementX) => {
// do something
})aHndl.off()
bHndl.off()
cHndl.off()// or
const handlers = on.pointerover('main > div.fancy-card', (e, cardDiv) => {
// do something
})
handlers.off() // <- loops over each and turns'em off
```