fix(trnasport): Names of the arguments for Transport and backend

Now the backends are working with 'message' and Transport is
working with 'request', 'response' and 'event'.
This commit is contained in:
hristoterezov 2017-04-28 16:03:36 -05:00
parent b49c1c6ba2
commit 96bde3ff44
2 changed files with 42 additions and 39 deletions

View File

@ -52,7 +52,7 @@ const LEGACY_OUTGOING_METHODS = [
*
* @type {string}
*/
const POSTIS_METHOD_NAME = 'data';
const POSTIS_METHOD_NAME = 'message';
/**
* Implements message transport using the postMessage API.
@ -71,10 +71,10 @@ export default class PostMessageTransportBackend {
});
/**
* If true PostMessageTransportBackend will process and send data using
* the legacy format and in the same time the current format. Otherwise
* all data received that is using the legacy format will be ignored and
* no data with the legacy format will be sent.
* If true PostMessageTransportBackend will process and send messages
* using the legacy format and in the same time the current format.
* Otherwise all messages (outgoing and incoming) that are using the
* legacy format will be ignored.
*
* @type {boolean}
*/
@ -85,7 +85,10 @@ export default class PostMessageTransportBackend {
LEGACY_INCOMING_METHODS.forEach(method =>
this.postis.listen(
method,
params => this._legacyDataReceiveCallback(method, params)));
params =>
this._legacyMessageReceivedCallback(method, params)
)
);
}
this._receiveCallback = () => {
@ -95,17 +98,17 @@ export default class PostMessageTransportBackend {
this.postis.listen(
POSTIS_METHOD_NAME,
data => this._receiveCallback(data));
message => this._receiveCallback(message));
}
/**
* Handles incoming legacy postis data.
* Handles incoming legacy postis messages.
*
* @param {string} method - The method property from postis data object.
* @param {Any} params - The params property from postis data object.
* @param {string} method - The method property from the postis message.
* @param {Any} params - The params property from the postis message.
* @returns {void}
*/
_legacyDataReceiveCallback(method, params = {}) {
_legacyMessageReceivedCallback(method, params = {}) {
this._receiveCallback({
data: {
name: method,
@ -115,12 +118,12 @@ export default class PostMessageTransportBackend {
}
/**
* Sends the passed data via postis using the old format.
* Sends the passed message via postis using the old format.
*
* @param {Object} data - The data to be sent.
* @param {Object} legacyMessage - The message to be sent.
* @returns {void}
*/
_sendLegacyData({ data, name }) {
_sendLegacyMessage({ data, name }) {
if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
this.postis.send({
method: name,
@ -139,23 +142,23 @@ export default class PostMessageTransportBackend {
}
/**
* Sends the passed data.
* Sends the passed message.
*
* @param {Object} data - The data to be sent.
* @param {Object} message - The message to be sent.
* @returns {void}
*/
send(data) {
send(message) {
this.postis.send({
method: POSTIS_METHOD_NAME,
params: data
params: message
});
if (this._enableLegacyFormat) {
// For the legacy use case we don't need any new fields defined in
// Transport class. That's why we are passing only the original
// object passed by the consumer of the Transport class which is
// data.data.
this._sendLegacyData(data.data);
// message.data.
this._sendLegacyMessage(message.data);
}
}

View File

@ -72,30 +72,30 @@ export default class Transport {
}
/**
* Handles incoming data from the transport backend.
* Handles incoming messages from the transport backend.
*
* @param {Object} data - The data.
* @param {Object} message - The message.
* @returns {void}
*/
_onDataReceived(data) {
if (data.type === MESSAGE_TYPE_RESPONSE) {
const handler = this._responseHandlers.get(data.id);
_onMessageReceived(message) {
if (message.type === MESSAGE_TYPE_RESPONSE) {
const handler = this._responseHandlers.get(message.id);
if (handler) {
handler(data);
this._responseHandlers.delete(data.id);
handler(message);
this._responseHandlers.delete(message.id);
}
} else if (data.type === MESSAGE_TYPE_REQUEST) {
this.emit('request', data.data, (result, error) => {
} else if (message.type === MESSAGE_TYPE_REQUEST) {
this.emit('request', message.data, (result, error) => {
this._backend.send({
type: MESSAGE_TYPE_RESPONSE,
error,
id: data.id,
id: message.id,
result
});
});
} else {
this.emit('event', data.data);
this.emit('event', message.data);
}
}
@ -202,16 +202,16 @@ export default class Transport {
}
/**
* Sends the passed data.
* Sends the passed event.
*
* @param {Object} data - The data to be sent.
* @param {Object} event - The event to be sent.
* @returns {void}
*/
sendEvent(data = {}) {
sendEvent(event = {}) {
if (this._backend) {
this._backend.send({
type: MESSAGE_TYPE_EVENT,
data
data: event
});
}
}
@ -219,10 +219,10 @@ export default class Transport {
/**
* Sending request.
*
* @param {Object} data - The data for the request.
* @param {Object} request - The request to be sent.
* @returns {Promise}
*/
sendRequest(data) {
sendRequest(request) {
if (!this._backend) {
return Promise.reject(new Error('No transport backend defined!'));
}
@ -244,7 +244,7 @@ export default class Transport {
this._backend.send({
type: MESSAGE_TYPE_REQUEST,
data,
data: request,
id
});
});
@ -260,6 +260,6 @@ export default class Transport {
this._disposeBackend();
this._backend = backend;
this._backend.setReceiveCallback(this._onDataReceived.bind(this));
this._backend.setReceiveCallback(this._onMessageReceived.bind(this));
}
}