UNPKG

@pubby/sdk

Version:
136 lines (129 loc) 5.89 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib = require('tslib'); var io = require('socket.io-client'); var timeSync_event = require('./outgoing/time-sync.event.js'); var allEvents = require('./incoming/all-events.js'); var polyfills = require('../lib/polyfills.js'); require('../lib/streams/index.js'); require('../lib/streams/operators/index.js'); var filter = require('../lib/streams/operators/filter.js'); var stream = require('../lib/streams/lib/stream.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var io__default = /*#__PURE__*/_interopDefaultLegacy(io); var TIME_SYNC_INTERVAL = 60 * 5000; var WsStream = /** @class */ (function (_super) { tslib.__extends(WsStream, _super); function WsStream(url, pubby) { var _this = _super.call(this) || this; _this.url = url; _this.pubby = pubby; _this._timeOffset = 0; return _this; } WsStream.prototype.connect = function (reconnect) { var _this = this; if (reconnect === void 0) { reconnect = false; } // eslint-disable-next-line no-async-promise-executor return new Promise(function (resolve, reject) { return tslib.__awaiter(_this, void 0, void 0, function () { var ticket; var _this = this; return tslib.__generator(this, function (_a) { switch (_a.label) { case 0: if (this.io) { if (this.io.connected) { return [2 /*return*/, resolve()]; } this.io.removeAllListeners(); } return [4 /*yield*/, this.pubby.auth.strategy.getTicket()]; case 1: ticket = _a.sent(); this.io = io__default["default"](this.url, { reconnection: true, query: { reconnect: String(reconnect), ticket: ticket, }, }); // Handle connection events this.io.once("connect", function () { // Envia o pacote de sincronização _this.syncTime().then(resolve); // Inicia a sincronização de tempo clearInterval(_this.syncTimer); _this.syncTimer = setInterval(function () { return _this.syncTime(); }, TIME_SYNC_INTERVAL); }); this.io.once("error", reject); this.io.once("connect_error", function (err) { console.log("Failed to connect"); reject(err); }); this.io.once("disconnect", function () { _this.error(new Error("disconnected")); }); this.io.once("force_disconnect", function (reason) { _this.error(new Error("forced_disconnected: " + reason)); }); this.io.on("exception", function (err) { return _this.error(err); }); // Handle stream events this.io.onAny(function (event, data) { var MessageConstructor = allEvents.ALL_EVENTS.get(event); if (MessageConstructor) { _super.prototype.add.call(_this, new (MessageConstructor.bind.apply(MessageConstructor, tslib.__spread([void 0], [].concat(data))))()); } }); return [2 /*return*/]; } }); }); }); }; WsStream.prototype.add = function (data, cb) { if (data !== null) { this._write(data, cb); } return this; }; WsStream.prototype.asyncAdd = function (data) { var _this = this; return new Promise(function (resolve, reject) { _this.add(data, function (err, data) { return (err ? reject(err) : resolve(data)); }); }); }; WsStream.prototype.listen = function (type) { return this.pipe(filter.filter(function (d) { return d instanceof type; })); }; WsStream.prototype._write = function (message, cb) { var _a; (_a = this.io).emit.apply(_a, tslib.__spread([message.constructor.id], message.toMessage(), [cb])); }; Object.defineProperty(WsStream.prototype, "timeOffset", { get: function () { return this._timeOffset; }, enumerable: false, configurable: true }); Object.defineProperty(WsStream.prototype, "now", { get: function () { return Date.now() - this.timeOffset; }, enumerable: false, configurable: true }); WsStream.prototype.syncTime = function () { var _this = this; return new Promise(function (resolve) { var start = polyfills.performance.now(); _this.add(new timeSync_event.TimeSyncRequest(Date.now()), function (_, _a) { var _b = tslib.__read(_a, 1), timeOffset = _b[0]; var delay = (polyfills.performance.now() - start) / 2; _this._timeOffset = timeOffset - delay; resolve(); }); }); }; return WsStream; }(stream.PubbyStream)); exports.WsStream = WsStream;