UNPKG

diffusion

Version:

Diffusion JavaScript client

57 lines (56 loc) 1.78 kB
"use strict"; /** * @module Ping */ Object.defineProperty(exports, "__esModule", { value: true }); exports.PingImpl = void 0; var Services = require("./../services/ping-services"); /** * Implements the {@link Ping} feature */ var PingImpl = /** @class */ (function () { /** * Create a new PingImpl instance * * @param internal the internal session * @param currentTime a function returning the current time in milliseconds. * Only used for testing */ function PingImpl(internal, currentTime) { if (currentTime === void 0) { currentTime = Date.now; } /** * A function returning the current time in milliseconds * * Only used for testing. Otherwise always `Date.now` */ this.currentTime = Date.now; this.internal = internal; this.currentTime = currentTime; this.ping = internal.getServiceLocator().obtain(Services.USER_PING); } /** * @inheritdoc */ PingImpl.prototype.pingServer = function () { var _this = this; return new Promise(function (resolve, reject) { if (_this.internal.checkConnected(reject)) { var start_1 = _this.currentTime(); _this.ping.send(null, function (err) { if (err) { reject(err); } else { var elapsed = _this.currentTime() - start_1; resolve({ timestamp: start_1, rtt: elapsed }); } }); } }); }; return PingImpl; }()); exports.PingImpl = PingImpl;