You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestManager.js 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const errors_1 = require("../errors");
  4. class RequestManager {
  5. constructor() {
  6. this.nextId = 0;
  7. this.promisesAwaitingResponse = new Map();
  8. }
  9. resolve(id, response) {
  10. const promise = this.promisesAwaitingResponse.get(id);
  11. if (promise == null) {
  12. throw new errors_1.XrplError(`No existing promise with id ${id}`, {
  13. type: 'resolve',
  14. response,
  15. });
  16. }
  17. clearTimeout(promise.timer);
  18. promise.resolve(response);
  19. this.deletePromise(id);
  20. }
  21. reject(id, error) {
  22. const promise = this.promisesAwaitingResponse.get(id);
  23. if (promise == null) {
  24. throw new errors_1.XrplError(`No existing promise with id ${id}`, {
  25. type: 'reject',
  26. error,
  27. });
  28. }
  29. clearTimeout(promise.timer);
  30. promise.reject(error);
  31. this.deletePromise(id);
  32. }
  33. rejectAll(error) {
  34. this.promisesAwaitingResponse.forEach((_promise, id, _map) => {
  35. this.reject(id, error);
  36. this.deletePromise(id);
  37. });
  38. }
  39. createRequest(request, timeout) {
  40. let newId;
  41. if (request.id == null) {
  42. newId = this.nextId;
  43. this.nextId += 1;
  44. }
  45. else {
  46. newId = request.id;
  47. }
  48. const newRequest = JSON.stringify(Object.assign(Object.assign({}, request), { id: newId }));
  49. const timer = setTimeout(() => {
  50. this.reject(newId, new errors_1.TimeoutError(`Timeout for request: ${JSON.stringify(request)} with id ${newId}`, request));
  51. }, timeout);
  52. if (timer.unref) {
  53. ;
  54. timer.unref();
  55. }
  56. if (this.promisesAwaitingResponse.has(newId)) {
  57. clearTimeout(timer);
  58. throw new errors_1.XrplError(`Response with id '${newId}' is already pending`, request);
  59. }
  60. const newPromise = new Promise((resolve, reject) => {
  61. this.promisesAwaitingResponse.set(newId, { resolve, reject, timer });
  62. });
  63. return [newId, newRequest, newPromise];
  64. }
  65. handleResponse(response) {
  66. var _a, _b;
  67. if (response.id == null ||
  68. !(typeof response.id === 'string' || typeof response.id === 'number')) {
  69. throw new errors_1.ResponseFormatError('valid id not found in response', response);
  70. }
  71. if (!this.promisesAwaitingResponse.has(response.id)) {
  72. return;
  73. }
  74. if (response.status == null) {
  75. const error = new errors_1.ResponseFormatError('Response has no status');
  76. this.reject(response.id, error);
  77. }
  78. if (response.status === 'error') {
  79. const errorResponse = response;
  80. const error = new errors_1.RippledError((_a = errorResponse.error_message) !== null && _a !== void 0 ? _a : errorResponse.error, errorResponse);
  81. this.reject(response.id, error);
  82. return;
  83. }
  84. if (response.status !== 'success') {
  85. const error = new errors_1.ResponseFormatError(`unrecognized response.status: ${(_b = response.status) !== null && _b !== void 0 ? _b : ''}`, response);
  86. this.reject(response.id, error);
  87. return;
  88. }
  89. delete response.status;
  90. this.resolve(response.id, response);
  91. }
  92. deletePromise(id) {
  93. this.promisesAwaitingResponse.delete(id);
  94. }
  95. }
  96. exports.default = RequestManager;
  97. //# sourceMappingURL=RequestManager.js.map