@jrc03c/pause 0.0.11 → 0.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/iife.mjs +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +18 -0
- package/package.json +24 -6
- package/readme.md +6 -0
- package/.prettierrc.json +0 -10
- package/build.mjs +0 -24
- package/dist/pause.js +0 -18
- package/dist/pause.min.js +0 -1
- package/eslint.config.js +0 -8
- package/moon.yml +0 -29
- package/src/iife.mjs +0 -5
- package/src/index.mjs +0 -12
- package/src/index.test.mjs +0 -30
package/dist/iife.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return new Promise((t,n)=>{try{let n=performance.now();return setTimeout(()=>t(performance.now()-n),e)}catch(e){return n(e)}})}function t(e){let t=performance.now(),n=!0;for(;n;)performance.now()-t>=e&&(n=!1);return performance.now()-t}Object.defineProperty(globalThis,"pause",{configurable:!0,enumerable:!0,writable:!0,value:e}),Object.defineProperty(globalThis,"pauseSync",{configurable:!0,enumerable:!0,writable:!0,value:t});export{};
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function pause(ms) {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
try {
|
|
5
|
+
const start = performance.now();
|
|
6
|
+
return setTimeout(() => resolve(performance.now() - start), ms);
|
|
7
|
+
} catch (e) {
|
|
8
|
+
return reject(e);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
function pauseSync(ms) {
|
|
13
|
+
const start = performance.now();
|
|
14
|
+
let isStillWaiting = true;
|
|
15
|
+
while (isStillWaiting) if (performance.now() - start >= ms) isStillWaiting = false;
|
|
16
|
+
return performance.now() - start;
|
|
17
|
+
}
|
|
18
|
+
export { pause, pauseSync };
|
package/package.json
CHANGED
|
@@ -3,20 +3,38 @@
|
|
|
3
3
|
"description": "a simple async pause function",
|
|
4
4
|
"devDependencies": {
|
|
5
5
|
"@eslint/js": "^9.12.0",
|
|
6
|
-
"
|
|
7
|
-
"eslint": "^9.12.0",
|
|
6
|
+
"eslint": "^10.14.0",
|
|
8
7
|
"globals": "^15.11.0",
|
|
9
|
-
"prettier": "^3.
|
|
8
|
+
"prettier": "^3.9.10",
|
|
9
|
+
"tsdown": "^0.23.5",
|
|
10
|
+
"typescript": "^6.0.3",
|
|
11
|
+
"typescript-eslint": "^8.72.1",
|
|
12
|
+
"vitest": "^5.1.0"
|
|
10
13
|
},
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": "./dist/index.mjs",
|
|
17
|
+
"types": "./dist/index.d.mts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
11
23
|
"keywords": [
|
|
12
24
|
"js",
|
|
13
25
|
"pause",
|
|
14
26
|
"settimeout"
|
|
15
27
|
],
|
|
16
28
|
"license": "ISC",
|
|
17
|
-
"main": "
|
|
18
|
-
"module": "
|
|
29
|
+
"main": "dist/index.mjs",
|
|
30
|
+
"module": "dist/index.mjs",
|
|
19
31
|
"name": "@jrc03c/pause",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsdown",
|
|
34
|
+
"pub": "npm version patch --force && npm publish --access=public",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"watch": "tsdown --watch"
|
|
37
|
+
},
|
|
20
38
|
"type": "module",
|
|
21
|
-
"version": "0.0.
|
|
39
|
+
"version": "0.0.13"
|
|
22
40
|
}
|
package/readme.md
CHANGED
|
@@ -23,3 +23,9 @@ pause(1000).then(elapsed => {
|
|
|
23
23
|
## `pause(ms)`
|
|
24
24
|
|
|
25
25
|
Given a number of milliseconds, `ms`, for which to pause, returns a `Promise` that resolves to a number indicating how many milliseconds have actually elapsed since the function was called.
|
|
26
|
+
|
|
27
|
+
## `pauseSync(ms)`
|
|
28
|
+
|
|
29
|
+
Given a number of milliseconds, `ms`, for which to pause, returns a number indicating how many milliseconds have actually elapsed since the function was called.
|
|
30
|
+
|
|
31
|
+
> **WARNING:** Because this function is synchronous, it completely blocks its thread while running! Therefore, you should probably almost never use this in production. I provide it mainly because it's useful in some testing contexts.
|
package/.prettierrc.json
DELETED
package/build.mjs
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { execSync } from "node:child_process"
|
|
2
|
-
|
|
3
|
-
function rebuild() {
|
|
4
|
-
console.log("-----")
|
|
5
|
-
console.log(`\nRebuilding... (${new Date().toLocaleString()})`)
|
|
6
|
-
|
|
7
|
-
try {
|
|
8
|
-
execSync(
|
|
9
|
-
`npx esbuild src/iife.mjs --bundle --outfile=dist/pause.js`,
|
|
10
|
-
{ encoding: "utf8" }
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
execSync(
|
|
14
|
-
`npx esbuild src/iife.mjs --bundle --minify --outfile=dist/pause.min.js`,
|
|
15
|
-
{ encoding: "utf8" }
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
console.log("\nDone! 🎉\n")
|
|
19
|
-
} catch (e) {
|
|
20
|
-
console.error(e)
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
rebuild()
|
package/dist/pause.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
(() => {
|
|
2
|
-
// src/index.mjs
|
|
3
|
-
function pause(ms) {
|
|
4
|
-
return new Promise((resolve, reject) => {
|
|
5
|
-
try {
|
|
6
|
-
const start = performance.now();
|
|
7
|
-
return setTimeout(() => resolve(performance.now() - start), ms);
|
|
8
|
-
} catch (e) {
|
|
9
|
-
return reject(e);
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// src/iife.mjs
|
|
15
|
-
if (typeof globalThis !== "undefined") {
|
|
16
|
-
globalThis.pause = pause;
|
|
17
|
-
}
|
|
18
|
-
})();
|
package/dist/pause.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(()=>{function r(o){return new Promise((t,n)=>{try{let e=performance.now();return setTimeout(()=>t(performance.now()-e),o)}catch(e){return n(e)}})}typeof globalThis<"u"&&(globalThis.pause=r);})();
|
package/eslint.config.js
DELETED
package/moon.yml
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
language: javascript
|
|
2
|
-
tasks:
|
|
3
|
-
build:
|
|
4
|
-
command:
|
|
5
|
-
- node
|
|
6
|
-
- build.mjs
|
|
7
|
-
platform: node
|
|
8
|
-
pub:
|
|
9
|
-
command:
|
|
10
|
-
- npm
|
|
11
|
-
- publish
|
|
12
|
-
- --access=public
|
|
13
|
-
deps:
|
|
14
|
-
- ~:pub-dep1
|
|
15
|
-
local: true
|
|
16
|
-
platform: node
|
|
17
|
-
pub-dep1:
|
|
18
|
-
command:
|
|
19
|
-
- npm
|
|
20
|
-
- version
|
|
21
|
-
- patch
|
|
22
|
-
- --force
|
|
23
|
-
local: true
|
|
24
|
-
platform: node
|
|
25
|
-
test:
|
|
26
|
-
command:
|
|
27
|
-
- node
|
|
28
|
-
- src/index.test.mjs
|
|
29
|
-
platform: node
|
package/src/iife.mjs
DELETED
package/src/index.mjs
DELETED
package/src/index.test.mjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { pause } from "./index.mjs"
|
|
2
|
-
|
|
3
|
-
!(async () => {
|
|
4
|
-
const start = performance.now()
|
|
5
|
-
|
|
6
|
-
for (let i = 0; i < 100; i++) {
|
|
7
|
-
const t = Math.floor(Math.random() * 100)
|
|
8
|
-
const elapsed = await pause(t)
|
|
9
|
-
|
|
10
|
-
if (typeof elapsed !== "number" || isNaN(elapsed)) {
|
|
11
|
-
throw new Error(
|
|
12
|
-
`The function was supposed to return a number representing the number of milliseconds that elapsed since the function was called, but instead it returned a ${typeof elapsed} value! (${elapsed})`,
|
|
13
|
-
)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// there should be less than 10ms difference between the given time and the
|
|
17
|
-
// actual elapsed time
|
|
18
|
-
if (Math.abs(elapsed - t) > 10) {
|
|
19
|
-
throw new Error(
|
|
20
|
-
`The function was supposed to wait for ${t}ms, but instead it waited for ${elapsed}ms!`,
|
|
21
|
-
)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const seconds = (performance.now() - start) / 1000
|
|
26
|
-
|
|
27
|
-
console.log(
|
|
28
|
-
`🎉 All asynchronous tests passed! (${seconds.toFixed(2)} seconds)`,
|
|
29
|
-
)
|
|
30
|
-
})()
|