Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Transport.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {
  2. MESSAGE_TYPE_EVENT,
  3. MESSAGE_TYPE_RESPONSE,
  4. MESSAGE_TYPE_REQUEST
  5. } from './constants';
  6. /**
  7. * Stores the currnet transport that have to be used.
  8. */
  9. export default class Transport {
  10. /**
  11. * Creates new instance.
  12. *
  13. * @param {Object} options - Optional parameters for configuration of the
  14. * transport.
  15. */
  16. constructor(options = {}) {
  17. const { transport } = options;
  18. this._requestID = 0;
  19. this._responseHandlers = new Map();
  20. this._listeners = new Map();
  21. this._unprocessedMessages = new Set();
  22. this.addListener = this.on;
  23. if (transport) {
  24. this.setTransport(transport);
  25. }
  26. }
  27. /**
  28. * Disposes the current transport.
  29. *
  30. * @returns {void}
  31. */
  32. _disposeTransport() {
  33. if (this._transport) {
  34. this._transport.dispose();
  35. this._transport = null;
  36. }
  37. }
  38. /**
  39. * Handles incomming data from the transport.
  40. *
  41. * @param {Object} data - The data.
  42. * @returns {void}
  43. */
  44. _onDataReceived(data) {
  45. if (data.type === MESSAGE_TYPE_RESPONSE) {
  46. const handler = this._responseHandlers.get(data.id);
  47. if (handler) {
  48. handler(data);
  49. this._responseHandlers.delete(data.id);
  50. }
  51. return;
  52. }
  53. if (data.type === MESSAGE_TYPE_REQUEST) {
  54. this.emit('request', data.data, (result, error) => {
  55. this._transport.send({
  56. type: MESSAGE_TYPE_RESPONSE,
  57. result,
  58. error,
  59. id: data.id
  60. });
  61. });
  62. } else {
  63. this.emit('event', data.data);
  64. }
  65. }
  66. /**
  67. * Disposes the allocated resources.
  68. *
  69. * @returns {void}
  70. */
  71. dispose() {
  72. this._responseHandlers.clear();
  73. this._unprocessedMessages.clear();
  74. this.removeAllListeners();
  75. this._disposeTransport();
  76. }
  77. /**
  78. * Calls each of the listeners registered for the event named eventName, in
  79. * the order they were registered, passing the supplied arguments to each.
  80. *
  81. * @param {string} eventName - The name of the event.
  82. * @returns {boolean} True if the event had listeners, false otherwise.
  83. */
  84. emit(eventName, ...args) {
  85. const listenersForEvent = this._listeners.get(eventName);
  86. if (!listenersForEvent || listenersForEvent.size === 0) {
  87. this._unprocessedMessages.add(args);
  88. return false;
  89. }
  90. let isProcessed = false;
  91. listenersForEvent.forEach(listener => {
  92. isProcessed = listener(...args) || isProcessed;
  93. });
  94. if (!isProcessed) {
  95. this._unprocessedMessages.add(args);
  96. }
  97. }
  98. /**
  99. * Adds the listener function to the listeners collection for the event
  100. * named eventName.
  101. *
  102. * @param {string} eventName - The name of the event.
  103. * @param {Function} listener - The listener that will be added.
  104. * @returns {Transport} References to the instance of Transport class, so
  105. * that calls can be chained.
  106. */
  107. on(eventName, listener) {
  108. let listenersForEvent = this._listeners.get(eventName);
  109. if (!listenersForEvent) {
  110. listenersForEvent = new Set();
  111. this._listeners.set(eventName, listenersForEvent);
  112. }
  113. listenersForEvent.add(listener);
  114. this._unprocessedMessages.forEach(args => {
  115. if (listener(...args)) {
  116. this._unprocessedMessages.delete(args);
  117. }
  118. });
  119. return this;
  120. }
  121. /**
  122. * Removes all listeners, or those of the specified eventName.
  123. *
  124. * @param {string} [eventName] - The name of the event.
  125. * @returns {Transport} References to the instance of Transport class, so
  126. * that calls can be chained.
  127. */
  128. removeAllListeners(eventName) {
  129. if (eventName) {
  130. this._listeners.delete(eventName);
  131. } else {
  132. this._listeners.clear();
  133. }
  134. return this;
  135. }
  136. /**
  137. * Removes the listener function from the listeners collection for the event
  138. * named eventName.
  139. *
  140. * @param {string} eventName - The name of the event.
  141. * @param {Function} listener - The listener that will be removed.
  142. * @returns {Transport} References to the instance of Transport class, so
  143. * that calls can be chained.
  144. */
  145. removeListener(eventName, listener) {
  146. const listenersForEvent = this._listeners.get(eventName);
  147. if (listenersForEvent) {
  148. listenersForEvent.delete(listener);
  149. }
  150. return this;
  151. }
  152. /**
  153. * Sends the passed data.
  154. *
  155. * @param {Object} data - The data to be sent.
  156. * @returns {void}
  157. */
  158. sendEvent(data = {}) {
  159. if (this._transport) {
  160. this._transport.send({
  161. type: MESSAGE_TYPE_EVENT,
  162. data
  163. });
  164. }
  165. }
  166. /**
  167. * Sending request.
  168. *
  169. * @param {Object} data - The data for the request.
  170. * @returns {Promise}
  171. */
  172. sendRequest(data) {
  173. if (!this._transport) {
  174. return Promise.reject(new Error('No transport defined!'));
  175. }
  176. this._requestID++;
  177. const id = this._requestID;
  178. return new Promise((resolve, reject) => {
  179. this._responseHandlers.set(this._requestID, response => {
  180. const { result, error } = response;
  181. if (result) {
  182. resolve(result);
  183. } else if (error) {
  184. reject(error);
  185. } else { // no response
  186. reject(new Error('Unexpected response format!'));
  187. }
  188. });
  189. this._transport.send({
  190. id,
  191. type: MESSAGE_TYPE_REQUEST,
  192. data
  193. });
  194. });
  195. }
  196. /**
  197. * Changes the current transport.
  198. *
  199. * @param {Object} transport - The new transport that will be used.
  200. * @returns {void}
  201. */
  202. setTransport(transport) {
  203. this._disposeTransport();
  204. this._transport = transport;
  205. this._transport.setDataReceivedCallback(
  206. this._onDataReceived.bind(this));
  207. }
  208. }