Package Exports
- evnty
Readme
Evnty
0-Deps, simple, fast, for browser and node js anonymous event library.
Table of Contents
Features
- Supports ESM and CommonJS
- Promises support
- Full-featured typeScript support
- Browser & Workers environment compatibility
- Performance eventemitter3/eventemitter2/event-emitter/events/native node/native browser
Roadmap
- Namespaces/Wildcards
- Times To Listen (TTL)
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
Installing
Using yarn:
yarn add evnty
Using npm:
npm install evnty
Examples
import createEvent, { Event, once } from 'evnty';
// Creates a click event
type Click = { button: string };
const clickEvent = createEvent<[Click]>();
const handleClick = ({ button }: Click) => console.log('Clicked button is', button);
const unsubscribeClick = clickEvent.on(handleClick);
// Creates a key press event
type KeyPress = { key: string };
const keyPressEvent = createEvent<[KeyPress]>();
const handleKeyPress = ({ key }: KeyPress) => console.log('Key pressed', key);
const unsubscribeKeyPress = keyPressEvent.on(handleKeyPress);
// Merges click and key press events into input event
type Input = Click | KeyPress;
const handleInput = (input: Input) => console.log('Input', input);;
const inputEvent = Event.merge(clickEvent, keyPressEvent);
inputEvent.on(handleInput);
// Filters a click event to only include left-click events.
const handleLeftClick = () => console.log('Left button is clicked');
const leftClickEvent = clickEvent.filter(({ button }) => button === 'left');
leftClickEvent.on(handleLeftClick);
// Will press Enter after one second
setTimeout(keyPressEvent, 1000, { key: 'Enter' });
// Waits once the first Enter key press event occurs
await once(keyPressEvent.first(({ key }) => key === 'Enter'));
keyPressEvent({ key: 'W' });
keyPressEvent({ key: 'A' });
keyPressEvent({ key: 'S' });
keyPressEvent({ key: 'D' });
clickEvent({ button: 'right' });
clickEvent({ button: 'left' });
clickEvent({ button: 'middle' });
// Unsubscribe click listener
unsubscribeClick();
// It does not log anything because of click listener is unsubscribed
leftClickEvent.off(handleLeftClick);
// Unsubscribe key press listener once first Esc key press occur
unsubscribeKeyPress.after(() => once(keyPressEvent.first(({ key }) => key === 'Esc')));
// Press Esc to unsubscribe key press listener
keyPressEvent({ key: 'Esc' });
License
License Apache-2.0 Copyright (c) 2021-present Ivan Zakharchanka