Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

broadcast.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.RippleAPIBroadcast = void 0;
  13. const api_1 = require("./api");
  14. class RippleAPIBroadcast extends api_1.RippleAPI {
  15. constructor(servers, options = {}) {
  16. super(options);
  17. this.ledgerVersion = undefined;
  18. const apis = servers.map((server) => new api_1.RippleAPI(Object.assign({}, options, { server })));
  19. this._apis = apis;
  20. this.getMethodNames().forEach((name) => {
  21. this[name] = function () {
  22. return Promise.race(apis.map((api) => api[name](...arguments)));
  23. };
  24. });
  25. this.connect = function () {
  26. return __awaiter(this, void 0, void 0, function* () {
  27. yield Promise.all(apis.map((api) => api.connect()));
  28. });
  29. };
  30. this.disconnect = function () {
  31. return __awaiter(this, void 0, void 0, function* () {
  32. yield Promise.all(apis.map((api) => api.disconnect()));
  33. });
  34. };
  35. this.isConnected = function () {
  36. return apis.map((api) => api.isConnected()).every(Boolean);
  37. };
  38. const defaultAPI = apis[0];
  39. const syncMethods = ['sign', 'generateAddress', 'computeLedgerHash'];
  40. syncMethods.forEach((name) => {
  41. this[name] = defaultAPI[name].bind(defaultAPI);
  42. });
  43. apis.forEach((api) => {
  44. api.on('ledger', this.onLedgerEvent.bind(this));
  45. api.on('error', (errorCode, errorMessage, data) => this.emit('error', errorCode, errorMessage, data));
  46. });
  47. }
  48. onLedgerEvent(ledger) {
  49. if (ledger.ledgerVersion > this.ledgerVersion ||
  50. this.ledgerVersion == null) {
  51. this.ledgerVersion = ledger.ledgerVersion;
  52. this.emit('ledger', ledger);
  53. }
  54. }
  55. getMethodNames() {
  56. const methodNames = [];
  57. const rippleAPI = this._apis[0];
  58. for (const name of Object.getOwnPropertyNames(rippleAPI)) {
  59. if (typeof rippleAPI[name] === 'function') {
  60. methodNames.push(name);
  61. }
  62. }
  63. return methodNames;
  64. }
  65. }
  66. exports.RippleAPIBroadcast = RippleAPIBroadcast;
  67. //# sourceMappingURL=broadcast.js.map