Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@folklore/clock

Package Overview
Dependencies
Maintainers
3
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@folklore/clock - npm Package Compare versions

Comparing version 0.1.16 to 0.1.17

371

dist/cjs.js

@@ -5,9 +5,2 @@ 'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
var _inherits = require('@babel/runtime/helpers/inherits');
var _possibleConstructorReturn = require('@babel/runtime/helpers/possibleConstructorReturn');
var _getPrototypeOf = require('@babel/runtime/helpers/getPrototypeOf');
var EventEmitter = require('wolfy87-eventemitter');

@@ -19,25 +12,14 @@ var createDebug = require('debug');

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var _assertThisInitialized__default = /*#__PURE__*/_interopDefaultLegacy(_assertThisInitialized);
var _inherits__default = /*#__PURE__*/_interopDefaultLegacy(_inherits);
var _possibleConstructorReturn__default = /*#__PURE__*/_interopDefaultLegacy(_possibleConstructorReturn);
var _getPrototypeOf__default = /*#__PURE__*/_interopDefaultLegacy(_getPrototypeOf);
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
var createDebug__default = /*#__PURE__*/_interopDefaultLegacy(createDebug);
function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
// Thanks http://stackoverflow.com/questions/1638337/the-best-way-to-synchronize-client-side-javascript-clock-with-server-date
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
const getTime = () => new Date().getTime();
var getTime = function getTime() {
return new Date().getTime();
};
var getServerTime = function getServerTime(url, opts) {
var options = _objectSpread$1({
const getServerTime = (url, opts) => {
const options = {
urlFormat: '{url}?time={timestamp}',
parseResponse: function parseResponse(response) {
var responseParts = response.split(',');
parseResponse: response => {
const responseParts = response.split(',');
return {

@@ -47,20 +29,18 @@ serverTimestamp: parseInt(responseParts[0], 10),

};
}
}, Object.keys(opts || {}).reduce(function (finalOptions, key) {
return typeof opts[key] !== 'undefined' && opts[key] !== null ? _objectSpread$1(_objectSpread$1({}, finalOptions), {}, _defineProperty__default["default"]({}, key, opts[key])) : finalOptions;
}, {}));
var clientTimestamp = getTime();
var clockUrl = options.urlFormat.replace(/\{\s*url\s*\}/, url).replace(/\{\s*timestamp\s*\}/, clientTimestamp);
},
...Object.keys(opts || {}).reduce((finalOptions, key) => typeof opts[key] !== 'undefined' && opts[key] !== null ? { ...finalOptions,
[key]: opts[key]
} : finalOptions, {})
};
const clientTimestamp = getTime();
const clockUrl = options.urlFormat.replace(/\{\s*url\s*\}/, url).replace(/\{\s*timestamp\s*\}/, clientTimestamp);
return fetch(clockUrl, {
method: 'GET'
}).then(function (response) {
return response.text();
}).then(function (response) {
var nowTimeStamp = getTime();
}).then(response => response.text()).then(response => {
const nowTimeStamp = getTime();
const {
serverTimestamp,
serverClientRequestDiffTime
} = options.parseResponse(response);
var _options$parseRespons = options.parseResponse(response),
serverTimestamp = _options$parseRespons.serverTimestamp,
serverClientRequestDiffTime = _options$parseRespons.serverClientRequestDiffTime;
if (typeof serverTimestamp === 'undefined' || typeof serverClientRequestDiffTime === 'undefined' || Number.isNaN(serverTimestamp) || Number.isNaN(serverClientRequestDiffTime)) {

@@ -70,4 +50,4 @@ throw new Error('Bad response');

var serverClientResponseDiffTime = nowTimeStamp - serverTimestamp;
var responseTime = (serverClientRequestDiffTime - nowTimeStamp + (clientTimestamp - serverClientResponseDiffTime)) / 2;
const serverClientResponseDiffTime = nowTimeStamp - serverTimestamp;
const responseTime = (serverClientRequestDiffTime - nowTimeStamp + (clientTimestamp - serverClientResponseDiffTime)) / 2;
return nowTimeStamp + responseTime;

@@ -77,23 +57,13 @@ });

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
const debug = createDebug__default["default"]('folklore:clock');
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
class Clock extends EventEmitter__default["default"] {
static getUTCTime(date) {
const realDate = date || new Date();
return realDate.getTime();
}
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var debug = createDebug__default["default"]('folklore:clock');
var Clock = /*#__PURE__*/function (_EventEmitter) {
_inherits__default["default"](Clock, _EventEmitter);
var _super = _createSuper(Clock);
function Clock(opts) {
var _this;
_classCallCheck__default["default"](this, Clock);
_this = _super.call(this);
_this.options = _objectSpread({
constructor(opts) {
super();
this.options = {
time: null,

@@ -105,189 +75,166 @@ updateInterval: 10,

serverParseResponse: null,
syncCount: 5
}, opts);
_this.onUpdate = _this.onUpdate.bind(_assertThisInitialized__default["default"](_this));
_this.startTime = Clock.getUTCTime();
_this.currentTime = _this.startTime;
_this.customStartTime = _this.options.time || _this.startTime;
_this.time = _this.customStartTime;
_this.serverOffset = 0;
_this.started = false;
_this.shouldStart = false;
_this.server = null;
_this.interval = null;
syncCount: 5,
...opts
};
this.onUpdate = this.onUpdate.bind(this);
this.startTime = Clock.getUTCTime();
this.currentTime = this.startTime;
this.customStartTime = this.options.time || this.startTime;
this.time = this.customStartTime;
this.serverOffset = 0;
this.started = false;
this.shouldStart = false;
this.server = null;
this.interval = null;
if (_this.options.server !== null) {
_this.ready = false;
_this.setServer(_this.options.server);
if (this.options.server !== null) {
this.ready = false;
this.setServer(this.options.server);
} else {
_this.ready = true;
_this.emit('ready');
this.ready = true;
this.emit('ready');
}
if (_this.options.autoStart) {
_this.start();
if (this.options.autoStart) {
this.start();
}
}
return _this;
setTime(time) {
this.startTime = Clock.getUTCTime();
this.customStartTime = time;
}
_createClass__default["default"](Clock, [{
key: "setTime",
value: function setTime(time) {
this.startTime = Clock.getUTCTime();
this.customStartTime = time;
setServer(server) {
debug("Setting server to: ".concat(server));
this.ready = false;
this.server = server;
const wasStarted = this.started;
if (wasStarted) {
debug('Stopping time whil syncing.');
this.stop();
}
}, {
key: "setServer",
value: function setServer(server) {
var _this2 = this;
debug("Setting server to: ".concat(server));
this.ready = false;
this.server = server;
var wasStarted = this.started;
return this.sync().then(() => {
this.ready = true;
this.emit('ready');
if (wasStarted) {
debug('Stopping time whil syncing.');
this.stop();
if (wasStarted || this.shouldStart) {
debug('Starting back...');
this.start();
}
});
}
return this.sync().then(function () {
_this2.ready = true;
sync() {
debug("Syncing with server: ".concat(this.server));
const {
syncCount,
serverUrlFormat,
serverParseResponse
} = this.options;
const promises = [];
_this2.emit('ready');
if (wasStarted || _this2.shouldStart) {
debug('Starting back...');
_this2.start();
}
for (let i = 0; i < syncCount; i += 1) {
const promise = getServerTime(this.server, {
urlFormat: serverUrlFormat,
parseResponse: serverParseResponse
}).then(time => {
const clientTime = new Date().getTime();
return {
server: time,
client: clientTime
};
});
promises.push(promise);
}
}, {
key: "sync",
value: function sync() {
var _this3 = this;
debug("Syncing with server: ".concat(this.server));
var _this$options = this.options,
syncCount = _this$options.syncCount,
serverUrlFormat = _this$options.serverUrlFormat,
serverParseResponse = _this$options.serverParseResponse;
var promises = [];
return Promise.all(promises).then(times => {
const timesCount = times.length;
const avgOffset = times.reduce((total, _ref) => {
let {
client,
server
} = _ref;
return total + (client - server);
}, 0) / timesCount;
const avgTime = times.reduce((total, _ref2) => {
let {
server
} = _ref2;
return total + server;
}, 0) / timesCount;
this.serverOffset = avgOffset;
this.setTime(avgTime);
debug("Time synced with server. Offset: ".concat(this.serverOffset));
this.emit('synced', avgTime);
});
}
for (var i = 0; i < syncCount; i += 1) {
var promise = getServerTime(this.server, {
urlFormat: serverUrlFormat,
parseResponse: serverParseResponse
}).then(function (time) {
var clientTime = new Date().getTime();
return {
server: time,
client: clientTime
};
});
promises.push(promise);
}
getOffset() {
return this.serverOffset;
}
return Promise.all(promises).then(function (times) {
var timesCount = times.length;
var avgOffset = times.reduce(function (total, _ref) {
var client = _ref.client,
server = _ref.server;
return total + (client - server);
}, 0) / timesCount;
var avgTime = times.reduce(function (total, _ref2) {
var server = _ref2.server;
return total + server;
}, 0) / timesCount;
_this3.serverOffset = avgOffset;
start() {
if (this.started) {
return;
}
_this3.setTime(avgTime);
if (!this.ready) {
this.shouldStart = true;
debug('Not ready, waiting to start...');
return;
}
debug("Time synced with server. Offset: ".concat(_this3.serverOffset));
debug('Starting...');
this.started = true;
const {
updateInterval
} = this.options;
_this3.emit('synced', avgTime);
});
if (updateInterval !== null && updateInterval > 0) {
this.interval = setInterval(this.onUpdate, updateInterval);
}
}, {
key: "getOffset",
value: function getOffset() {
return this.serverOffset;
}
stop() {
if (!this.started) {
return;
}
}, {
key: "start",
value: function start() {
if (this.started) {
return;
}
if (!this.ready) {
this.shouldStart = true;
debug('Not ready, waiting to start...');
return;
}
this.shouldStart = false;
this.started = false;
debug('Starting...');
this.started = true;
var updateInterval = this.options.updateInterval;
if (updateInterval !== null && updateInterval > 0) {
this.interval = setInterval(this.onUpdate, updateInterval);
}
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}, {
key: "stop",
value: function stop() {
if (!this.started) {
return;
}
}
this.shouldStart = false;
this.started = false;
getTime() {
this.update();
return this.time;
}
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
}, {
key: "getTime",
value: function getTime() {
this.update();
return this.time;
}
}, {
key: "update",
value: function update() {
this.currentTime = Clock.getUTCTime();
var currentDelta = this.currentTime - this.startTime;
var time = this.customStartTime + currentDelta;
var changed = time !== this.time;
this.time = time;
update() {
this.currentTime = Clock.getUTCTime();
const currentDelta = this.currentTime - this.startTime;
const time = this.customStartTime + currentDelta;
const changed = time !== this.time;
this.time = time;
if (changed) {
this.emit('change', this.time);
}
if (changed) {
this.emit('change', this.time);
}
}, {
key: "onUpdate",
value: function onUpdate() {
this.update();
}
}], [{
key: "getUTCTime",
value: function getUTCTime(date) {
var realDate = date || new Date();
return realDate.getTime();
}
}]);
}
return Clock;
}(EventEmitter__default["default"]);
onUpdate() {
this.update();
}
}
exports.Clock = Clock;
exports["default"] = Clock;
exports.getServerTime = getServerTime;

@@ -1,8 +0,1 @@

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
import _inherits from '@babel/runtime/helpers/inherits';
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
import EventEmitter from 'wolfy87-eventemitter';

@@ -12,15 +5,11 @@ import createDebug from 'debug';

function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
// Thanks http://stackoverflow.com/questions/1638337/the-best-way-to-synchronize-client-side-javascript-clock-with-server-date
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
const getTime = () => new Date().getTime();
var getTime = function getTime() {
return new Date().getTime();
};
var getServerTime = function getServerTime(url, opts) {
var options = _objectSpread$1({
const getServerTime = (url, opts) => {
const options = {
urlFormat: '{url}?time={timestamp}',
parseResponse: function parseResponse(response) {
var responseParts = response.split(',');
parseResponse: response => {
const responseParts = response.split(',');
return {

@@ -30,20 +19,18 @@ serverTimestamp: parseInt(responseParts[0], 10),

};
}
}, Object.keys(opts || {}).reduce(function (finalOptions, key) {
return typeof opts[key] !== 'undefined' && opts[key] !== null ? _objectSpread$1(_objectSpread$1({}, finalOptions), {}, _defineProperty({}, key, opts[key])) : finalOptions;
}, {}));
var clientTimestamp = getTime();
var clockUrl = options.urlFormat.replace(/\{\s*url\s*\}/, url).replace(/\{\s*timestamp\s*\}/, clientTimestamp);
},
...Object.keys(opts || {}).reduce((finalOptions, key) => typeof opts[key] !== 'undefined' && opts[key] !== null ? { ...finalOptions,
[key]: opts[key]
} : finalOptions, {})
};
const clientTimestamp = getTime();
const clockUrl = options.urlFormat.replace(/\{\s*url\s*\}/, url).replace(/\{\s*timestamp\s*\}/, clientTimestamp);
return fetch(clockUrl, {
method: 'GET'
}).then(function (response) {
return response.text();
}).then(function (response) {
var nowTimeStamp = getTime();
}).then(response => response.text()).then(response => {
const nowTimeStamp = getTime();
const {
serverTimestamp,
serverClientRequestDiffTime
} = options.parseResponse(response);
var _options$parseRespons = options.parseResponse(response),
serverTimestamp = _options$parseRespons.serverTimestamp,
serverClientRequestDiffTime = _options$parseRespons.serverClientRequestDiffTime;
if (typeof serverTimestamp === 'undefined' || typeof serverClientRequestDiffTime === 'undefined' || Number.isNaN(serverTimestamp) || Number.isNaN(serverClientRequestDiffTime)) {

@@ -53,4 +40,4 @@ throw new Error('Bad response');

var serverClientResponseDiffTime = nowTimeStamp - serverTimestamp;
var responseTime = (serverClientRequestDiffTime - nowTimeStamp + (clientTimestamp - serverClientResponseDiffTime)) / 2;
const serverClientResponseDiffTime = nowTimeStamp - serverTimestamp;
const responseTime = (serverClientRequestDiffTime - nowTimeStamp + (clientTimestamp - serverClientResponseDiffTime)) / 2;
return nowTimeStamp + responseTime;

@@ -60,23 +47,13 @@ });

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
const debug = createDebug('folklore:clock');
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
class Clock extends EventEmitter {
static getUTCTime(date) {
const realDate = date || new Date();
return realDate.getTime();
}
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var debug = createDebug('folklore:clock');
var Clock = /*#__PURE__*/function (_EventEmitter) {
_inherits(Clock, _EventEmitter);
var _super = _createSuper(Clock);
function Clock(opts) {
var _this;
_classCallCheck(this, Clock);
_this = _super.call(this);
_this.options = _objectSpread({
constructor(opts) {
super();
this.options = {
time: null,

@@ -88,187 +65,164 @@ updateInterval: 10,

serverParseResponse: null,
syncCount: 5
}, opts);
_this.onUpdate = _this.onUpdate.bind(_assertThisInitialized(_this));
_this.startTime = Clock.getUTCTime();
_this.currentTime = _this.startTime;
_this.customStartTime = _this.options.time || _this.startTime;
_this.time = _this.customStartTime;
_this.serverOffset = 0;
_this.started = false;
_this.shouldStart = false;
_this.server = null;
_this.interval = null;
syncCount: 5,
...opts
};
this.onUpdate = this.onUpdate.bind(this);
this.startTime = Clock.getUTCTime();
this.currentTime = this.startTime;
this.customStartTime = this.options.time || this.startTime;
this.time = this.customStartTime;
this.serverOffset = 0;
this.started = false;
this.shouldStart = false;
this.server = null;
this.interval = null;
if (_this.options.server !== null) {
_this.ready = false;
_this.setServer(_this.options.server);
if (this.options.server !== null) {
this.ready = false;
this.setServer(this.options.server);
} else {
_this.ready = true;
_this.emit('ready');
this.ready = true;
this.emit('ready');
}
if (_this.options.autoStart) {
_this.start();
if (this.options.autoStart) {
this.start();
}
}
return _this;
setTime(time) {
this.startTime = Clock.getUTCTime();
this.customStartTime = time;
}
_createClass(Clock, [{
key: "setTime",
value: function setTime(time) {
this.startTime = Clock.getUTCTime();
this.customStartTime = time;
setServer(server) {
debug("Setting server to: ".concat(server));
this.ready = false;
this.server = server;
const wasStarted = this.started;
if (wasStarted) {
debug('Stopping time whil syncing.');
this.stop();
}
}, {
key: "setServer",
value: function setServer(server) {
var _this2 = this;
debug("Setting server to: ".concat(server));
this.ready = false;
this.server = server;
var wasStarted = this.started;
return this.sync().then(() => {
this.ready = true;
this.emit('ready');
if (wasStarted) {
debug('Stopping time whil syncing.');
this.stop();
if (wasStarted || this.shouldStart) {
debug('Starting back...');
this.start();
}
});
}
return this.sync().then(function () {
_this2.ready = true;
sync() {
debug("Syncing with server: ".concat(this.server));
const {
syncCount,
serverUrlFormat,
serverParseResponse
} = this.options;
const promises = [];
_this2.emit('ready');
if (wasStarted || _this2.shouldStart) {
debug('Starting back...');
_this2.start();
}
for (let i = 0; i < syncCount; i += 1) {
const promise = getServerTime(this.server, {
urlFormat: serverUrlFormat,
parseResponse: serverParseResponse
}).then(time => {
const clientTime = new Date().getTime();
return {
server: time,
client: clientTime
};
});
promises.push(promise);
}
}, {
key: "sync",
value: function sync() {
var _this3 = this;
debug("Syncing with server: ".concat(this.server));
var _this$options = this.options,
syncCount = _this$options.syncCount,
serverUrlFormat = _this$options.serverUrlFormat,
serverParseResponse = _this$options.serverParseResponse;
var promises = [];
return Promise.all(promises).then(times => {
const timesCount = times.length;
const avgOffset = times.reduce((total, _ref) => {
let {
client,
server
} = _ref;
return total + (client - server);
}, 0) / timesCount;
const avgTime = times.reduce((total, _ref2) => {
let {
server
} = _ref2;
return total + server;
}, 0) / timesCount;
this.serverOffset = avgOffset;
this.setTime(avgTime);
debug("Time synced with server. Offset: ".concat(this.serverOffset));
this.emit('synced', avgTime);
});
}
for (var i = 0; i < syncCount; i += 1) {
var promise = getServerTime(this.server, {
urlFormat: serverUrlFormat,
parseResponse: serverParseResponse
}).then(function (time) {
var clientTime = new Date().getTime();
return {
server: time,
client: clientTime
};
});
promises.push(promise);
}
getOffset() {
return this.serverOffset;
}
return Promise.all(promises).then(function (times) {
var timesCount = times.length;
var avgOffset = times.reduce(function (total, _ref) {
var client = _ref.client,
server = _ref.server;
return total + (client - server);
}, 0) / timesCount;
var avgTime = times.reduce(function (total, _ref2) {
var server = _ref2.server;
return total + server;
}, 0) / timesCount;
_this3.serverOffset = avgOffset;
start() {
if (this.started) {
return;
}
_this3.setTime(avgTime);
if (!this.ready) {
this.shouldStart = true;
debug('Not ready, waiting to start...');
return;
}
debug("Time synced with server. Offset: ".concat(_this3.serverOffset));
debug('Starting...');
this.started = true;
const {
updateInterval
} = this.options;
_this3.emit('synced', avgTime);
});
if (updateInterval !== null && updateInterval > 0) {
this.interval = setInterval(this.onUpdate, updateInterval);
}
}, {
key: "getOffset",
value: function getOffset() {
return this.serverOffset;
}
stop() {
if (!this.started) {
return;
}
}, {
key: "start",
value: function start() {
if (this.started) {
return;
}
if (!this.ready) {
this.shouldStart = true;
debug('Not ready, waiting to start...');
return;
}
this.shouldStart = false;
this.started = false;
debug('Starting...');
this.started = true;
var updateInterval = this.options.updateInterval;
if (updateInterval !== null && updateInterval > 0) {
this.interval = setInterval(this.onUpdate, updateInterval);
}
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}, {
key: "stop",
value: function stop() {
if (!this.started) {
return;
}
}
this.shouldStart = false;
this.started = false;
getTime() {
this.update();
return this.time;
}
if (this.interval) {
clearInterval(this.interval);
this.interval = null;
}
}
}, {
key: "getTime",
value: function getTime() {
this.update();
return this.time;
}
}, {
key: "update",
value: function update() {
this.currentTime = Clock.getUTCTime();
var currentDelta = this.currentTime - this.startTime;
var time = this.customStartTime + currentDelta;
var changed = time !== this.time;
this.time = time;
update() {
this.currentTime = Clock.getUTCTime();
const currentDelta = this.currentTime - this.startTime;
const time = this.customStartTime + currentDelta;
const changed = time !== this.time;
this.time = time;
if (changed) {
this.emit('change', this.time);
}
if (changed) {
this.emit('change', this.time);
}
}, {
key: "onUpdate",
value: function onUpdate() {
this.update();
}
}], [{
key: "getUTCTime",
value: function getUTCTime(date) {
var realDate = date || new Date();
return realDate.getTime();
}
}]);
}
return Clock;
}(EventEmitter);
onUpdate() {
this.update();
}
}
export { Clock, Clock as default, getServerTime };
{
"name": "@folklore/clock",
"version": "0.1.16",
"version": "0.1.17",
"description": "Clock synced with server",

@@ -46,3 +46,3 @@ "keywords": [

},
"gitHead": "9f8a1f532529131d760cadf521b80fd3d9590209"
"gitHead": "80b0b5301d3c79cca819327b0f95d8fac1a0b77d"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc