https://github.com/tmaize/i-message
优雅的 iframe 通信封装
https://github.com/tmaize/i-message
iframe postmessage
Last synced: 12 months ago
JSON representation
优雅的 iframe 通信封装
- Host: GitHub
- URL: https://github.com/tmaize/i-message
- Owner: TMaize
- Created: 2022-10-25T13:16:26.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-26T12:20:21.000Z (over 3 years ago)
- Last Synced: 2025-05-11T09:47:40.132Z (about 1 year ago)
- Topics: iframe, postmessage
- Language: TypeScript
- Homepage: https://tmaize.github.io/i-message/
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# i-message
优雅的 iframe 通信封装
## 注册消息处理器
```ts
import * as msg from '@tmaize/i-message'
// 回调风格
// callback(new Error('xxx')) 返回错误
msg.on('test1', function (payload: msg.IRequest, callback: (data: any) => void) {
setTimeout(() => {
callback('resp:' + payload.messageId)
}, 3000)
})
// promise 风格
// return new Error('xxx') | Promise 返回错误
msg.on('test2', async function (payload: msg.IRequest) {
await sleep(3000)
return 'resp: ' + payload.messageId
})
```
## 发送消息
```ts
import * as msg from '@tmaize/i-message'
const resp = await msg.emit({ request: 'test1', target: window.parent, data: {} })
```