@peerbit/time
Advanced tools
Comparing version 2.0.6 to 2.0.7-171d517
{ | ||
"name": "@peerbit/time", | ||
"version": "2.0.6", | ||
"description": "Utility functions for time", | ||
"type": "module", | ||
"sideEffects": false, | ||
"module": "lib/esm/index.js", | ||
"types": "lib/esm/index.d.ts", | ||
"exports": { | ||
"import": "./lib/esm/index.js", | ||
"require": "./lib/cjs/index.js" | ||
}, | ||
"files": [ | ||
"lib", | ||
"src", | ||
"!src/**/__tests__", | ||
"!lib/**/__tests__", | ||
"LICENSE" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"clean": "shx rm -rf lib/*", | ||
"build": "yarn clean && tsc -p tsconfig.json", | ||
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit", | ||
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit", | ||
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit" | ||
}, | ||
"author": "dao.xyz", | ||
"license": "MIT", | ||
"gitHead": "e50907578b203c2f16199e91545c5213cf8cdc3e" | ||
"name": "@peerbit/time", | ||
"version": "2.0.7-171d517", | ||
"description": "Utility functions for time", | ||
"type": "module", | ||
"sideEffects": false, | ||
"types": "./dist/src/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
], | ||
"src/*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"src", | ||
"dist", | ||
"!dist/test", | ||
"!**/*.tsbuildinfo" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "peerbit", | ||
"parserOptions": { | ||
"project": true, | ||
"sourceType": "module" | ||
}, | ||
"ignorePatterns": [ | ||
"!.aegir.js", | ||
"test/ts-use", | ||
"*.d.ts" | ||
] | ||
}, | ||
"browser": { | ||
"./dist/src/hrtime.js": "./dist/src/hrtime.browser.js" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"clean": "aegir clean", | ||
"build": "aegir build --no-bundle", | ||
"test": "aegir test", | ||
"lint": "aegir lint" | ||
}, | ||
"author": "dao.xyz", | ||
"license": "MIT" | ||
} |
@@ -1,78 +0,2 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2020 Vlad Tansky | ||
Copyright (c) 2022 dao.xyz | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
const _perfomancePolyfill = () => { | ||
// based on https://gist.github.com/paulirish/5438650 copyright Paul Irish 2015. | ||
if ("performance" in window === false) { | ||
((window as any).performance as any) = {}; | ||
} | ||
Date.now = | ||
Date.now || | ||
(() => { | ||
// thanks IE8 | ||
return new Date().getTime(); | ||
}); | ||
if ("now" in window.performance === false) { | ||
let nowOffset = Date.now(); | ||
if (performance.timeOrigin) { | ||
nowOffset = performance.timeOrigin; | ||
} | ||
(window.performance as any)["now"] = () => Date.now() - nowOffset; | ||
} | ||
}; | ||
const _hrtime = (previousTimestamp?: [number, number]): [number, number] => { | ||
_perfomancePolyfill(); | ||
const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3); | ||
const clocktime = performance.now() * 1e-3; | ||
let seconds = Math.floor(clocktime) + baseNow; | ||
let nanoseconds = Math.floor((clocktime % 1) * 1e9); | ||
if (previousTimestamp) { | ||
seconds = seconds - previousTimestamp[0]; | ||
nanoseconds = nanoseconds - previousTimestamp[1]; | ||
if (nanoseconds < 0) { | ||
seconds--; | ||
nanoseconds += 1e9; | ||
} | ||
} | ||
return [seconds, nanoseconds]; | ||
}; | ||
const NS_PER_SEC = 1e9; | ||
_hrtime.bigint = (time?: [number, number]): bigint => { | ||
const diff = _hrtime(time); | ||
return BigInt(diff[0] * NS_PER_SEC + diff[1]); | ||
}; | ||
export default typeof process === "undefined" || | ||
typeof process.hrtime === "undefined" | ||
? _hrtime | ||
: process.hrtime; | ||
const hrtime = process.hrtime; | ||
export { hrtime }; |
@@ -0,4 +1,5 @@ | ||
import { hrtime } from "./hrtime.js"; | ||
export * from "./wait.js"; | ||
export * from "./metrics.js"; | ||
import hrtime from "./hrtime.js"; | ||
export { hrtime }; |
@@ -1,2 +0,2 @@ | ||
import hrtime from "./hrtime.js"; | ||
import { hrtime } from "./hrtime.js"; | ||
@@ -11,2 +11,3 @@ export class MovingAverageTracker { | ||
} | ||
add(number: number) { | ||
@@ -20,5 +21,5 @@ const now = hrtime.bigint(); | ||
this.lastTS = now; | ||
const alpha_t = 1 - Math.exp(-dt / this.tau); | ||
this.value = (1 - alpha_t) * this.value + (alpha_t * number) / dt; | ||
const alphaT = 1 - Math.exp(-dt / this.tau); | ||
this.value = (1 - alphaT) * this.value + (alphaT * number) / dt; | ||
} | ||
} |
@@ -1,17 +0,9 @@ | ||
export class TimeoutError extends Error { | ||
constructor(message?: string) { | ||
super(message); | ||
} | ||
} | ||
export class TimeoutError extends Error {} | ||
export class AbortError extends Error { | ||
constructor(message?: string) { | ||
super(message); | ||
} | ||
} | ||
export const delay = (ms: number, options?: { signal?: AbortSignal }) => { | ||
return new Promise<void>((res, rej) => { | ||
export class AbortError extends Error {} | ||
export const delay = async (ms: number, options?: { signal?: AbortSignal }) => { | ||
return new Promise<void>((resolve, reject) => { | ||
function handleAbort() { | ||
clearTimeout(timer); | ||
rej(new AbortError()); | ||
reject(new AbortError()); | ||
} | ||
@@ -21,3 +13,3 @@ options?.signal?.addEventListener("abort", handleAbort); | ||
options?.signal?.removeEventListener("abort", handleAbort); | ||
res(); | ||
resolve(); | ||
}, ms); | ||
@@ -31,3 +23,3 @@ }); | ||
? "Timed out: " + options?.timeoutMessage | ||
: "Timed out" | ||
: "Timed out", | ||
); | ||
@@ -42,7 +34,7 @@ | ||
timeoutMessage?: string; | ||
} = { timeout: 10 * 1000, delayInterval: 100 } | ||
} = { timeout: 10 * 1000, delayInterval: 100 }, | ||
): Promise<T | undefined> => { | ||
const delayInterval = options.delayInterval || 100; | ||
const timeout = options.timeout || 10 * 1000; | ||
const startTime = +new Date(); | ||
const startTime = Number(new Date()); | ||
let stop = false; | ||
@@ -56,3 +48,5 @@ | ||
options.signal?.addEventListener("abort", handleAbort); | ||
while (!stop && +new Date() - startTime < timeout) { | ||
// eslint-disable-next-line no-unmodified-loop-condition | ||
while (!stop && Number(new Date()) - startTime < timeout) { | ||
const result = await fn(); | ||
@@ -76,3 +70,3 @@ if (result) { | ||
timeoutMessage?: string; | ||
} = { timeout: 10 * 1000, delayInterval: 50 } | ||
} = { timeout: 10 * 1000, delayInterval: 50 }, | ||
): Promise<T> => { | ||
@@ -82,3 +76,3 @@ const delayInterval = options.delayInterval || 50; | ||
const startTime = +new Date(); | ||
const startTime = Number(new Date()); | ||
let stop = false; | ||
@@ -93,3 +87,5 @@ let lastError: Error | undefined; | ||
options.signal?.addEventListener("abort", handleAbort); | ||
while (!stop && +new Date() - startTime < timeout) { | ||
// eslint-disable-next-line no-unmodified-loop-condition | ||
while (!stop && Number(new Date()) - startTime < timeout) { | ||
try { | ||
@@ -100,3 +96,3 @@ const result = await fn(); | ||
} catch (error: any) { | ||
if (error instanceof AbortError === false) { | ||
if (!(error instanceof AbortError)) { | ||
lastError = error; | ||
@@ -103,0 +99,0 @@ } else { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20689
27
328
2
1