https://github.com/mebtte/eventin
Type-constrained eventemitter.
https://github.com/mebtte/eventin
eventemitter type-constrained typescript
Last synced: 4 months ago
JSON representation
Type-constrained eventemitter.
- Host: GitHub
- URL: https://github.com/mebtte/eventin
- Owner: mebtte
- License: mit
- Created: 2022-09-06T06:51:05.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-05-14T14:08:40.000Z (5 months ago)
- Last Synced: 2025-05-26T13:55:10.577Z (4 months ago)
- Topics: eventemitter, type-constrained, typescript
- Language: TypeScript
- Homepage:
- Size: 149 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# eventin [](https://www.npmjs.com/package/eventin) [](https://github.com/mebtte/react-lrc/blob/master/LICENSE) [](https://bundlephobia.com/result?p=eventin)
Strongly-typed event emitter.
## Install & Usage
```sh
npm install eventin
``````ts
import Eventin from 'eventin';enum EventType {
OPEN = 'open',
CLOSE = 'close',
TOGGLE = 'toggle',
}const e = new Eventin<
EventType,
{
[EventType.OPEN]: { id: string };
[EventType.CLOSE]: { source: number };
[EventType.TOGGLE]: boolean;
}
>();const unlisten = e.listen(EventType.OPEN, ({ id }) => console.log(id));
const unlisten = e.listen(EventType.OPEN, ({ source }) => console.log(source)); // it will throw typescript errore.emit(EventType.OPEN, { id: 'eventin' });
e.emit(EventType.OPEN, 123); // it will throw typescript error/** listen once */
const unlisten = e.listen(EventType.OPEN, ({ id }) => console.log(id), {
once: true,
});/** trigger listener synchronously */
e.emit(EventType.OPEN, { id: 'eventin' }, { sync: true });/** unlistenAll */
e.unlistenAll(EventType.OPEN);
e.unlistenAll();
```Also you can use `e.unlisten` to remove a listener:
```ts
import Eventin, { Listener } from 'eventin';enum EventType {
OPEN = 'open',
CLOSE = 'close',
TOGGLE = 'toggle',
}
type EventTypeMapData = {
[EventType.OPEN]: { id: string };
[EventType.CLOSE]: { source: number };
[EventType.TOGGLE]: boolean;
};
const e = new Eventin();// (data: { id: string }) => void
type OpenListener = Listener;const openListener: OpenListener = (data) => console.log(data);
e.listen(EventType.OPEN, openListener);
e.unlisten(EventType.OPEN, openListener);
```## License
[MIT](./LICENSE)