{"id":15893911,"url":"https://github.com/chinanf-boy/explain-mitt","last_synced_at":"2026-01-16T00:53:56.079Z","repository":{"id":90548247,"uuid":"119546198","full_name":"chinanf-boy/explain-mitt","owner":"chinanf-boy","description":"explain mitt Tiny 200b emitter/on 订阅/触发 模式","archived":false,"fork":false,"pushed_at":"2018-02-01T13:56:37.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T08:47:14.470Z","etag":null,"topics":["explain","explain-mitt","mitt"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chinanf-boy.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-01-30T14:20:29.000Z","updated_at":"2018-01-30T15:53:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"938f79fe-a809-40c6-9995-4592c4e5965d","html_url":"https://github.com/chinanf-boy/explain-mitt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fexplain-mitt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fexplain-mitt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fexplain-mitt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chinanf-boy%2Fexplain-mitt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chinanf-boy","download_url":"https://codeload.github.com/chinanf-boy/explain-mitt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246866100,"owners_count":20846496,"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":["explain","explain-mitt","mitt"],"created_at":"2024-10-06T08:14:03.497Z","updated_at":"2026-01-16T00:53:56.044Z","avatar_url":"https://github.com/chinanf-boy.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# mitt\n\nTiny 200 byte functional event emitter / pubsub.\n\n发布 / 订阅\n\n[![explain](http://llever.com/explain.svg)](https://github.com/chinanf-boy/Source-Explain)\n    \nExplanation\n\n\u003e \"version\": \"1.0.0\"\n\n[github source](https://github.com/developit/mitt)\n\n~~[english](./README.en.md)~~\n\n---\n\n我们从主文件着手\n\n- package.json\n\n\u003e `\"rollup\": \"rollup -c\",`\n\n- rollup.config.js\n\n\u003e `entry: 'src/index.js',`\n\n可以看出, 入口 `src/index.js`\n\n---\n\n## 使用\n\n``` js\nimport mitt from 'mitt'\n\nlet 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// working with handler references:\nfunction onFoo() {}\nemitter.on('foo', onFoo)   // listen\nemitter.off('foo', onFoo)  // unlisten\n```\n\n[preact+mitt codepen](http://codepen.io/developit/pen/rjMEwW?editors=0110)\n\n---\n\n- [index.js](#index)\n\n- [rollup-flow 类型推导插件](#rollup-flow])\n\n- [`rollup.config.js` 解释](#rollup-config)\n\n\n---\n## index\n\n### rollup-flow\n\n这是-`rollup`-构建工具的，类型推导插件.\n\n可用来, 作为类型约束\n\n[rollup.config.js](#rollup-config)\n\n``` js\n// @flow\n// An event handler can take an optional event argument\n// and should not return a value\ntype EventHandler = (event?: any) =\u003e void;\ntype WildCardEventHandler = (type: string, event?: any) =\u003e void\n\n// An array of all currently registered event handlers for a type\ntype EventHandlerList = Array\u003cEventHandler\u003e;\ntype WildCardEventHandlerList = Array\u003cWildCardEventHandler\u003e;\n// A map of event types and their corresponding event handlers.\ntype EventHandlerMap = {\n  '*'?: WildCardEventHandlerList,\n  [type: string]: EventHandlerList,\n};\n\n```\n\n``` js\n/** Mitt: Tiny (~200b) functional event emitter / pubsub.\n *  @name mitt\n *  @returns {Mitt}\n */\nexport default function mitt(all: EventHandlerMap) {\n\tall = all || Object.create(null);\n\n\treturn {\n\t\t/**\n\t\t * 通过给予的·type·, 保存-handler-函数\n\t\t *\n\t\t * @param  {String} type\tType of event to listen for, or `\"*\"` for all events\n\t\t * @param  {Function} handler Function to call in response to given event\n\t\t * @memberOf mitt\n\t\t */\n\t\ton(type: string, handler: EventHandler) {\n\t\t\t(all[type] || (all[type] = [])).push(handler);\n\t\t},\n\n\t\t/**\n\t\t * 通过给予的·type·, 移除-handler-函数\n\t\t *\n\t\t * @param  {String} type\tType of event to unregister `handler` from, or `\"*\"`\n\t\t * @param  {Function} handler Handler function to remove\n\t\t * @memberOf mitt\n\t\t */\n\t\toff(type: string, handler: EventHandler) {\n\t\t\tif (all[type]) {\n\t\t\t\tall[type].splice(all[type].indexOf(handler) \u003e\u003e\u003e 0, 1);\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * 触发-`type`-所有相应函数\n\t\t *  `\"*\"` 函数 也会根据·type·触发\n\t\t *\n\t\t * @param {String} type  The event type to invoke\n\t\t * @param {Any} [evt]  Any value (object is recommended and powerful), passed to each handler\n\t\t * @memberOf mitt\n\t\t */\n\t\temit(type: string, evt: any) {\n\t\t\t(all[type] || []).slice().map((handler) =\u003e { handler(evt); });\n\t\t\t(all['*'] || []).slice().map((handler) =\u003e { handler(type, evt); });\n\t\t}\n\t};\n}\n\n```\n\n- `on` 一 保存\n\n- `off` 二 移除\n\n- `emit` 三 触发\n\n---\n\n## rollup-config\n\n``` js\nimport buble from 'rollup-plugin-buble';\nimport flow from 'rollup-plugin-flow';\nimport fs from 'fs';\n\nconst pkg = JSON.parse(fs.readFileSync('./package.json'));\n\nexport default {\n\tentry: 'src/index.js', // 入口\n\tuseStrict: false, // 是否严格模式\n\tsourceMap: true, // 源码地图-debug\n\tplugins: [\n\t\tflow(), // 类型约束\n\t\tbuble() // 用buble转换ES2015。\n\t],\n\ttargets: [\n\t\t{ dest: pkg.main, format: 'cjs' }, // cjc 属于-通用-旧版-js\n\t\t{ dest: pkg.module, format: 'es' }, // es6 属于-可以使用-新版本js\n\t\t{ dest: pkg['umd:main'], format: 'umd', moduleName: pkg.name } // umd 属于-验证是否在node|browser平台-js\n\t]\n};\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinanf-boy%2Fexplain-mitt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchinanf-boy%2Fexplain-mitt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchinanf-boy%2Fexplain-mitt/lists"}