{"id":13451793,"url":"https://github.com/developit/mitt","last_synced_at":"2025-05-12T05:16:29.839Z","repository":{"id":37698366,"uuid":"78983309","full_name":"developit/mitt","owner":"developit","description":"🥊 Tiny 200 byte functional event emitter / pubsub.","archived":false,"fork":false,"pushed_at":"2024-08-14T08:58:20.000Z","size":217,"stargazers_count":11278,"open_issues_count":20,"forks_count":458,"subscribers_count":70,"default_branch":"main","last_synced_at":"2025-05-11T02:54:17.719Z","etag":null,"topics":["event","event-bus","event-handlers","event-listener","eventemitter","mitt","pubsub","tiny"],"latest_commit_sha":null,"homepage":"https://npm.im/mitt","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-15T01:10:50.000Z","updated_at":"2025-05-10T02:34:49.000Z","dependencies_parsed_at":"2022-07-09T11:00:26.445Z","dependency_job_id":"2b66bce1-6d90-486b-a90a-b1be233b1d53","html_url":"https://github.com/developit/mitt","commit_stats":{"total_commits":119,"total_committers":37,"mean_commits":"3.2162162162162162","dds":0.6470588235294117,"last_synced_commit":"6b41670516ed8e8b738612f60491995470aa63b3"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fmitt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fmitt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fmitt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fmitt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/mitt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253509775,"owners_count":21919587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event","event-bus","event-handlers","event-listener","eventemitter","mitt","pubsub","tiny"],"created_at":"2024-07-31T07:01:02.753Z","updated_at":"2025-05-11T02:54:23.782Z","avatar_url":"https://github.com/developit.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://i.imgur.com/BqsX9NT.png\" width=\"300\" height=\"300\" alt=\"mitt\"\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://www.npmjs.org/package/mitt\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/mitt.svg\" alt=\"npm\"\u003e\u003c/a\u003e\n  \u003cimg src=\"https://github.com/developit/mitt/workflows/CI/badge.svg\" alt=\"build status\"\u003e\n  \u003ca href=\"https://unpkg.com/mitt/dist/mitt.js\"\u003e\u003cimg src=\"https://img.badgesize.io/https://unpkg.com/mitt/dist/mitt.js?compression=gzip\" alt=\"gzip size\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n# Mitt\n\n\u003e Tiny 200b functional event emitter / pubsub.\n\n-   **Microscopic:** weighs less than 200 bytes gzipped\n-   **Useful:** a wildcard `\"*\"` event type listens to all events\n-   **Familiar:** same names \u0026 ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)\n-   **Functional:** methods don't rely on `this`\n-   **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken\n\nMitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.\n\n## Table of Contents\n\n-   [Install](#install)\n-   [Usage](#usage)\n-   [Examples \u0026 Demos](#examples--demos)\n-   [API](#api)\n-   [Contribute](#contribute)\n-   [License](#license)\n\n## Install\n\nThis project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.\n\n```sh\n$ npm install --save mitt\n```\n\nThen with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:\n\n```javascript\n// using ES6 modules\nimport mitt from 'mitt'\n\n// using CommonJS modules\nvar mitt = require('mitt')\n```\n\nThe [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):\n\n```html\n\u003cscript src=\"https://unpkg.com/mitt/dist/mitt.umd.js\"\u003e\u003c/script\u003e\n```\n\nYou can find the library on `window.mitt`.\n\n## Usage\n\n```js\nimport mitt from 'mitt'\n\nconst emitter = mitt()\n\n// listen to an event\nemitter.on('foo', e =\u003e console.log('foo', e) )\n\n// listen to all events\nemitter.on('*', (type, e) =\u003e console.log(type, e) )\n\n// fire an event\nemitter.emit('foo', { a: 'b' })\n\n// clearing all events\nemitter.all.clear()\n\n// working with handler references:\nfunction onFoo() {}\nemitter.on('foo', onFoo)   // listen\nemitter.off('foo', onFoo)  // unlisten\n```\n\n### Typescript\n\nSet `\"strict\": true` in your tsconfig.json to get improved type inference for `mitt` instance methods.\n\n```ts\nimport mitt from 'mitt';\n\ntype Events = {\n  foo: string;\n  bar?: number;\n};\n\nconst emitter = mitt\u003cEvents\u003e(); // inferred as Emitter\u003cEvents\u003e\n\nemitter.on('foo', (e) =\u003e {}); // 'e' has inferred type 'string'\n\nemitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)\n```\n\nAlternatively, you can use the provided `Emitter` type:\n\n```ts\nimport mitt, { Emitter } from 'mitt';\n\ntype Events = {\n  foo: string;\n  bar?: number;\n};\n\nconst emitter: Emitter\u003cEvents\u003e = mitt\u003cEvents\u003e();\n```\n\n## Examples \u0026 Demos\n\n\u003ca href=\"http://codepen.io/developit/pen/rjMEwW?editors=0110\"\u003e\n  \u003cb\u003ePreact + Mitt Codepen Demo\u003c/b\u003e\n  \u003cbr\u003e\n  \u003cimg src=\"https://i.imgur.com/CjBgOfJ.png\" width=\"278\" alt=\"preact + mitt preview\"\u003e\n\u003c/a\u003e\n\n* * *\n\n## API\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n#### Table of Contents\n\n-   [mitt](#mitt)\n-   [all](#all)\n-   [on](#on)\n    -   [Parameters](#parameters)\n-   [off](#off)\n    -   [Parameters](#parameters-1)\n-   [emit](#emit)\n    -   [Parameters](#parameters-2)\n\n### mitt\n\nMitt: Tiny (~200b) functional event emitter / pubsub.\n\nReturns **Mitt** \n\n### all\n\nA Map of event names to registered handler functions.\n\n### on\n\nRegister an event handler for the given type.\n\n#### Parameters\n\n-   `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all events\n-   `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event\n\n### off\n\nRemove an event handler for the given type.\nIf `handler` is omitted, all handlers of the given type are removed.\n\n#### Parameters\n\n-   `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `'*'`\n-   `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Handler function to remove\n\n### emit\n\nInvoke all handlers for the given type.\nIf present, `'*'` handlers are invoked after type-matched handlers.\n\nNote: Manually firing '\\*' handlers is not supported.\n\n#### Parameters\n\n-   `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \\| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invoke\n-   `evt` **Any?** Any value (object is recommended and powerful), passed to each handler\n\n## Contribute\n\nFirst off, thanks for taking the time to contribute!\nNow, take a moment to be sure your contributions make sense to everyone else.\n\n### Reporting Issues\n\nFound a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).\nIf don't, just open a [new clear and descriptive issue](../../issues/new).\n\n### Submitting pull requests\n\nPull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.\n\n-   Fork it!\n-   Clone your fork: `git clone https://github.com/\u003cyour-username\u003e/mitt`\n-   Navigate to the newly cloned directory: `cd mitt`\n-   Create a new branch for the new feature: `git checkout -b my-new-feature`\n-   Install the tools necessary for development: `npm install`\n-   Make your changes.\n-   Commit your changes: `git commit -am 'Add some feature'`\n-   Push to the branch: `git push origin my-new-feature`\n-   Submit a pull request with full remarks documenting your changes.\n\n## License\n\n[MIT License](https://opensource.org/licenses/MIT) © [Jason Miller](https://jasonformat.com/)\n","funding_links":[],"categories":["TypeScript","JavaScript","Event Emitter","语言资源库","others",":clap: 欢迎参与​","前端常用","Components \u0026 Libraries","Event Emitters"],"sub_categories":["typescript","工具库","Utilities"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fmitt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fmitt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fmitt/lists"}