UNPKG

@pubby/sdk

Version:
128 lines (125 loc) 5.57 kB
import { __extends, __awaiter, __generator, __spread, __read } from 'tslib'; import io from 'socket.io-client'; import { TimeSyncRequest } from './outgoing/time-sync.event.js'; import { ALL_EVENTS } from './incoming/all-events.js'; import { performance } from '../lib/polyfills.js'; import '../lib/streams/index.js'; import '../lib/streams/operators/index.js'; import { filter } from '../lib/streams/operators/filter.js'; import { PubbyStream } from '../lib/streams/lib/stream.js'; var TIME_SYNC_INTERVAL = 60 * 5000; var WsStream = /** @class */ (function (_super) { __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 __awaiter(_this, void 0, void 0, function () { var ticket; var _this = this; return __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(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 = ALL_EVENTS.get(event); if (MessageConstructor) { _super.prototype.add.call(_this, new (MessageConstructor.bind.apply(MessageConstructor, __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(function (d) { return d instanceof type; })); }; WsStream.prototype._write = function (message, cb) { var _a; (_a = this.io).emit.apply(_a, __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 = performance.now(); _this.add(new TimeSyncRequest(Date.now()), function (_, _a) { var _b = __read(_a, 1), timeOffset = _b[0]; var delay = (performance.now() - start) / 2; _this._timeOffset = timeOffset - delay; resolve(); }); }); }; return WsStream; }(PubbyStream)); export { WsStream };