前端移动端框架如何推动数字化转型与创新发展
1514
2022-11-05
Mitt: 微小的函数事件emitter / pubsub
Mitt
Tiny 200b functional event emitter / pubsub.
Microscopic: weighs less than 200 bytes gzippedUseful: a wildcard "*" event type listens to all eventsFamiliar: same names & ideas as Node's EventEmitterFunctional: methods don't rely on thisGreat 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
InstallUsageExamples & DemosAPIContributeLicense
Install
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm install --save mitt
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modulesimport mitt from 'mitt'// using CommonJS modulesvar mitt = require('mitt')
The UMD build is also available on unpkg:
You can find the library on window.mitt.
Usage
import mitt from 'mitt'const emitter = mitt()// listen to an eventemitter.on('foo', e => console.log('foo', e) )// listen to all eventsemitter.on('*', (type, e) => console.log(type, e) )// fire an eventemitter.emit('foo', { a: 'b' })// working with handler references:function onFoo() {}emitter.on('foo', onFoo) // listenemitter.off('foo', onFoo) // unlisten
Typescript
import mitt from 'mitt';const emitter: mitt.Emitter = mitt();
Examples & Demos
API
Table of Contents
mittonParameters offParameters emitParameters
mitt
Mitt: Tiny (~200b) functional event emitter / pubsub.
Returns Mitt
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.
Parameters
type (string | symbol) Type of event to unregister handler 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/
License
MIT License © Jason Miller
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~