{"id":35011689,"url":"https://github.com/aooiuu/easy-post-message","last_synced_at":"2026-04-27T10:31:55.332Z","repository":{"id":188465208,"uuid":"678652658","full_name":"aooiuu/easy-post-message","owner":"aooiuu","description":"🥊适用于浏览器和Electron的消息通信工具, postMessage + mitt 支持返回值, 支持自定义 adapter","archived":false,"fork":false,"pushed_at":"2023-08-16T10:06:49.000Z","size":51,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-01T10:14:45.307Z","etag":null,"topics":["bus","electron","eventbus","iframe","mitt","postmessage"],"latest_commit_sha":null,"homepage":"https://aooiuu.github.io/easy-post-message/","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/aooiuu.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}},"created_at":"2023-08-15T03:35:55.000Z","updated_at":"2025-01-08T07:49:23.000Z","dependencies_parsed_at":"2023-08-15T13:32:35.498Z","dependency_job_id":null,"html_url":"https://github.com/aooiuu/easy-post-message","commit_stats":null,"previous_names":["aooiuu/easy-post-message"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aooiuu/easy-post-message","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aooiuu%2Feasy-post-message","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aooiuu%2Feasy-post-message/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aooiuu%2Feasy-post-message/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aooiuu%2Feasy-post-message/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aooiuu","download_url":"https://codeload.github.com/aooiuu/easy-post-message/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aooiuu%2Feasy-post-message/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32333196,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["bus","electron","eventbus","iframe","mitt","postmessage"],"created_at":"2025-12-27T04:59:20.230Z","updated_at":"2026-04-27T10:31:55.322Z","avatar_url":"https://github.com/aooiuu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyPostMessage\n\n[![npm version][npm-version-src]][npm-version-href]\n[![bundle][bundle-src]][bundle-href]\n[![JSDocs][jsdocs-src]][jsdocs-href]\n\n`postMessage` + `mitt` 支持返回值, 支持自定义 adapter\n\n- 一组和 `mitt` 类似的方法: `emit`、`on`、`off`\n- 一组 `Promise` 风格传递数据的方法: `send`、`answer`\n- 多端支持: 浏览器、electron、自定义\n\n## Usage\n\n完整例子: `docs\\index.html`\n\n```sh\nnpm i easy-post-message\n```\n\n```javascript\nimport EasyPostMessage from 'easy-post-message';\n\nconst pm = new EasyPostMessage();\n\n// 监听 event1 事件\npm.on('event1', ({ data, source }) =\u003e consle.log({ data, source }));\n\n// 监听 event2 事件\npm.on('event2', ({ data, source }) =\u003e consle.log({ data, source }));\n\n// 向 window.top 发送事件\npm.emit('event1', data, window.top);\n\n// 监听 event10 事件 并返回数据\npm.answer('event10', (data) =\u003e {\n  return 'answer val';\n});\n\npm.answer('event11', async (data) =\u003e {\n  return Promise.resolve('answer xxx');\n});\n\n// 向 window.top 发送事件, 并接收返回值\nconsle.log(await pm.send('event10', data, window.top));\n```\n\n## Methods\n\n```typescript\n/**\n * 监听事件\n * @param {string | symbol} event 事件类型\n * @param {Function} callback 回调函数\n */\non(event: Event, callback: (arg: EventData) =\u003e void): void;\n\n/**\n * 移除自定义事件监听器\n * @param {string | symbol} event 事件类型\n * @param {Function} callback 回调函数\n */\noff(event: Event, callback: (arg: EventData) =\u003e void): void;\n\n/**\n * 发送事件\n * @param {string | symbol} event 事件类型\n * @param {Object} data\n * @param {Window} target 发送对象\n */\nemit(event: Event, data: any, target?: Window | unknown): void;\n\n/**\n * 发送事件, 可以接收参数\n * @param {string | symbol} event 事件类型\n * @param {Object} data\n * @param {Window} target 发送对象\n * @returns {Promise}\n */\nsend(event: Event, data: any, target?: Window | unknown): Promise\u003cany\u003e;\n\n/**\n * 监听事件, 可以返回参数\n * @param {string | symbol} event 事件类型\n * @param {Function} callback 回调函数\n */\nanswer(event: Event, callback: (data: any) =\u003e void): void;\n```\n\n## Adapter\n\n默认 adapter 是基于浏览器的 `window.postMessage`, 你也可以自定义 adapter 用以支持其他平台\n\n```javascript\nnew EasyPostMessage(Adapter);\n```\n\n### Electron\n\n基于 `ipcMain` 和 `ipcRenderer`, main 进程发送消息时需要指定窗口\n\n```javascript\nimport EasyPostMessage from 'easy-post-message';\nimport Adapter from 'easy-post-message/electron-adapter';\n\nconst pm = new EasyPostMessage(Adapter);\n\n// main\nconst win = new BrowserWindow(/** */);\npm.emit('a', data, win);\npm.on('b', () =\u003e {});\n\npm.send('c', data, win);\npm.answer('d', () =\u003e {\n  return 'xx';\n});\n\n// renderer\npm.emit('b', data);\npm.on('a', () =\u003e {});\n\npm.send('d', data);\npm.answer('c', () =\u003e {\n  return 'xx';\n});\n```\n\n\u003c!-- Badges --\u003e\n\n[npm-version-src]: https://img.shields.io/npm/v/easy-post-message?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[npm-version-href]: https://npmjs.com/package/easy-post-message\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/easy-post-message?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[bundle-href]: https://bundlephobia.com/result?p=easy-post-message\n[jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat\u0026colorA=18181B\u0026colorB=F0DB4F\n[jsdocs-href]: https://www.jsdocs.io/package/easy-post-message\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faooiuu%2Feasy-post-message","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faooiuu%2Feasy-post-message","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faooiuu%2Feasy-post-message/lists"}