Skip to content

Commit 4e3ddba

Browse files
author
Saiya
committed
fix: typos in readme
1 parent 1ccd8f0 commit 4e3ddba

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

README.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">MessageHub</h1>
22

3-
<h5 align="center">A tinny(~2kb) utility than can simplify cross window(iframes, even workers) communications over `postMessage` and `addEventListener('message', xxx)`</h5>
3+
<h5 align="center">A tinny(~2kb) utility that can simplify cross window(iframes, even workers) communications over `postMessage` and `addEventListener('message', xxx)`</h5>
44
<div align="center">
55
<a href="https://travis-ci.com/oe/messagehub">
66
<img src="https://travis-ci.com/oe/messagehub.svg?branch=master" alt="Travis CI">
@@ -17,14 +17,14 @@
1717
</div>
1818

1919
## Features
20-
* **Tinny**: less than ~2kb gzipped, no external dependences required
20+
* **Tinny**: less than 2kb gzipped, no external dependencies required
2121
* **Compatibility**: use `postMessage` under the hood, support all modern browser(even IE8)
2222
* **Consistency**: use same api every where(parent window, iframe window, worker, etc)
2323
* **Simple API**: use api `on` `emit` `off` to handle all messages in current context from any window(parent window, child window, workers)
24-
* **Dedicated API**: use api `createDedicatedMessageHub` to create a dedicated message-hub to communicate with specified window(parent window, child window, workers)
25-
* **Responsible**: `emit` will return a promise that you can get response from the other side, you can return that response by return it in `on`'s callback
26-
* **Proxy Message**: with api `createProxyFor`, you can proxy all message from child window(iframe, webworker, etc) to parent window, or parent's parent window
27-
* **Typescript support**: this utility is written in typescript, has type definitions inborn
24+
* **Dedicated API**: use api `createDedicatedMessageHub` to create a dedicated message-hub to communicate with specified window(parent window, child window or worker)
25+
* **Responsible**: `emit` will return a promise that you can get response from the other side. You can respond `emit` on other side by return result in `on`'s callback
26+
* **Proxy Message**: with api `createProxy`, you can proxy all messages from a window(iframe, webworker, etc) to another window(worker)
27+
* **Typescript support**: this utility is written in typescript, has type definition inborn
2828

2929
## Install
3030

@@ -42,7 +42,7 @@ yarn add @evecalm/message-hub
4242

4343
## Usage
4444

45-
The following demo show you how to use it with iframe
45+
The following demo shows you how to use it to make normal window and its iframe communicate easily
4646

4747
in main window
4848
```js
@@ -51,18 +51,18 @@ import MessageHub from "@evecalm/messagehub"
5151
const iframeWin1 = document.getElementById('child-iframe-1').contentWindow
5252
const iframeWin2 = document.getElementById('child-iframe-2').contentWindow
5353

54-
// ----- listen message from peer ----
54+
// ----- listen messages from peer ----
5555

56-
// listen the message pageTitle from iframeWin1, and response it
56+
// listen the message pageTitle from iframeWin1, and respond it
5757
MessageHub.on(iframeWin1, 'pageTitle', () => {
5858
return document.title
5959
})
60-
// response to message getHead from iframeWin2
60+
// respond to message getHead from iframeWin2
6161
MessageHub.on(iframeWin2, 'getHead', () => {
6262
return document.head.outHTML
6363
})
6464

65-
// listen multi message by passing a handler map
65+
// listen multi messages by passing a handler map
6666
MessageHub.on(iframeWin1, {
6767
// no return, then the response is undefined
6868
notice: (name, msg) => {
@@ -94,28 +94,31 @@ import MessageHub from "@evecalm/messagehub"
9494
const peerWin = window.parent
9595

9696

97-
// send message to the parent and get its response
97+
// send a message to the parent and get its response
9898
MessageHub.emit(peerWin, "pageTitle").then(title => {
9999
console.log("page title of main thread", title)
100100
})
101101

102-
// create a dedicated message hub, so we won't need to pass `peerWin` every time
102+
// create a dedicated message hub, so you won't need to pass `peerWin` every time
103103
const messageHub = MessageHub.createDedicatedMessageHub(peerWin)
104104

105105
// send message to the parent, don't need the response
106106
messageHub.emit("notice", 'Jim', 'hello!')
107107

108-
// calc fibonacci, return the result to parent
109-
messageHub.on("fib", num => {
108+
// calc fibonacci, respond by a return
109+
messageHub.on("fib", async (num) => {
110+
// emit a message and wait its response
111+
const title = await messageHub.emit("pageTitle")
112+
console.log(title)
110113
return fib(num)
111114
});
112-
// listen multi message by passing a handler map
115+
// listen multi messages by passing a handler map
113116
messageHub.on({
114117
method1 () {},
115118
method2 () {},
116119
})
117120

118-
// use a recursion algorithm which will take more than half a minute when n big than 50
121+
// use a recursive algorithm which will take more than half a minute when n big than 50
119122
function fib(n) {
120123
if (n < 2) return n
121124
return fib(n - 1) + fib(n - 2)
@@ -126,15 +129,15 @@ To see a real world example, you may:
126129

127130
1. clone [this repo](https://github.com/evecalm/messagehub)
128131
2. check the code in folder `test`
129-
3. run `yarn` to install the project dependences
132+
3. run `yarn` to install the project dependencies
130133
4. run `yarn run dev` to view the sample
131134
5. navigate to <http://localhost:1234/worker/index.html> to see worker example
132135
6. navigate to <http://localhost:1234/frame/index.html> to see iframe example
133136

134137
## API
135138

136139
### MessageHub.emit(peer: Window | Worker, methodName: string, ...args: any[])
137-
Send message to peer, invoking `methodName` and all its arguments `args`.
140+
Send a message to peer, invoking `methodName` registered on the peer via `on` with all its arguments `args`.
138141

139142
This api return a promise, you can get response or catch the exception via it.
140143

@@ -148,7 +151,7 @@ method name you can want to call(emit) which registered(on) in peer
148151
`args` vary with `methodName`'s handler registered via `on` in peer's context
149152

150153
### MessageHub.on
151-
Listen message send from peer, it has following forms:
154+
Listen messages sent from peer, it has following forms:
152155
```ts
153156
// register(listen)) one handler for methodName when message received from peer
154157
MessageHub.on(peer: Window | Worker | '*', methodName: string, handler: Function)
@@ -164,14 +167,14 @@ MessageHub.on(peer: Window | Worker | '*', singleHandler: Function)
164167
3. you can set `peer` to `*` to listen all messages from all peers(parent, children, workers) to current window. **Due to worker's restrictions, you need register worker so that `*` could works worker's message by `MessageHub.on(worker, {})`**
165168

