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} * @type {string}
*/ */
const POSTIS_METHOD_NAME = 'data'; const POSTIS_METHOD_NAME = 'message';
/** /**
* Implements message transport using the postMessage API. * Implements message transport using the postMessage API.
@ -71,10 +71,10 @@ export default class PostMessageTransportBackend {
}); });
/** /**
* If true PostMessageTransportBackend will process and send data using * If true PostMessageTransportBackend will process and send messages
* the legacy format and in the same time the current format. Otherwise * using the legacy format and in the same time the current format.
* all data received that is using the legacy format will be ignored and * Otherwise all messages (outgoing and incoming) that are using the
* no data with the legacy format will be sent. * legacy format will be ignored.
* *
* @type {boolean} * @type {boolean}
*/ */
@ -85,7 +85,10 @@ export default class PostMessageTransportBackend {
LEGACY_INCOMING_METHODS.forEach(method => LEGACY_INCOMING_METHODS.forEach(method =>
this.postis.listen( this.postis.listen(
method, method,
params => this._legacyDataReceiveCallback(method, params))); params =>
this._legacyMessageReceivedCallback(method, params)
)
);
} }
this._receiveCallback = () => { this._receiveCallback = () => {
@ -95,17 +98,17 @@ export default class PostMessageTransportBackend {
this.postis.listen( this.postis.listen(
POSTIS_METHOD_NAME, 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 {string} method - The method property from the postis message.
* @param {Any} params - The params property from postis data object. * @param {Any} params - The params property from the postis message.
* @returns {void} * @returns {void}
*/ */
_legacyDataReceiveCallback(method, params = {}) { _legacyMessageReceivedCallback(method, params = {}) {
this._receiveCallback({ this._receiveCallback({
data: { data: {
name: method, 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} * @returns {void}
*/ */
_sendLegacyData({ data, name }) { _sendLegacyMessage({ data, name }) {
if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) { if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
this.postis.send({ this.postis.send({
method: name, 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} * @returns {void}
*/ */
send(data) { send(message) {
this.postis.send({ this.postis.send({
method: POSTIS_METHOD_NAME, method: POSTIS_METHOD_NAME,
params: data params: message
}); });
if (this._enableLegacyFormat) { if (this._enableLegacyFormat) {
// For the legacy use case we don't need any new fields defined in // 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 // Transport class. That's why we are passing only the original
// object passed by the consumer of the Transport class which is // object passed by the consumer of the Transport class which is
// data.data. // message.data.
this._sendLegacyData(data.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} * @returns {void}
*/ */
_onDataReceived(data) { _onMessageReceived(message) {
if (data.type === MESSAGE_TYPE_RESPONSE) { if (message.type === MESSAGE_TYPE_RESPONSE) {
const handler = this._responseHandlers.get(data.id); const handler = this._responseHandlers.get(message.id);
if (handler) { if (handler) {
handler(data); handler(message);
this._responseHandlers.delete(data.id); this._responseHandlers.delete(message.id);
} }
} else if (data.type === MESSAGE_TYPE_REQUEST) { } else if (message.type === MESSAGE_TYPE_REQUEST) {
this.emit('request', data.data, (result, error) => { this.emit('request', message.data, (result, error) => {
this._backend.send({ this._backend.send({
type: MESSAGE_TYPE_RESPONSE, type: MESSAGE_TYPE_RESPONSE,
error, error,
id: data.id, id: message.id,
result result
}); });
}); });
} else { } 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} * @returns {void}
*/ */
sendEvent(data = {}) { sendEvent(event = {}) {
if (this._backend) { if (this._backend) {
this._backend.send({ this._backend.send({
type: MESSAGE_TYPE_EVENT, type: MESSAGE_TYPE_EVENT,
data data: event
}); });
} }
} }
@ -219,10 +219,10 @@ export default class Transport {
/** /**
* Sending request. * Sending request.
* *
* @param {Object} data - The data for the request. * @param {Object} request - The request to be sent.
* @returns {Promise} * @returns {Promise}
*/ */
sendRequest(data) { sendRequest(request) {
if (!this._backend) { if (!this._backend) {
return Promise.reject(new Error('No transport backend defined!')); return Promise.reject(new Error('No transport backend defined!'));
} }
@ -244,7 +244,7 @@ export default class Transport {
this._backend.send({ this._backend.send({
type: MESSAGE_TYPE_REQUEST, type: MESSAGE_TYPE_REQUEST,
data, data: request,
id id
}); });
}); });
@ -260,6 +260,6 @@ export default class Transport {
this._disposeBackend(); this._disposeBackend();
this._backend = backend; this._backend = backend;
this._backend.setReceiveCallback(this._onDataReceived.bind(this)); this._backend.setReceiveCallback(this._onMessageReceived.bind(this));
} }
} }