4a6d7ae38a3b4ac033149c2851232bef0324ce4f1fd2c94bea85b14ee592d882.json raw

   1  {"ast":null,"code":"var __assign = this && this.__assign || function () {\n  __assign = Object.assign || function (t) {\n    for (var s, i = 1, n = arguments.length; i < n; i++) {\n      s = arguments[i];\n      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\n    }\n    return t;\n  };\n  return __assign.apply(this, arguments);\n};\nvar __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {\n  function adopt(value) {\n    return value instanceof P ? value : new P(function (resolve) {\n      resolve(value);\n    });\n  }\n  return new (P || (P = Promise))(function (resolve, reject) {\n    function fulfilled(value) {\n      try {\n        step(generator.next(value));\n      } catch (e) {\n        reject(e);\n      }\n    }\n    function rejected(value) {\n      try {\n        step(generator[\"throw\"](value));\n      } catch (e) {\n        reject(e);\n      }\n    }\n    function step(result) {\n      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);\n    }\n    step((generator = generator.apply(thisArg, _arguments || [])).next());\n  });\n};\nvar __generator = this && this.__generator || function (thisArg, body) {\n  var _ = {\n      label: 0,\n      sent: function () {\n        if (t[0] & 1) throw t[1];\n        return t[1];\n      },\n      trys: [],\n      ops: []\n    },\n    f,\n    y,\n    t,\n    g;\n  return g = {\n    next: verb(0),\n    \"throw\": verb(1),\n    \"return\": verb(2)\n  }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function () {\n    return this;\n  }), g;\n  function verb(n) {\n    return function (v) {\n      return step([n, v]);\n    };\n  }\n  function step(op) {\n    if (f) throw new TypeError(\"Generator is already executing.\");\n    while (g && (g = 0, op[0] && (_ = 0)), _) try {\n      if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n      if (y = 0, t) op = [op[0] & 2, t.value];\n      switch (op[0]) {\n        case 0:\n        case 1:\n          t = op;\n          break;\n        case 4:\n          _.label++;\n          return {\n            value: op[1],\n            done: false\n          };\n        case 5:\n          _.label++;\n          y = op[1];\n          op = [0];\n          continue;\n        case 7:\n          op = _.ops.pop();\n          _.trys.pop();\n          continue;\n        default:\n          if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {\n            _ = 0;\n            continue;\n          }\n          if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {\n            _.label = op[1];\n            break;\n          }\n          if (op[0] === 6 && _.label < t[1]) {\n            _.label = t[1];\n            t = op;\n            break;\n          }\n          if (t && _.label < t[2]) {\n            _.label = t[2];\n            _.ops.push(op);\n            break;\n          }\n          if (t[2]) _.ops.pop();\n          _.trys.pop();\n          continue;\n      }\n      op = body.call(thisArg, _);\n    } catch (e) {\n      op = [6, e];\n      y = 0;\n    } finally {\n      f = t = 0;\n    }\n    if (op[0] & 5) throw op[1];\n    return {\n      value: op[0] ? op[1] : void 0,\n      done: true\n    };\n  }\n};\nimport { MessageQueue } from './utils';\nimport { getWebSocketImpl } from './ws';\nvar ConnectionManager = /** @class */function () {\n  function ConnectionManager() {\n    this.connectionMap = new Map();\n  }\n  ConnectionManager.getInstance = function () {\n    if (!ConnectionManager.instace) {\n      ConnectionManager.instace = new ConnectionManager();\n    }\n    return ConnectionManager.instace;\n  };\n  ConnectionManager.prototype.getConnection = function (url) {\n    if (this.connectionMap.has(url)) {\n      return this.connectionMap.get(url);\n    }\n    var newConn = new WSConnection(url);\n    this.connectionMap.set(url, newConn);\n    return newConn;\n  };\n  return ConnectionManager;\n}();\nexport { ConnectionManager };\nvar WSConnection = /** @class */function () {\n  function WSConnection(url) {\n    this.subListeners = {};\n    this.rpcListeners = {};\n    this.rpcId = 0;\n    this._WS = getWebSocketImpl();\n    this.url = new URL(url);\n    this.messageQueue = new MessageQueue();\n  }\n  WSConnection.prototype.connect = function () {\n    var _this = this;\n    if (!this.connectionPromise) {\n      this.connectionPromise = new Promise(function (res, rej) {\n        try {\n          _this.ws = new _this._WS(_this.url);\n        } catch (err) {\n          rej(err);\n          return;\n        }\n        _this.ws.onopen = function () {\n          res();\n        };\n        _this.ws.onerror = function () {\n          rej(new Error('Failed to open WebSocket'));\n        };\n        _this.ws.onmessage = function (e) {\n          _this.messageQueue.enqueue(e.data);\n          if (!_this.handlingInterval) {\n            _this.handlingInterval = setInterval(_this.handleNextMesage.bind(_this), 0);\n          }\n        };\n        _this.ws.onclose = function () {\n          _this.connectionPromise = undefined;\n        };\n      });\n    }\n    return this.connectionPromise;\n  };\n  WSConnection.prototype.sendRequest = function (method, params) {\n    var _a, _b;\n    if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) !== 1) {\n      throw new Error('Socket not open...');\n    }\n    var id = this.rpcId;\n    this.rpcId++;\n    var message = JSON.stringify({\n      jsonrpc: '2.0',\n      method: method,\n      params: params,\n      id: id\n    });\n    (_b = this.ws) === null || _b === void 0 ? void 0 : _b.send(message);\n  };\n  WSConnection.prototype.closeSubscription = function (subId) {\n    var _a;\n    (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(['CLOSE', subId]));\n  };\n  WSConnection.prototype.addSubListener = function (subId, callback) {\n    (this.subListeners[subId] = this.subListeners[subId] || []).push(callback);\n  };\n  //TODO: Move to RPCManagerClass\n  WSConnection.prototype.addRpcListener = function (callback, errorCallback, id) {\n    this.rpcListeners[id] = {\n      callback: callback,\n      errorCallback: errorCallback\n    };\n  };\n  //TODO: Move to RPCManagerClass\n  WSConnection.prototype.removeRpcListener = function (id) {\n    delete this.rpcListeners[id];\n  };\n  WSConnection.prototype.removeListener = function (subId, callback) {\n    if (this.subListeners[subId].length === 1) {\n      delete this.subListeners[subId];\n      return;\n    }\n    this.subListeners[subId] = this.subListeners[subId].filter(function (fn) {\n      return fn !== callback;\n    });\n  };\n  WSConnection.prototype.ensureConnection = function () {\n    var _a;\n    return __awaiter(this, void 0, void 0, function () {\n      return __generator(this, function (_b) {\n        switch (_b.label) {\n          case 0:\n            if (!(((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) !== 1)) return [3 /*break*/, 2];\n            return [4 /*yield*/, this.connect()];\n          case 1:\n            _b.sent();\n            _b.label = 2;\n          case 2:\n            return [2 /*return*/];\n        }\n      });\n    });\n  };\n  WSConnection.prototype.handleNextMesage = function () {\n    var _a;\n    if (this.messageQueue.size === 0) {\n      clearInterval(this.handlingInterval);\n      this.handlingInterval = undefined;\n      return;\n    }\n    var message = this.messageQueue.dequeue();\n    var parsed;\n    try {\n      parsed = JSON.parse(message);\n      if ('result' in parsed && parsed.id != undefined) {\n        if (this.rpcListeners[parsed.id]) {\n          this.rpcListeners[parsed.id].callback();\n          this.removeRpcListener(parsed.id);\n        }\n      } else if ('error' in parsed && parsed.id != undefined) {\n        if (this.rpcListeners[parsed.id]) {\n          this.rpcListeners[parsed.id].errorCallback(parsed.error);\n          this.removeRpcListener(parsed.id);\n        }\n      } else if ('method' in parsed) {\n        if ('id' in parsed) {\n          // Do nothing as mints should not send requests\n        } else {\n          var subId = parsed.params.subId;\n          if (!subId) {\n            return;\n          }\n          if (((_a = this.subListeners[subId]) === null || _a === void 0 ? void 0 : _a.length) > 0) {\n            var notification_1 = parsed;\n            this.subListeners[subId].forEach(function (cb) {\n              return cb(notification_1.params.payload);\n            });\n          }\n        }\n      }\n    } catch (e) {\n      console.error(e);\n      return;\n    }\n  };\n  WSConnection.prototype.createSubscription = function (params, callback, errorCallback) {\n    var _this = this;\n    var _a;\n    if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) !== 1) {\n      return errorCallback(new Error('Socket is not open'));\n    }\n    var subId = (Math.random() + 1).toString(36).substring(7);\n    this.addRpcListener(function () {\n      _this.addSubListener(subId, callback);\n    }, function (e) {\n      errorCallback(new Error(e.message));\n    }, this.rpcId);\n    this.sendRequest('subscribe', __assign(__assign({}, params), {\n      subId: subId\n    }));\n    this.rpcId++;\n    return subId;\n  };\n  WSConnection.prototype.cancelSubscription = function (subId, callback) {\n    this.removeListener(subId, callback);\n    this.rpcId++;\n    this.sendRequest('unsubscribe', {\n      subId: subId\n    });\n  };\n  Object.defineProperty(WSConnection.prototype, \"activeSubscriptions\", {\n    get: function () {\n      return Object.keys(this.subListeners);\n    },\n    enumerable: false,\n    configurable: true\n  });\n  WSConnection.prototype.close = function () {\n    var _a;\n    if (this.ws) {\n      (_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();\n    }\n  };\n  return WSConnection;\n}();\nexport { WSConnection };\n//# sourceMappingURL=WSConnection.js.map","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}