166169
#### methodName
167-
Method name to register, a `methodName` can only has one `handler`, the `handler` will be override if you set `methodName` multi times
170+
Method name to register, a `methodName` can only has one `handler`, the `handler` will be overrode if you set same `methodName` multi times
168171

169172
#### handler
170173
1. handler could be an async function
171-
3. if handlers with same methodName registered both in specified peer and `*`, only handler for peer will be triggered when a message received by peer
174+
3. if handlers with same methodName registered both in specified peer and `*`, only handler for peer will be triggered when a message sent to peer
172175

173176
#### handlerMap
174-
A object of handlers, keys are methodName, values are handlers
177+
A object of handlers, keys are methodNames, values are handlers
175178

176179
#### singleHandler
177180
`singleHandler` will receive all parameters, i.e. `(methodName, ...args)`
@@ -198,13 +201,13 @@ It returns a new messageHub with following properties:
198201
Forward all messages from `fromWin` to `toWin` then forward `toWin`'s response to the `fromWin`, instead of handle messages by self
199202

200203
There is a funny use case:
201-
If you got two iframe in your page, you can make them communicate directly by following code
204+
If you got two iframes in your page, you can make them communicate directly by following code
202205
```ts
203206
MessageHub.createProxy(frame1Win, frame2Win) // forward message from frame1Win to frame2Win
204207
MessageHub.createProxy(frame2Win, frame1Win) // forward message from frame2Win to frame1Win
205208
```
206209

207-
### ~~MessageHub.createProxyFor(peer: Window | Worker)~~ deprecated
210+
### MessageHub.createProxyFor(peer: Window | Worker) * deprecated *
208211
Deprecated, but still working, you should use `MessageHub.createProxy(peer, window.parent)` instead.
209212

210213
Forward all messages from peer to parent window then forward parent's response to the peer, instead of handle messages by self.

dist/message-hub.browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/message-hub.es.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* @evecalm/message-hub v1.0.18
2+
* @evecalm/message-hub v1.0.19
33
* Copyright© 2021 Saiya https://github.com/oe/messagehub
44
*/
55
const WINDOW_ID = Math.random().toString(36).slice(2);

dist/message-hub.umd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* @evecalm/message-hub v1.0.18
2+
* @evecalm/message-hub v1.0.19
33
* Copyright© 2021 Saiya https://github.com/oe/messagehub
44
*/
55
(function (global, factory) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@evecalm/message-hub",
3-
"version": "1.0.18",
3+
"version": "1.0.19",
44
"description": "a middleware based RPC library over `postMessage` can work with both web worker and iframe",
55
"main": "dist/message-hub.umd.js",
66
"module": "dist/message-hub.es.js",

0 commit comments

Comments
 (0)