You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-25Lines changed: 28 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
<h1align="center">MessageHub</h1>
2
2
3
-
<h5align="center">A tinny(~2kb) utility than can simplify cross window(iframes, even workers) communications over `postMessage` and `addEventListener('message', xxx)`</h5>
3
+
<h5align="center">A tinny(~2kb) utility that can simplify cross window(iframes, even workers) communications over `postMessage` and `addEventListener('message', xxx)`</h5>
***Tinny**: less than ~2kb gzipped, no external dependences required
20
+
***Tinny**: less than 2kb gzipped, no external dependencies required
21
21
***Compatibility**: use `postMessage` under the hood, support all modern browser(even IE8)
22
22
***Consistency**: use same api every where(parent window, iframe window, worker, etc)
23
23
***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
28
28
29
29
## Install
30
30
@@ -42,7 +42,7 @@ yarn add @evecalm/message-hub
42
42
43
43
## Usage
44
44
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
46
46
47
47
in main window
48
48
```js
@@ -51,18 +51,18 @@ import MessageHub from "@evecalm/messagehub"
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, {})`**
165
168
166
169
#### 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
168
171
169
172
#### handler
170
173
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
172
175
173
176
#### handlerMap
174
-
A object of handlers, keys are methodName, values are handlers
177
+
A object of handlers, keys are methodNames, values are handlers
175
178
176
179
#### singleHandler
177
180
`singleHandler` will receive all parameters, i.e. `(methodName, ...args)`
@@ -198,13 +201,13 @@ It returns a new messageHub with following properties:
198
201
Forward all messages from `fromWin` to `toWin` then forward `toWin`'s response to the `fromWin`, instead of handle messages by self
199
202
200
203
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
202
205
```ts
203
206
MessageHub.createProxy(frame1Win, frame2Win) // forward message from frame1Win to frame2Win
204
207
MessageHub.createProxy(frame2Win, frame1Win) // forward message from frame2Win to frame1Win
0 commit comments