@nxtedition/lib 23.9.11 → 23.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.9.11",
3
+ "version": "23.9.14",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/under-pressure.js CHANGED
@@ -21,34 +21,28 @@ export default function (opts = {}) {
21
21
  let heapUsedBytes = 0
22
22
  let rssBytes = 0
23
23
  let eventLoopDelay = 0
24
- let elu
25
24
  let eventLoopUtilized = 0
26
25
 
27
- const histogram = monitorEventLoopDelay({ resolution })
28
- histogram.enable()
29
-
30
- if (performance.eventLoopUtilization) {
31
- elu = performance.eventLoopUtilization()
32
- }
33
-
34
26
  if (!maxEventLoopDelay && !maxHeapUsedBytes && !maxRssBytes && !maxEventLoopUtilization) {
35
27
  return
36
28
  }
37
29
 
30
+ const histogram = maxEventLoopDelay ? monitorEventLoopDelay({ resolution }) : null
31
+ histogram?.enable()
32
+ const elu = maxEventLoopUtilization ? performance.eventLoopUtilization?.() : null
33
+
38
34
  function updateEventLoopDelay() {
39
- eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
40
- if (Number.isNaN(eventLoopDelay)) {
41
- eventLoopDelay = Infinity
35
+ if (histogram) {
36
+ eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
37
+ if (Number.isNaN(eventLoopDelay)) {
38
+ eventLoopDelay = Infinity
39
+ }
40
+ histogram.reset()
42
41
  }
43
- histogram.reset()
44
42
  }
45
43
 
46
44
  function updateEventLoopUtilization() {
47
- if (elu) {
48
- eventLoopUtilized = performance.eventLoopUtilization(elu).utilization
49
- } else {
50
- eventLoopUtilized = 0
51
- }
45
+ eventLoopUtilized = elu ? performance.eventLoopUtilization(elu).utilization : 0
52
46
  }
53
47
 
54
48
  const timer = setTimeout(update, sampleInterval).unref()
package/yield.js CHANGED
@@ -1,16 +1,36 @@
1
+ import { AbortError } from './errors.js'
2
+
1
3
  let yieldTime = performance.now()
2
4
 
3
- export function maybeYield(opts) {
5
+ export function maybeYield(callback, opaque, opts) {
6
+ if (callback != null && typeof callback !== 'function') {
7
+ throw new TypeError('callback must be a function')
8
+ }
9
+
4
10
  const timeout = opts?.timeout ?? 50
11
+
12
+ if (!Number.isFinite(timeout) || timeout < 0) {
13
+ throw new TypeError('timeout must be a finite non-negative number')
14
+ }
15
+
5
16
  if (performance.now() - yieldTime > timeout) {
6
- return doYield(opts)
17
+ doYield(opts, callback, opaque)
18
+ } else {
19
+ callback(opts?.signal?.aborted ? (opts.signal.reason ?? new AbortError()) : null, opaque)
7
20
  }
8
- opts?.signal?.throwIfAborted()
9
21
  }
10
22
 
11
- export async function doYield(opts) {
12
- await new Promise((resolve) => setImmediate(resolve))
13
- resetYield(opts)
23
+ export async function doYield(callback, opaque, opts) {
24
+ if (callback != null && typeof callback !== 'function') {
25
+ throw new TypeError('callback must be a function')
26
+ }
27
+
28
+ // TODO (fix): Use a yield queue?
29
+
30
+ setImmediate(() => {
31
+ resetYield(opts)
32
+ callback(opts?.signal?.aborted ? (opts.signal.reason ?? new AbortError()) : null, opaque)
33
+ })
14
34
  }
15
35
 
16
36
  function resetYield(opts) {
@@ -21,3 +41,12 @@ function resetYield(opts) {
21
41
  setInterval(() => {
22
42
  yieldTime = performance.now()
23
43
  }, 500).unref()
44
+
45
+ export const promises = {
46
+ maybeYield(opts, opaque) {
47
+ return new Promise((resolve, reject) =>
48
+ maybeYield((err, val) => (err ? reject(err) : resolve(val)), opaque, opts),
49
+ )
50
+ },
51
+ resetYield,
52
+ }