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.

BroadcastClient.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. const _1 = require(".");
  13. class BroadcastClient extends _1.Client {
  14. constructor(servers, options = {}) {
  15. super(servers[0], options);
  16. const clients = servers.map((server) => new _1.Client(server, options));
  17. this.clients = clients;
  18. this.getMethodNames().forEach((name) => {
  19. this[name] = (...args) => __awaiter(this, void 0, void 0, function* () { return Promise.race(clients.map((client) => __awaiter(this, void 0, void 0, function* () { return client[name](...args); }))); });
  20. });
  21. this.connect = () => __awaiter(this, void 0, void 0, function* () {
  22. yield Promise.all(clients.map((client) => __awaiter(this, void 0, void 0, function* () { return client.connect(); })));
  23. });
  24. this.disconnect = () => __awaiter(this, void 0, void 0, function* () {
  25. yield Promise.all(clients.map((client) => __awaiter(this, void 0, void 0, function* () { return client.disconnect(); })));
  26. });
  27. this.isConnected = () => clients.map((client) => client.isConnected()).every(Boolean);
  28. clients.forEach((client) => {
  29. client.on('error', (errorCode, errorMessage, data) => this.emit('error', errorCode, errorMessage, data));
  30. });
  31. }
  32. getMethodNames() {
  33. const methodNames = [];
  34. const firstClient = this.clients[0];
  35. const methods = Object.getOwnPropertyNames(firstClient);
  36. methods.push(...Object.getOwnPropertyNames(Object.getPrototypeOf(firstClient)));
  37. for (const name of methods) {
  38. if (typeof firstClient[name] === 'function' &&
  39. name !== 'constructor' &&
  40. name !== 'on') {
  41. methodNames.push(name);
  42. }
  43. }
  44. return methodNames;
  45. }
  46. }
  47. exports.default = BroadcastClient;
  48. //# sourceMappingURL=BroadcastClient.js.map