@jrc03c/pause 0.0.11 → 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/dist/pause.min.js CHANGED
@@ -1 +1 @@
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);})();
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
@@ -18,5 +18,10 @@
18
18
  "module": "./src/index.mjs",
19
19
  "name": "@jrc03c/pause",
20
20
  "type": "module",
21
- "version": "0.0.11"
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
+ }
@@ -0,0 +1,2 @@
1
+ onlyBuiltDependencies:
2
+ - esbuild
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/src/index.mjs CHANGED
@@ -9,4 +9,17 @@ function pause(ms) {
9
9
  })
10
10
  }
11
11
 
12
- export { pause }
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 }
@@ -1,30 +1,63 @@
1
- import { pause } from "./index.mjs"
1
+ import { pause, pauseSync } from "./index.mjs"
2
2
 
3
3
  !(async () => {
4
- const start = performance.now()
4
+ // asynchronous tests
5
+ await (async () => {
6
+ const start = performance.now()
5
7
 
6
- for (let i = 0; i < 100; i++) {
7
- const t = Math.floor(Math.random() * 100)
8
- const elapsed = await pause(t)
8
+ for (let i = 0; i < 100; i++) {
9
+ const t = Math.floor(Math.random() * 100)
10
+ const elapsed = await pause(t)
9
11
 
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
- )
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
+ }
14
25
  }
15
26
 
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
- )
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
+ }
22
55
  }
23
- }
24
56
 
25
- const seconds = (performance.now() - start) / 1000
57
+ const seconds = (performance.now() - start) / 1000
26
58
 
27
- console.log(
28
- `🎉 All asynchronous tests passed! (${seconds.toFixed(2)} seconds)`,
29
- )
59
+ console.log(
60
+ `🎉 All synchronous tests passed! (${seconds.toFixed(2)} seconds)`,
61
+ )
62
+ })()
30
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