new Message(type)
Initialize a new `Message`
Parameters
Name | Type | Description |
---|---|---|
type |
String | Message type |
Members
respond
Respond to a message.
Example
receiver.on('hello', message => {
message.respond('world');
});
Methods
send(endpoint) → {Promise}
Send the message to an endpoint.
Parameters
Name | Type | Description |
---|---|---|
endpoint |
Iframe | Window | Worker | MessagePort |
Returns
- Type
- Promise
cancel()
Cancel a pending Message.
var msg = message('foo')
msg.send(new Worker('my-worker.js'))
.then(response => {
// this will never run
})
msg.cancel();
forward(endpoint)
Forward a `Message` onto another endpoint.
The `silentTrue` option prevents the
message request timing out should
the response come back via an
alternative route.
TODO: If forwarded message errors
check it reaches origin (#86).
Parameters
Name | Type | Description |
---|---|---|
endpoint |
HTMLIframeElement | MessagePort | Window |
on(type, callback) → {this}
Add an event listener.
It is possible to subscript to * events.
Parameters
Name | Type | Description |
---|---|---|
type |
String | |
callback |
function |
Returns
for chaining
- Type
- this
off(typeopt, callbackopt) → {this}
Remove an event listener.
emitter.off('name', fn); // remove one callback
emitter.off('name'); // remove all callbacks for 'name'
emitter.off(); // remove all callbacks
Parameters
Name | Type | Attributes | Description |
---|---|---|---|
type |
String |
<optional> |
|
callback |
function |
<optional> |
Returns
for chaining
- Type
- this
emit(type, dataopt) → {this}
Emit an event.
emitter.emit('name', { some: 'data' });
Parameters
Name | Type | Attributes | Description |
---|---|---|---|
type |
String | ||
data |
* |
<optional> |
Returns
for chaining
- Type
- this