@jrc03c/pause 0.0.10 → 0.0.12
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/dist/pause.js +4 -14
- package/dist/pause.min.js +1 -1
- package/package.json +8 -3
- package/pnpm-workspace.yaml +2 -0
- package/readme.md +31 -0
- package/src/iife.mjs +2 -4
- package/src/index.mjs +11 -9
- package/src/index.test.mjs +55 -57
- package/moon.yml +0 -29
package/.prettierrc.json
ADDED
package/dist/pause.js
CHANGED
|
@@ -1,28 +1,18 @@
|
|
|
1
1
|
(() => {
|
|
2
2
|
// src/index.mjs
|
|
3
|
-
function
|
|
3
|
+
function pause(ms) {
|
|
4
4
|
return new Promise((resolve, reject) => {
|
|
5
5
|
try {
|
|
6
|
-
const start =
|
|
7
|
-
return setTimeout(() => resolve(
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
return setTimeout(() => resolve(performance.now() - start), ms);
|
|
8
8
|
} catch (e) {
|
|
9
9
|
return reject(e);
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
12
|
}
|
|
13
|
-
function pauseSync(ms) {
|
|
14
|
-
const start = /* @__PURE__ */ new Date();
|
|
15
|
-
let now = /* @__PURE__ */ new Date();
|
|
16
|
-
while (now - start < ms) {
|
|
17
|
-
now = /* @__PURE__ */ new Date();
|
|
18
|
-
}
|
|
19
|
-
return /* @__PURE__ */ new Date() - start;
|
|
20
|
-
}
|
|
21
13
|
|
|
22
14
|
// src/iife.mjs
|
|
23
15
|
if (typeof globalThis !== "undefined") {
|
|
24
|
-
globalThis.pause =
|
|
25
|
-
globalThis.pauseAsync = pauseAsync;
|
|
26
|
-
globalThis.pauseSync = pauseSync;
|
|
16
|
+
globalThis.pause = pause;
|
|
27
17
|
}
|
|
28
18
|
})();
|
package/dist/pause.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{function
|
|
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/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "a simple async pause function",
|
|
4
4
|
"devDependencies": {
|
|
5
5
|
"@eslint/js": "^9.12.0",
|
|
6
|
-
"esbuild": "^0.
|
|
6
|
+
"esbuild": "^0.25.4",
|
|
7
7
|
"eslint": "^9.12.0",
|
|
8
8
|
"globals": "^15.11.0",
|
|
9
9
|
"prettier": "^3.3.3"
|
|
@@ -18,5 +18,10 @@
|
|
|
18
18
|
"module": "./src/index.mjs",
|
|
19
19
|
"name": "@jrc03c/pause",
|
|
20
20
|
"type": "module",
|
|
21
|
-
"version": "0.0.
|
|
22
|
-
|
|
21
|
+
"version": "0.0.12",
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "node build.mjs",
|
|
24
|
+
"pub": "npm version patch --force && npm publish --access=public",
|
|
25
|
+
"test": "node src/index.test.mjs"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Intro
|
|
2
|
+
|
|
3
|
+
An asynchronous `pause` function
|
|
4
|
+
|
|
5
|
+
# Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install --save @jrc03c/pause
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
# Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { pause } from "@jrc03c/pause"
|
|
15
|
+
|
|
16
|
+
pause(1000).then(elapsed => {
|
|
17
|
+
console.log(`${elapsed} milliseconds have elapsed!`)
|
|
18
|
+
})
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
# API
|
|
22
|
+
|
|
23
|
+
## `pause(ms)`
|
|
24
|
+
|
|
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/src/iife.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { pause
|
|
1
|
+
import { pause } from "./index.mjs"
|
|
2
2
|
|
|
3
3
|
if (typeof globalThis !== "undefined") {
|
|
4
|
-
globalThis.pause =
|
|
5
|
-
globalThis.pauseAsync = pauseAsync
|
|
6
|
-
globalThis.pauseSync = pauseSync
|
|
4
|
+
globalThis.pause = pause
|
|
7
5
|
}
|
package/src/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
function
|
|
1
|
+
function pause(ms) {
|
|
2
2
|
return new Promise((resolve, reject) => {
|
|
3
3
|
try {
|
|
4
|
-
const start =
|
|
5
|
-
return setTimeout(() => resolve(
|
|
4
|
+
const start = performance.now()
|
|
5
|
+
return setTimeout(() => resolve(performance.now() - start), ms)
|
|
6
6
|
} catch (e) {
|
|
7
7
|
return reject(e)
|
|
8
8
|
}
|
|
@@ -10,14 +10,16 @@ function pauseAsync(ms) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
function pauseSync(ms) {
|
|
13
|
-
const start =
|
|
14
|
-
let
|
|
13
|
+
const start = performance.now()
|
|
14
|
+
let isStillWaiting = true
|
|
15
15
|
|
|
16
|
-
while (
|
|
17
|
-
now
|
|
16
|
+
while (isStillWaiting) {
|
|
17
|
+
if (performance.now() - start >= ms) {
|
|
18
|
+
isStillWaiting = false
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
return
|
|
22
|
+
return performance.now() - start
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
export {
|
|
25
|
+
export { pause, pauseSync }
|
package/src/index.test.mjs
CHANGED
|
@@ -1,65 +1,63 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
let syncTestsAreRunning = true
|
|
1
|
+
import { pause, pauseSync } from "./index.mjs"
|
|
4
2
|
|
|
5
3
|
!(async () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const seconds = (new Date() - start) / 1000
|
|
32
|
-
|
|
33
|
-
console.log(
|
|
34
|
-
`🎉 All asynchronous tests passed! (${seconds.toFixed(2)} seconds)`,
|
|
35
|
-
)
|
|
36
|
-
})()
|
|
37
|
-
|
|
38
|
-
!(() => {
|
|
39
|
-
const start = new Date()
|
|
40
|
-
|
|
41
|
-
for (let i = 0; i < 100; i++) {
|
|
42
|
-
const t = Math.floor(Math.random() * 100)
|
|
43
|
-
const elapsed = pauseSync(t)
|
|
44
|
-
|
|
45
|
-
if (typeof elapsed !== "number" || isNaN(elapsed)) {
|
|
46
|
-
throw new Error(
|
|
47
|
-
`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})`,
|
|
48
|
-
)
|
|
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
|
+
}
|
|
49
25
|
}
|
|
50
26
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
)
|
|
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
55
|
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const seconds = (new Date() - start) / 1000
|
|
59
56
|
|
|
60
|
-
|
|
61
|
-
`🎉 All synchronous tests passed! (${seconds.toFixed(2)} seconds)`,
|
|
62
|
-
)
|
|
57
|
+
const seconds = (performance.now() - start) / 1000
|
|
63
58
|
|
|
64
|
-
|
|
59
|
+
console.log(
|
|
60
|
+
`🎉 All synchronous tests passed! (${seconds.toFixed(2)} seconds)`,
|
|
61
|
+
)
|
|
62
|
+
})()
|
|
65
63
|
})()
|
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
|