https://github.com/nnecec/mittss
Lightweight, extendable event emitter / pubsub.
https://github.com/nnecec/mittss
emitter event event-bus event-handlers event-listener eventbus eventemitter mitt pubsub
Last synced: 22 days ago
JSON representation
Lightweight, extendable event emitter / pubsub.
- Host: GitHub
- URL: https://github.com/nnecec/mittss
- Owner: nnecec
- Created: 2025-01-08T17:32:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-08-19T01:56:56.000Z (10 months ago)
- Last Synced: 2025-10-30T16:58:45.246Z (8 months ago)
- Topics: emitter, event, event-bus, event-handlers, event-listener, eventbus, eventemitter, mitt, pubsub
- Language: TypeScript
- Homepage: https://nnecec.github.io/mittss/
- Size: 260 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Mittss
> forked from [mitt](https://github.com/developit/mitt)



[](https://codecov.io/github/nnecec/mittss)

[](https://deepwiki.com/nnecec/mittss)
## What is Mittss?
`mitt + class = mittss`
`Mittss` is an lightweight and simple event emitter / pubsub, rewrite the `mitt`
using a class.
- **Lightweight**: weighs less than 300 bytes gzipped
- **Useful**: a wildcard `"*"` event type listens to all events
- **Familiar**: same names & ideas as
[Node's EventEmitter](https://nodejs.org/docs/latest/api/events.html#class-eventemitter)
- **Extendable**: easily extendable with inheritance.
- **Zero Dependencies**: Works out of the box without additional libraries.
## Differences from mitt
`Emitter` is forked from [mitt](https://github.com/developit/mitt), with the
following key differences:
- Implemented using `class`, supporting inheritance and extension.
- Provides a `once` method for one-time event listeners.
## Installation
```bash
npm install mittss
```
or
```bash
pnpm add mittss
```
## Usage
Visit the [documentation website](https://nnecec.github.io/mittss/) to explore
more guides and API references.
### Basic usage
```javascript
import { Emitter } from 'mittss'
// Create a Emitter instance
const emitter = new Emitter()
// listen to an event
emitter.on('foo', e => console.log('foo', e))
// listen to all events
emitter.on('*', (type, e) => console.log(type, e))
// fire an event
emitter.emit('foo', { a: 'b' })
// clearing all events
emitter.all.clear()
// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo) // listen
emitter.off('foo', onFoo) // unlisten
emitter.once('foo', onFoo) // listen once
```
### Typescript
Set "strict": true in your tsconfig.json to get improved type inference for mitt
instance methods.
```typescript
import { Emitter } from 'mittss'
type Events = {
foo: string
bar?: number
}
const emitter = new Emitter() // inferred as Emitter
emitter.on('foo', e => {}) // 'e' has inferred type 'string'
emitter.emit('foo', 42) // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
```
### Extending Emitter
Since `Emitter` is a class, you can easily extend it to add custom
functionality:
```javascript
class MyEmitter extends Emitter {
constructor() {
super()
}
greet(name) {
this.emit('greet', name)
}
}
const myEmitter = new MyEmitter()
myEmitter.on('greet', name => {
console.log(`Hello, ${name}!`)
})
myEmitter.greet('Bob') // Output: Hello, Bob!
```
### Functional Emitter
```typescript
const mitt = >(
all?: EventHandlerMap,
) => {
return new Emitter(all)
}
const emitter = mitt()
```
## Acknowledgments
Thanks to [mitt](https://github.com/developit/mitt) again for the inspiration.
## License
MIT © [nnecec](https://github.com/nnecec)