Browse Source

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'.
master
hristoterezov 8 years ago
parent
commit
96bde3ff44
2 changed files with 42 additions and 39 deletions
  1. 23
    20
      modules/transport/PostMessageTransportBackend.js
  2. 19
    19
      modules/transport/Transport.js

+ 23
- 20
modules/transport/PostMessageTransportBackend.js View File

52
  *
52
  *
53
  * @type {string}
53
  * @type {string}
54
  */
54
  */
55
-const POSTIS_METHOD_NAME = 'data';
55
+const POSTIS_METHOD_NAME = 'message';
56
 
56
 
57
 /**
57
 /**
58
  * Implements message transport using the postMessage API.
58
  * Implements message transport using the postMessage API.
71
         });
71
         });
72
 
72
 
73
         /**
73
         /**
74
-         * If true PostMessageTransportBackend will process and send data using
75
-         * the legacy format and in the same time the current format. Otherwise
76
-         * all data received that is using the legacy format will be ignored and
77
-         * no data with the legacy format will be sent.
74
+         * If true PostMessageTransportBackend will process and send messages
75
+         * using the legacy format and in the same time the current format.
76
+         * Otherwise all messages (outgoing and incoming) that are using the
77
+         * legacy format will be ignored.
78
          *
78
          *
79
          * @type {boolean}
79
          * @type {boolean}
80
          */
80
          */
85
             LEGACY_INCOMING_METHODS.forEach(method =>
85
             LEGACY_INCOMING_METHODS.forEach(method =>
86
                 this.postis.listen(
86
                 this.postis.listen(
87
                     method,
87
                     method,
88
-                    params => this._legacyDataReceiveCallback(method, params)));
88
+                    params =>
89
+                        this._legacyMessageReceivedCallback(method, params)
90
+                )
91
+            );
89
         }
92
         }
90
 
93
 
91
         this._receiveCallback = () => {
94
         this._receiveCallback = () => {
95
 
98
 
96
         this.postis.listen(
99
         this.postis.listen(
97
             POSTIS_METHOD_NAME,
100
             POSTIS_METHOD_NAME,
98
-            data => this._receiveCallback(data));
101
+            message => this._receiveCallback(message));
99
     }
102
     }
100
 
103
 
101
     /**
104
     /**
102
-     * Handles incoming legacy postis data.
105
+     * Handles incoming legacy postis messages.
103
      *
106
      *
104
-     * @param {string} method - The method property from postis data object.
105
-     * @param {Any} params - The params property from postis data object.
107
+     * @param {string} method - The method property from the postis message.
108
+     * @param {Any} params - The params property from the postis message.
106
      * @returns {void}
109
      * @returns {void}
107
      */
110
      */
