Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit c202261

Browse files
author
Daniel Brain
committed
Better docs
1 parent 73af0e6 commit c202261

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

README.md

+35-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
# postRobot
22

3-
Simple postMessage based server. Good for if you want to use post-messaging between windows, but you don't like the
4-
fire-and-forget nature of post messages.
3+
Simple postMessage based server.
54

6-
With this, you can set up a listener in one window, have it wait for a post message, and then have it reply with data.
5+
Use this if you want to communicate between two different windows (or popups, or iframes) using `window.postMessage`,
6+
but you don't like the fire-and-forget nature of the native api.
7+
8+
With this, you can set up a listener in one window, have it wait for a post message, and then have it reply with data,
9+
and gracefully handle any errors that crop up. You can also set a timeout, to be sure that the other window is responding to you,
10+
and fail gracefully if it does not.
711

812
## Example
913

10-
// In one window
14+
```javascript
1115

12-
postRobot.listen({
16+
// In one window
1317

14-
name: 'get_cart',
18+
postRobot.listen({
1519

16-
handler: function(data, callback) {
17-
setTimeout(function() {
18-
return callback(null, {
19-
amount: 1,
20-
shipping: 2
21-
});
22-
}, 3000)
23-
}
24-
});
20+
name: 'get_cart',
21+
22+
handler: function(data, callback) {
23+
setTimeout(function() {
24+
return callback(null, {
25+
amount: 1,
26+
shipping: 2
27+
});
28+
}, 3000)
29+
}
30+
});
2531

26-
// In another window
32+
// In another window
2733

28-
postRobot.request({
34+
postRobot.request({
2935

30-
window: window,
31-
name: 'get_cart',
36+
window: window,
37+
name: 'get_cart',
38+
39+
response: function(err, data) {
40+
if (err) {
41+
throw err;
42+
}
43+
console.log('got cart!', data)
44+
},
3245

33-
response: function(err, data) {
34-
if (err) {
35-
throw err;
36-
}
37-
console.log('got cart!', data)
38-
},
46+
timeout: 1000
47+
});
3948

40-
timeout: 1000
41-
});
49+
```

0 commit comments

Comments
 (0)