🥊 Tiny 200 byte functional event emitter / pubsub.
Installations
npm install mitt
Developer Guide
Typescript
No
Module System
CommonJS, ESM
Node Version
18.12.1
NPM Version
9.7.2
Score
98.8
Supply Chain
98.2
Quality
75.5
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Languages
TypeScript (100%)
Developer
Download Statistics
Total Downloads
742,603,717
Last Day
318,551
Last Week
7,834,508
Last Month
32,245,183
Last Year
321,002,704
GitHub Statistics
10,969 Stars
139 Commits
446 Forks
70 Watching
8 Branches
32 Contributors
Bundle Size
477.00 B
Minified
270.00 B
Minified + Gzipped
Package Meta Information
Latest Version
3.0.1
Package Id
mitt@3.0.1
Unpacked Size
25.82 kB
Size
6.00 kB
File Count
10
NPM Version
9.7.2
Node Version
18.12.1
Publised On
04 Jul 2023
Total Downloads
Cumulative downloads
Total Downloads
742,603,717
Last day
-11.7%
318,551
Compared to previous day
Last week
-3.7%
7,834,508
Compared to previous week
Last month
-2.3%
32,245,183
Compared to previous month
Last year
104.6%
321,002,704
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Mitt
Tiny 200b functional event emitter / pubsub.
- Microscopic: weighs less than 200 bytes gzipped
- Useful: a wildcard
"*"
event type listens to all events - Familiar: same names & ideas as Node's EventEmitter
- Functional: methods don't rely on
this
- Great Name: somehow mitt wasn't taken
Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.
Table of Contents
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
1$ npm install --save mitt
Then with a module bundler like rollup or webpack, use as you would anything else:
1// using ES6 modules 2import mitt from 'mitt' 3 4// using CommonJS modules 5var mitt = require('mitt')
The UMD build is also available on unpkg:
1<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>
You can find the library on window.mitt
.
Usage
1import mitt from 'mitt' 2 3const emitter = mitt() 4 5// listen to an event 6emitter.on('foo', e => console.log('foo', e) ) 7 8// listen to all events 9emitter.on('*', (type, e) => console.log(type, e) ) 10 11// fire an event 12emitter.emit('foo', { a: 'b' }) 13 14// clearing all events 15emitter.all.clear() 16 17// working with handler references: 18function onFoo() {} 19emitter.on('foo', onFoo) // listen 20emitter.off('foo', onFoo) // unlisten
Typescript
Set "strict": true
in your tsconfig.json to get improved type inference for mitt
instance methods.
1import mitt from 'mitt'; 2 3type Events = { 4 foo: string; 5 bar?: number; 6}; 7 8const emitter = mitt<Events>(); // inferred as Emitter<Events> 9 10emitter.on('foo', (e) => {}); // 'e' has inferred type 'string' 11 12emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
Alternatively, you can use the provided Emitter
type:
1import mitt, { Emitter } from 'mitt'; 2 3type Events = { 4 foo: string; 5 bar?: number; 6}; 7 8const emitter: Emitter<Events> = mitt<Events>();
Examples & Demos
Preact + Mitt Codepen DemoAPI
Table of Contents
mitt
Mitt: Tiny (~200b) functional event emitter / pubsub.
Returns Mitt
all
A Map of event names to registered handler functions.
on
Register an event handler for the given type.
Parameters
type
(string | symbol) Type of event to listen for, or'*'
for all eventshandler
Function Function to call in response to given event
off
Remove an event handler for the given type.
If handler
is omitted, all handlers of the given type are removed.
Parameters
type
(string | symbol) Type of event to unregisterhandler
from, or'*'
handler
Function? Handler function to remove
emit
Invoke all handlers for the given type.
If present, '*'
handlers are invoked after type-matched handlers.
Note: Manually firing '*' handlers is not supported.
Parameters
type
(string | symbol) The event type to invokeevt
Any? Any value (object is recommended and powerful), passed to each handler
Contribute
First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.
Reporting Issues
Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.
Submitting pull requests
Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
- Fork it!
- Clone your fork:
git clone https://github.com/<your-username>/mitt
- Navigate to the newly cloned directory:
cd mitt
- Create a new branch for the new feature:
git checkout -b my-new-feature
- Install the tools necessary for development:
npm install
- Make your changes.
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request with full remarks documenting your changes.
License
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 8/26 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/compressed-size.yml:9: update your workflow using https://app.stepsecurity.io/secureworkflow/developit/mitt/compressed-size.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/compressed-size.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/developit/mitt/compressed-size.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/developit/mitt/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/developit/mitt/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:21
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/compressed-size.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 14 are checked with a SAST tool
Score
4.1
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More