Transform postMessage calls to return a promise
Available on NPM and bower under name self-addressed
.
Provides single function stamp(mailman, address, data)
, that returns a Promise.
var stamp = require('self-addressed');
var mailman = function (address, envelope) {
address.postMessage(envelope, '*');
};
stamp(mailman, window, { foo: 'foo' })
.then(function (response) {
console.log('got response to our letter', response);
});
Single function selfAddressed
can do 3 things at once
selfAddressed(mailman, address, data)
- can deliver data via mailman
to address
and then
returns a promise that resolves with response.selfAddressed(envelope, letter)
- puts letter
into valid enveloper
to be delivered back as
a response.selfAddressed(envelope)
- returns either the letter stored in the envelope
or undefined
if this
is an invalid envelope.You can see verbose log messages by setting selfAddressed.options.verbose = true;
iframed page.html that wants to communicate with the parent can postMessage
and receive a reply
function mailman(address, msg) {
address.postMessage(msg, '*');
}
window.onmessage = function (event) {
selfAddressed(event.data);
};
selfAddressed(mailman, parent, 'foo')
.then(function (response) {
console.log(response); // "bar"
});
index page that iframes page.html
inside and uses onmessage
to open the envelope,
place new letter inside and send the response back to the source.
function mailman(address, msg) {
address.postMessage(msg, '*');
}
window.onmessage = function (event) {
var letter = selfAddressed(event.data);
// if letter === undefined => not an envelope, handle differently!
var returnAddress = event.source;
selfAddressed(event.data, 'bar'); // places the letter into the same envelope
selfAddressed(mailman, returnAddress, event.data);
};
Often we have other sources of data to be handled inside onmessage
callback.
One can ask if the received data is self-addressed to determine if it has been handled
by self-addressed
or not using selfAddressed.is(envelope)
.
window.onmessage = function (event) {
// event could be self-addressed envelope or not
if (selfAddressed.is(event.data)) {
var letter = selfAddressed(event.data);
if (!letter) {
// nothing to do - it has been handled via promise resolution
} else {
// handle letter, maybe reseal and send response?
}
} else {
// not an envelope - handle yourself
}
}
Author: Gleb Bahmutov © 2015
License: MIT - do anything with the code, but don't blame me if it does not work.
Spread the word: tweet, star on github, etc.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2015 Gleb Bahmutov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.