| 1 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|---|
| 2 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|---|
| 3 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|---|
| 4 | function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|---|
| 5 | function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|---|
| 6 | import { log } from "../utils/log.js";
|
|---|
| 7 | var WebSocketClient = /*#__PURE__*/function () {
|
|---|
| 8 | /**
|
|---|
| 9 | * @param {string} url
|
|---|
| 10 | */
|
|---|
| 11 | function WebSocketClient(url) {
|
|---|
| 12 | _classCallCheck(this, WebSocketClient);
|
|---|
| 13 | this.client = new WebSocket(url);
|
|---|
| 14 | this.client.onerror = function (error) {
|
|---|
| 15 | log.error(error);
|
|---|
| 16 | };
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * @param {(...args: any[]) => void} f
|
|---|
| 21 | */
|
|---|
| 22 | _createClass(WebSocketClient, [{
|
|---|
| 23 | key: "onOpen",
|
|---|
| 24 | value: function onOpen(f) {
|
|---|
| 25 | this.client.onopen = f;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * @param {(...args: any[]) => void} f
|
|---|
| 30 | */
|
|---|
| 31 | }, {
|
|---|
| 32 | key: "onClose",
|
|---|
| 33 | value: function onClose(f) {
|
|---|
| 34 | this.client.onclose = f;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | // call f with the message string as the first argument
|
|---|
| 38 | /**
|
|---|
| 39 | * @param {(...args: any[]) => void} f
|
|---|
| 40 | */
|
|---|
| 41 | }, {
|
|---|
| 42 | key: "onMessage",
|
|---|
| 43 | value: function onMessage(f) {
|
|---|
| 44 | this.client.onmessage = function (e) {
|
|---|
| 45 | f(e.data);
|
|---|
| 46 | };
|
|---|
| 47 | }
|
|---|
| 48 | }]);
|
|---|
| 49 | return WebSocketClient;
|
|---|
| 50 | }();
|
|---|
| 51 | export { WebSocketClient as default }; |
|---|