108
-    _legacyDataReceiveCallback(method, params = {}) {
111
+    _legacyMessageReceivedCallback(method, params = {}) {
109
         this._receiveCallback({
112
         this._receiveCallback({
110
             data: {
113
             data: {
111
                 name: method,
114
                 name: method,
115
     }
118
     }
116
 
119
 
117
     /**
120
     /**
118
-     * Sends the passed data via postis using the old format.
121
+     * Sends the passed message via postis using the old format.
119
      *
122
      *
120
-     * @param {Object} data - The data to be sent.
123
+     * @param {Object} legacyMessage - The message to be sent.
121
      * @returns {void}
124
      * @returns {void}
122
      */
125
      */
123
-    _sendLegacyData({ data, name }) {
126
+    _sendLegacyMessage({ data, name }) {
124
         if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
127
         if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
125
             this.postis.send({
128
             this.postis.send({
126
                 method: name,
129
                 method: name,
139
     }
142
     }
140
 
143
 
141
     /**
144
     /**
142
-     * Sends the passed data.
145
+     * Sends the passed message.
143
      *
146
      *
144
-     * @param {Object} data - The data to be sent.
147
+     * @param {Object} message - The message to be sent.
145
      * @returns {void}
148
      * @returns {void}
146
      */
149
      */
147
-    send(data) {
150
+    send(message) {
148
         this.postis.send({
151
         this.postis.send({
149
             method: POSTIS_METHOD_NAME,
152
             method: POSTIS_METHOD_NAME,
150
-            params: data
153
+            params: message
151
         });
154
         });
152
 
155
 
153
         if (this._enableLegacyFormat) {
156
         if (this._enableLegacyFormat) {
154
             // For the legacy use case we don't need any new fields defined in
157
             // For the legacy use case we don't need any new fields defined in
155
             // Transport class. That's why we are passing only the original
158
             // Transport class. That's why we are passing only the original
156
             // object passed by the consumer of the Transport class which is
159
             // object passed by the consumer of the Transport class which is
157
-            // data.data.
158
-            this._sendLegacyData(data.data);
160
+            // message.data.
161
+            this._sendLegacyMessage(message.data);
159
         }
162
         }
160
     }
163
     }
161
 
164
 

+ 19
- 19
modules/transport/Transport.js View File

72
     }
72
     }
73
 
73
 
74
     /**
74
     /**
75
-     * Handles incoming data from the transport backend.
75
+     * Handles incoming messages from the transport backend.
76
      *
76
      *
77
-     * @param {Object} data - The data.
77
+     * @param {Object} message - The message.
78
      * @returns {void}
78
      * @returns {void}
79
      */
79
      */
80
-    _onDataReceived(data) {
81
-        if (data.type === MESSAGE_TYPE_RESPONSE) {
82
-            const handler = this._responseHandlers.get(data.id);
80
+    _onMessageReceived(message) {
81
+        if (message.type === MESSAGE_TYPE_RESPONSE) {
82
+            const handler = this._responseHandlers.get(message.id);
83
 
83
 
84
             if (handler) {
84
             if (handler) {
85
-                handler(data);
86
-                this._responseHandlers.delete(data.id);
85
+                handler(message);
86
+                this._responseHandlers.delete(message.id);
87
             }
87
             }
88
-        } else if (data.type === MESSAGE_TYPE_REQUEST) {
89
-            this.emit('request', data.data, (result, error) => {
88
+        } else if (message.type === MESSAGE_TYPE_REQUEST) {
89
+            this.emit('request', message.data, (result, error) => {
90
                 this._backend.send({
90
                 this._backend.send({
91
                     type: MESSAGE_TYPE_RESPONSE,
91
                     type: MESSAGE_TYPE_RESPONSE,
92
                     error,
92
                     error,
93
-                    id: data.id,
93
+                    id: message.id,
94
                     result
94
                     result
95
                 });
95
                 });
96
             });
96
             });
97
         } else {
97
         } else {
98
-            this.emit('event', data.data);
98
+            this.emit('event', message.data);
99
         }
99
         }
100
     }
100
     }
101
 
101
 
202
     }
202
     }
203
 
203
 
204
     /**
204
     /**
205
-     * Sends the passed data.
205
+     * Sends the passed event.
206
      *
206
      *
207
-     * @param {Object} data - The data to be sent.
207
+     * @param {Object} event - The event to be sent.
208
      * @returns {void}
208
      * @returns {void}
209
      */
209
      */
210
-    sendEvent(data = {}) {
210
+    sendEvent(event = {}) {
211
         if (this._backend) {
211
         if (this._backend) {
212
             this._backend.send({
212
             this._backend.send({
213
                 type: MESSAGE_TYPE_EVENT,
213
                 type: MESSAGE_TYPE_EVENT,
214
-                data
214
+                data: event
215
             });
215
             });
216
         }
216
         }
217
     }
217
     }
219
     /**
219
     /**
220
      * Sending request.
220
      * Sending request.
221
      *
221
      *
222
-     * @param {Object} data - The data for the request.
222
+     * @param {Object} request - The request to be sent.
223
      * @returns {Promise}
223
      * @returns {Promise}
224
      */
224
      */
225
-    sendRequest(data) {
225
+    sendRequest(request) {
226
         if (!this._backend) {
226
         if (!this._backend) {
227
             return Promise.reject(new Error('No transport backend defined!'));
227
             return Promise.reject(new Error('No transport backend defined!'));
228
         }
228
         }
244
 
244
 
245
             this._backend.send({
245
             this._backend.send({
246
                 type: MESSAGE_TYPE_REQUEST,
246
                 type: MESSAGE_TYPE_REQUEST,
247
-                data,
247
+                data: request,
248
                 id
248
                 id
249
             });
249
             });
250
         });
250
         });
260
         this._disposeBackend();
260
         this._disposeBackend();
261
 
261
 
262
         this._backend = backend;
262
         this._backend = backend;
263
-        this._backend.setReceiveCallback(this._onDataReceived.bind(this));
263
+        this._backend.setReceiveCallback(this._onMessageReceived.bind(this));
264
     }
264
     }
265
 }
265
 }

Loading…
Cancel
Save