@nxtedition/lib 23.13.0 → 23.14.0

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.
Files changed (3) hide show
  1. package/fixed-queue.js +113 -0
  2. package/package.json +4 -2
  3. package/yield.js +58 -0
package/fixed-queue.js ADDED
@@ -0,0 +1,113 @@
1
+ // Extracted from node/lib/internal/fixed_queue.js
2
+
3
+ // Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.
4
+ const kSize = 2048
5
+ const kMask = kSize - 1
6
+
7
+ // The FixedQueue is implemented as a singly-linked list of fixed-size
8
+ // circular buffers. It looks something like this:
9
+ //
10
+ // head tail
11
+ // | |
12
+ // v v
13
+ // +-----------+ <-----\ +-----------+ <------\ +-----------+
14
+ // | [null] | \----- | next | \------- | next |
15
+ // +-----------+ +-----------+ +-----------+
16
+ // | item | <-- bottom | item | <-- bottom | [empty] |
17
+ // | item | | item | | [empty] |
18
+ // | item | | item | | [empty] |
19
+ // | item | | item | | [empty] |
20
+ // | item | | item | bottom --> | item |
21
+ // | item | | item | | item |
22
+ // | ... | | ... | | ... |
23
+ // | item | | item | | item |
24
+ // | item | | item | | item |
25
+ // | [empty] | <-- top | item | | item |
26
+ // | [empty] | | item | | item |
27
+ // | [empty] | | [empty] | <-- top top --> | [empty] |
28
+ // +-----------+ +-----------+ +-----------+
29
+ //
30
+ // Or, if there is only one circular buffer, it looks something
31
+ // like either of these:
32
+ //
33
+ // head tail head tail
34
+ // | | | |
35
+ // v v v v
36
+ // +-----------+ +-----------+
37
+ // | [null] | | [null] |
38
+ // +-----------+ +-----------+
39
+ // | [empty] | | item |
40
+ // | [empty] | | item |
41
+ // | item | <-- bottom top --> | [empty] |
42
+ // | item | | [empty] |
43
+ // | [empty] | <-- top bottom --> | item |
44
+ // | [empty] | | item |
45
+ // +-----------+ +-----------+
46
+ //
47
+ // Adding a value means moving `top` forward by one, removing means
48
+ // moving `bottom` forward by one. After reaching the end, the queue
49
+ // wraps around.
50
+ //
51
+ // When `top === bottom` the current queue is empty and when
52
+ // `top + 1 === bottom` it's full. This wastes a single space of storage
53
+ // but allows much quicker checks.
54
+
55
+ class FixedCircularBuffer {
56
+ constructor() {
57
+ this.bottom = 0
58
+ this.top = 0
59
+ this.list = new Array(kSize)
60
+ this.next = null
61
+ }
62
+
63
+ isEmpty() {
64
+ return this.top === this.bottom
65
+ }
66
+
67
+ isFull() {
68
+ return ((this.top + 1) & kMask) === this.bottom
69
+ }
70
+
71
+ push(data) {
72
+ this.list[this.top] = data
73
+ this.top = (this.top + 1) & kMask
74
+ }
75
+
76
+ shift() {
77
+ const nextItem = this.list[this.bottom]
78
+ if (nextItem === undefined) return null
79
+ this.list[this.bottom] = undefined
80
+ this.bottom = (this.bottom + 1) & kMask
81
+ return nextItem
82
+ }
83
+ }
84
+
85
+ export class FixedQueue {
86
+ constructor() {
87
+ this.head = this.tail = new FixedCircularBuffer()
88
+ this.size = 0
89
+ }
90
+
91
+ isEmpty() {
92
+ return this.head.isEmpty()
93
+ }
94
+
95
+ push(data) {
96
+ if (this.head.isFull()) {
97
+ // Head is full: Creates a new queue, sets the old queue's `.next` to it,
98
+ // and sets it as the new main queue.
99
+ this.head = this.head.next = new FixedCircularBuffer()
100
+ }
101
+ this.head.push(data)
102
+ }
103
+
104
+ shift() {
105
+ const tail = this.tail
106
+ const next = tail.shift()
107
+ if (tail.isEmpty() && tail.next !== null) {
108
+ // If there is another queue, it forms the new tail.
109
+ this.tail = tail.next
110
+ }
111
+ return next
112
+ }
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.13.0",
3
+ "version": "23.14.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -10,6 +10,7 @@
10
10
  "rxjs/*",
11
11
  "util/*",
12
12
  "cache.js",
13
+ "fixed-queue.js",
13
14
  "http-client.js",
14
15
  "subtract-ranges.js",
15
16
  "serializers.js",
@@ -44,7 +45,8 @@
44
45
  "transcript.js",
45
46
  "docker-secrets.js",
46
47
  "wordwrap.js",
47
- "under-pressure.js"
48
+ "under-pressure.js",
49
+ "yield.js"
48
50
  ],
49
51
  "scripts": {
50
52
  "prepublishOnly": "pinst --disable",
package/yield.js ADDED
@@ -0,0 +1,58 @@
1
+ import { FixedQueue } from './fixed-queue.js'
2
+
3
+ const yieldTimeout = 50
4
+ const yieldQueue = new FixedQueue()
5
+ let yieldScheduled = false
6
+ let yieldTime = performance.now()
7
+ let yieldActive = false
8
+
9
+ export function maybeYield(opts, callback) {
10
+ if (callback != null && typeof callback !== 'function') {
11
+ throw new TypeError('callback must be a function')
12
+ }
13
+
14
+ return performance.now() - yieldTime < yieldTimeout ? null : doYield(opts, callback)
15
+ }
16
+
17
+ export function doYield(opts, callback) {
18
+ if (!yieldScheduled) {
19
+ yieldScheduled = true
20
+ setImmediate(dispatchYield)
21
+ }
22
+
23
+ if (callback) {
24
+ yieldQueue.push(callback)
25
+ return true
26
+ } else {
27
+ return new Promise((resolve) => {
28
+ yieldQueue.push(resolve)
29
+ })
30
+ }
31
+ }
32
+
33
+ function dispatchYield() {
34
+ if (yieldActive) {
35
+ return
36
+ }
37
+
38
+ yieldActive = true
39
+ yieldTime = performance.now()
40
+
41
+ while (!yieldQueue.isEmpty()) {
42
+ const resolve = yieldQueue.shift()
43
+
44
+ resolve(null)
45
+
46
+ if (performance.now() - yieldTime > yieldTimeout) {
47
+ setImmediate(dispatchYield)
48
+ return
49
+ }
50
+ }
51
+
52
+ yieldActive = false
53
+ yieldScheduled = false
54
+ }
55
+
56
+ setInterval(() => {
57
+ yieldTime = performance.now()
58
+ }, 500).unref()