mitt<\/strong><\/div>\nMitt \u662f\u4e00\u4e2a\u529f\u80fd\u6027 \u4e8b\u4ef6\u53d1\u5c04\u5668\uff0c\u7528\u4e8e\u5728 JavaScript \u4e2d\u8fdb\u884c\u4e8b\u4ef6\u53d1\u5e03\u548c\u8ba2\u9605\u3002\u5b83\u4f53\u79ef\u5c0f\u5de7\uff0c\u538b\u7f29\u540e\u53ea\u6709 200 \u5b57\u8282\uff0c\u4e14\u65e0\u9700\u4f9d\u8d56\u5176\u4ed6\u5e93\u3002\u5b83\u5177\u6709\u7c7b\u4f3c\u4e8e Node.js \u4e2d\u7684 EventEmitter \u7684\u529f\u80fd\uff0c\u5e76\u652f\u6301\u4f7f\u7528\u901a\u914d\u7b26\u201c*\u201d\u6765\u76d1\u542c\u6240\u6709\u4e8b\u4ef6\u3002Mitt \u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u548c\u5176\u4ed6 JavaScript \u8fd0\u884c\u65f6\u4e2d\u4f7f\u7528\uff0c\u5e76\u652f\u6301 IE9+\u3002<\/p>\n
\u4e0b\u9762\u662f\u4f7f\u7528 Mitt \u8fdb\u884c\u4e8b\u4ef6\u53d1\u5e03\u548c\u8ba2\u9605\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n
\/\/ \u521b\u5efa\u4e00\u4e2a mitt \u5b9e\u4f8b\r\nconst emitter = mitt();\r\n\/\/ \u8ba2\u9605 'myEvent' \u4e8b\u4ef6\r\nemitter.on('myEvent', (data) => {\r\n console.log('myEvent triggered with data:', data);\r\n});\r\n\/\/ \u53d1\u5e03 'myEvent' \u4e8b\u4ef6\r\nemitter.emit('myEvent', { foo: 'bar' }); \/\/ \u8f93\u51fa: myEvent triggered with data: { foo: 'bar' }<\/pre>\n\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26 * \u6765\u76d1\u542c\u6240\u6709\u4e8b\u4ef6\uff1a<\/p>\n
emitter.on('*', (event, data) => {\r\n console.log(`event \"${event}\" triggered with data:`, data);\r\n});\r\nemitter.emit('myEvent', { foo: 'bar' }); \r\n\/\/ \u8f93\u51fa: event \"myEvent\" triggered with data: { foo: 'bar' }\r\nemitter.emit('anotherEvent', { baz: 'qux' }); \r\n\/\/ \u8f93\u51fa: event \"anotherEvent\" triggered with data: { baz: 'qux' }<\/pre>\n\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 emitter.off \u65b9\u6cd5\u53d6\u6d88\u8ba2\u9605\u7279\u5b9a\u4e8b\u4ef6\uff1a<\/p>\n
const handler = (data) => {\r\n console.log('myEvent triggered with data:', data);\r\n};\r\nemitter.on('myEvent', handler);\r\nemitter.emit('myEvent', { foo: 'bar' }); \/\/ \u8f93\u51fa: myEvent triggered with data: { foo: 'bar' }\r\nemitter.off('myEvent', handler);\r\nemitter.emit('myEvent', { foo: 'bar' }); \/\/ \u4e0d\u4f1a\u8f93\u51fa\u4efb\u4f55\u5185\u5bb9<\/pre>\ntiny-emitter<\/strong><\/div>\ntiny-emitter \u540c\u6837\u662f\u4e00\u4e2a\u5c0f\u578b\u7684\u4e8b\u4ef6\u53d1\u5c04\u5668\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728 JavaScript \u4e2d\u8fdb\u884c\u4e8b\u4ef6\u53d1\u5e03\u548c\u8ba2\u9605\u3002<\/p>\n
\u4f7f\u7528 tiny-emitter \u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
\/\/ \u521b\u5efa\u4e00\u4e2a tiny-emitter \u5b9e\u4f8b\r\nconst emitter = new TinyEmitter();\r\n\/\/ \u8ba2\u9605 'myEvent' \u4e8b\u4ef6\r\nemitter.on('myEvent', (data) => {\r\n console.log('myEvent triggered with data:', data);\r\n});\r\n\/\/ \u53d1\u5e03 'myEvent' \u4e8b\u4ef6\r\nemitter.emit('myEvent', { foo: 'bar' }); \r\n\/\/ \u8f93\u51fa: myEvent triggered with data: { foo: 'bar' }<\/pre>\nMitt \u4e0e tiny-emitter\u7684\u5f02\u540c<\/strong><\/div>\nMitt \u548c tiny-emitter \u90fd\u662f JavaScript \u4e8b\u4ef6\u53d1\u5c04\u5668\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728 JavaScript \u4e2d\u8fdb\u884c\u4e8b\u4ef6\u53d1\u5e03\u548c\u8ba2\u9605\u3002\u4f46\u662f\uff0c\u5b83\u4eec\u6709\u4e00\u4e9b\u660e\u663e\u7684\u533a\u522b\uff1a<\/p>\n
\u4f53\u79ef\uff1aMitt \u6bd4 tiny-emitter \u66f4\u5c0f\uff0c\u538b\u7f29\u540e\u4ec5\u6709 200 \u5b57\u8282\u3002tiny-emitter \u7684\u4f53\u79ef\u7565\u5927\uff0c\u538b\u7f29\u540e\u7ea6\u4e3a 2.2 KB\u3002<\/li>\n\u652f\u6301\u7684\u6d4f\u89c8\u5668\u7248\u672c\uff1aMitt \u652f\u6301 IE9+\uff0c\u800c tiny-emitter \u652f\u6301 IE6+\u3002<\/li>\n\u5c3d\u7ba1\u5b83\u4eec\u6709\u4e00\u4e9b\u533a\u522b\uff0cMitt \u548c tiny-emitter \u90fd\u662f\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u4e8b\u4ef6\u53d1\u5c04\u5668\u5e93\u3002\u9009\u62e9\u54ea\u4e2a\u5e93\u53d6\u51b3\u4e8e\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u9700\u6c42\u548c\u504f\u597d\u3002<\/p>\n
\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f<\/strong><\/div>\n\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\uff08\u4e5f\u79f0\u4e3a\u89c2\u5bdf\u8005\u6a21\u5f0f\uff09\u662f\u4e00\u79cd\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5141\u8bb8\u5bf9\u8c61\uff08\u79f0\u4e3a\u53d1\u5e03\u8005\uff09\u5411\u591a\u4e2a\u5bf9\u8c61\uff08\u79f0\u4e3a\u8ba2\u9605\u8005\uff09\u53d1\u5e03\u4fe1\u606f\uff0c\u5e76\u5141\u8bb8\u8ba2\u9605\u8005\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u3002\u8fd9\u79cd\u6a21\u5f0f\u901a\u5e38\u7528\u4e8e\u5728\u4e0d\u540c\u7ec4\u4ef6\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u7ec4\u4ef6\u4e4b\u95f4\u76f4\u63a5\u4ea4\u4e92\u3002<\/p>\n
\u5728\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u4e2d\uff0c\u53d1\u5e03\u8005\u901a\u5e38\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8d1f\u8d23\u7ef4\u62a4\u8ba2\u9605\u8005\u7684\u5217\u8868\u5e76\u53d1\u5e03\u4fe1\u606f\u3002\u8ba2\u9605\u8005\u901a\u5e38\u662f\u53e6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8d1f\u8d23\u8ba2\u9605\u7279\u5b9a\u7684\u4fe1\u606f\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u8ba2\u9605\u8005\u901a\u5e38\u4f1a\u5b9e\u73b0\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5728\u6536\u5230\u53d1\u5e03\u8005\u53d1\u5e03\u7684\u4fe1\u606f\u65f6\u8c03\u7528\u3002<\/p>\n
\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u5141\u8bb8\u7ec4\u4ef6\u4e4b\u95f4\u677e\u6563\u8026\u5408\uff0c\u5e76\u4e14\u6613\u4e8e\u6269\u5c55\u3002\u53d1\u5e03\u8005\u65e0\u9700\u77e5\u9053\u54ea\u4e9b\u8ba2\u9605\u8005\u8ba2\u9605\u4e86\u4fe1\u606f\uff0c\u8ba2\u9605\u8005\u4e5f\u65e0\u9700\u77e5\u9053\u54ea\u4e9b\u53d1\u5e03\u8005\u53d1\u5e03\u4e86\u4fe1\u606f\u3002\u8fd9\u4f7f\u5f97\u53d1\u5e03\u8005\u548c\u8ba2\u9605\u8005\u53ef\u4ee5\u72ec\u7acb\u53d8\u5316\u548c\u6269\u5c55\u3002<\/p>\n
\u793a\u4f8b\uff1a<\/p>\n
class Publisher {\r\n constructor() {\r\n this.subscribers = [];\r\n }\r\n register(subscriber) {\r\n this.subscribers.push(subscriber);\r\n }\r\n unregister(subscriber) {\r\n this.subscribers = this.subscribers.filter(s => s !== subscriber);\r\n }\r\n notify(message) {\r\n this.subscribers.forEach(subscriber => subscriber.update(message));\r\n }\r\n}\r\nclass Subscriber {\r\n update(message) {\r\n console.log('Received message:', message);\r\n }\r\n}\r\nconst publisher = new Publisher();\r\nconst subscriber1 = new Subscriber();\r\nconst subscriber2 = new Subscriber();\r\npublisher.register(subscriber1);\r\npublisher.register(subscriber2);\r\npublisher.notify('Hello, world!'); \/\/ Output: \"Received message: Hello, world!\"\r\npublisher.unregister(subscriber1);\r\npublisher.notify('Hello, again!'); \/\/ Output: \"Received message: Hello, again!\"<\/pre>\n\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a Publisher \u7c7b\u548c\u4e00\u4e2a Subscriber \u7c7b\u3002Publisher \u7c7b\u7ef4\u62a4\u4e86\u4e00\u4e2a\u8ba2\u9605\u8005\u5217\u8868\uff0c\u53ef\u4ee5\u6ce8\u518c\u548c\u53d6\u6d88\u8ba2\u9605\u8005\uff0c\u5e76\u4e14\u53ef\u4ee5\u5411\u6240\u6709\u8ba2\u9605\u8005\u53d1\u9001\u6d88\u606f\u3002Subscriber \u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a update \u65b9\u6cd5\uff0c\u7528\u4e8e\u63a5\u6536\u6765\u81ea\u53d1\u5e03\u8005\u7684\u6d88\u606f\u3002<\/p>\n
\u5728\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u53d1\u5e03\u8005\u5b9e\u4f8b\u548c\u4e24\u4e2a\u8ba2\u9605\u8005\u5b9e\u4f8b\uff0c\u5e76\u5c06\u8ba2\u9605\u8005\u6ce8\u518c\u5230\u53d1\u5e03\u8005\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u53d1\u5e03\u8005\u7684 notify \u65b9\u6cd5\u5411\u6240\u6709\u8ba2\u9605\u8005\u53d1\u9001\u6d88\u606f\uff0c\u5e76\u4f7f\u7528 unregister \u65b9\u6cd5\u53d6\u6d88\u4e00\u4e2a\u8ba2\u9605\u8005\u7684\u6ce8\u518c\u3002<\/p>\n
\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u662f\u4e00\u79cd\u6709\u7528\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u89e3\u51b3\u8bb8\u591a\u5bf9\u8c61\u6216\u7ec4\u4ef6\u4e4b\u95f4\u7684\u534f\u4f5c\u95ee\u9898\u3002\u5b83\u4f7f\u5f97\u5bf9\u8c61\u6216\u7ec4\u4ef6\u4e4b\u95f4\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u534f\u4f5c\uff0c\u540c\u65f6\u4e5f\u80fd\u89e3\u9664\u8026\u5408\uff0c\u4f7f\u5f97\u7cfb\u7edf\u66f4\u52a0\u7a33\u5b9a\u548c\u53ef\u6269\u5c55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u672c\u6b21\u8bfe\u7a0b\u7684\u5b66\u4e60\u76ee\u6807\uff1a \u4e86\u89e3 mitt\u3001tiny-emitter \u4f5c\u7528\u548c\u4f7f\u7528\u573a\u666f\u3001\u5e94\u7528\u573a\u666f \u5b66\u4e60 \u53d1\u5e03\u8ba2\u9605 \u8bbe\u8ba1 […]<\/p>\n","protected":false},"author":1479,"featured_media":257158,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-260472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/260472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=260472"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/260472\/revisions"}],"predecessor-version":[{"id":260475,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/260472\/revisions\/260475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/257158"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=260472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=260472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=260472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}