@jrc03c/pause 0.0.13 → 0.0.14
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/.prettierrc.json +10 -0
- package/build.mjs +24 -0
- package/dist/pause.js +18 -0
- package/dist/pause.min.js +1 -0
- package/eslint.config.js +8 -0
- package/package.json +8 -21
- package/pnpm-workspace.yaml +4 -0
- package/src/iife.mjs +5 -0
- package/src/index.mjs +25 -0
- package/src/index.test.mjs +63 -0
- package/dist/iife.mjs +0 -1
- package/dist/index.d.mts +0 -3
- package/dist/index.mjs +0 -18
package/.prettierrc.json
ADDED
package/build.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{function r(n){return new Promise((t,o)=>{try{let e=performance.now();return setTimeout(()=>t(performance.now()-e),n)}catch(e){return o(e)}})}typeof globalThis<"u"&&(globalThis.pause=r);})();
|
package/eslint.config.js
ADDED
package/package.json
CHANGED
|
@@ -3,38 +3,25 @@
|
|
|
3
3
|
"description": "a simple async pause function",
|
|
4
4
|
"devDependencies": {
|
|
5
5
|
"@eslint/js": "^9.12.0",
|
|
6
|
-
"
|
|
6
|
+
"esbuild": "^0.25.4",
|
|
7
|
+
"eslint": "^9.12.0",
|
|
7
8
|
"globals": "^15.11.0",
|
|
8
|
-
"prettier": "^3.
|
|
9
|
-
"tsdown": "^0.23.5",
|
|
10
|
-
"typescript": "^6.0.3",
|
|
11
|
-
"typescript-eslint": "^8.72.1",
|
|
12
|
-
"vitest": "^5.1.0"
|
|
9
|
+
"prettier": "^3.3.3"
|
|
13
10
|
},
|
|
14
|
-
"exports": {
|
|
15
|
-
".": {
|
|
16
|
-
"import": "./dist/index.mjs",
|
|
17
|
-
"types": "./dist/index.d.mts"
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
"files": [
|
|
21
|
-
"dist"
|
|
22
|
-
],
|
|
23
11
|
"keywords": [
|
|
24
12
|
"js",
|
|
25
13
|
"pause",
|
|
26
14
|
"settimeout"
|
|
27
15
|
],
|
|
28
16
|
"license": "ISC",
|
|
29
|
-
"main": "
|
|
30
|
-
"module": "
|
|
17
|
+
"main": "./src/index.mjs",
|
|
18
|
+
"module": "./src/index.mjs",
|
|
31
19
|
"name": "@jrc03c/pause",
|
|
32
20
|
"scripts": {
|
|
33
|
-
"build": "
|
|
21
|
+
"build": "node build.mjs",
|
|
34
22
|
"pub": "npm version patch --force && npm publish --access=public",
|
|
35
|
-
"test": "
|
|
36
|
-
"watch": "tsdown --watch"
|
|
23
|
+
"test": "node src/index.test.mjs"
|
|
37
24
|
},
|
|
38
25
|
"type": "module",
|
|
39
|
-
"version": "0.0.
|
|
26
|
+
"version": "0.0.14"
|
|
40
27
|
}
|
package/src/iife.mjs
ADDED
package/src/index.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
function pause(ms) {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
try {
|
|
4
|
+
const start = performance.now()
|
|
5
|
+
return setTimeout(() => resolve(performance.now() - start), ms)
|
|
6
|
+
} catch (e) {
|
|
7
|
+
return reject(e)
|
|
8
|
+
}
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function pauseSync(ms) {
|
|
13
|
+
const start = performance.now()
|
|
14
|
+
let isStillWaiting = true
|
|
15
|
+
|
|
16
|
+
while (isStillWaiting) {
|
|
17
|
+
if (performance.now() - start >= ms) {
|
|
18
|
+
isStillWaiting = false
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return performance.now() - start
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { pause, pauseSync }
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { pause, pauseSync } from "./index.mjs"
|
|
2
|
+
|
|
3
|
+
!(async () => {
|
|
4
|
+
// asynchronous tests
|
|
5
|
+
await (async () => {
|
|
6
|
+
const start = performance.now()
|
|
7
|
+
|
|
8
|
+
for (let i = 0; i < 100; i++) {
|
|
9
|
+
const t = Math.floor(Math.random() * 100)
|
|
10
|
+
const elapsed = await pause(t)
|
|
11
|
+
|
|
12
|
+
if (typeof elapsed !== "number" || isNaN(elapsed)) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`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})`,
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// there should be less than 10ms difference between the given time and
|
|
19
|
+
// the actual elapsed time
|
|
20
|
+
if (Math.abs(elapsed - t) > 10) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`The function was supposed to wait for ${t}ms, but instead it waited for ${elapsed}ms!`,
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const seconds = (performance.now() - start) / 1000
|
|
28
|
+
|
|
29
|
+
console.log(
|
|
30
|
+
`🎉 All asynchronous tests passed! (${seconds.toFixed(2)} seconds)`,
|
|
31
|
+
)
|
|
32
|
+
})()
|
|
33
|
+
|
|
34
|
+
// synchronous tests
|
|
35
|
+
!(() => {
|
|
36
|
+
const start = performance.now()
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < 100; i++) {
|
|
39
|
+
const t = Math.floor(Math.random() * 100)
|
|
40
|
+
const elapsed = pauseSync(t)
|
|
41
|
+
|
|
42
|
+
if (typeof elapsed !== "number" || isNaN(elapsed)) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`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})`,
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// there should be less than 10ms difference between the given time and
|
|
49
|
+
// the actual elapsed time
|
|
50
|
+
if (Math.abs(elapsed - t) > 10) {
|
|
51
|
+
throw new Error(
|
|
52
|
+
`The function was supposed to wait for ${t}ms, but instead it waited for ${elapsed}ms!`,
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const seconds = (performance.now() - start) / 1000
|
|
58
|
+
|
|
59
|
+
console.log(
|
|
60
|
+
`🎉 All synchronous tests passed! (${seconds.toFixed(2)} seconds)`,
|
|
61
|
+
)
|
|
62
|
+
})()
|
|
63
|
+
})()
|
package/dist/iife.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
package/dist/index.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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 };
|