xcraft-core-utils 4.19.1 → 4.21.1

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/lib/batcher.js CHANGED
@@ -28,6 +28,10 @@ class Batcher {
28
28
  }
29
29
  }
30
30
 
31
+ #tick() {
32
+ return new Promise((resolve) => setImmediate(resolve));
33
+ }
34
+
31
35
  async start() {
32
36
  this.#running = true;
33
37
  await this.#begin();
@@ -51,6 +55,8 @@ class Batcher {
51
55
  }
52
56
 
53
57
  async pump() {
58
+ await this.#tick();
59
+
54
60
  if (this.#disposing) {
55
61
  await this.stop();
56
62
  return false;
package/lib/locks.js CHANGED
@@ -100,9 +100,39 @@ class GetMutex {
100
100
  }
101
101
  }
102
102
 
103
+ class CoalescingExecutor {
104
+ #running;
105
+ #pending;
106
+
107
+ async run(work) {
108
+ if (this.#running) {
109
+ /* Keep the last one */
110
+ this.#pending = work;
111
+ await this.#running;
112
+ return;
113
+ }
114
+
115
+ /* The first */
116
+ this.#running = (async () => {
117
+ await work();
118
+
119
+ /* Run the last if exists */
120
+ while (this.#pending) {
121
+ const last = this.#pending;
122
+ this.#pending = null;
123
+ await last();
124
+ }
125
+ })();
126
+
127
+ await this.#running;
128
+ this.#running = null;
129
+ }
130
+ }
131
+
103
132
  module.exports = {
104
133
  Mutex,
105
134
  RecursiveMutex,
106
135
  Semaphore,
136
+ CoalescingExecutor,
107
137
  getMutex: new GetMutex(),
108
138
  };
package/lib/yaml.js CHANGED
@@ -4,7 +4,12 @@ const fse = require('fs-extra');
4
4
 
5
5
  exports.fromFile = function (yamlFile) {
6
6
  const yaml = require('js-yaml');
7
-
8
7
  const data = fse.readFileSync(yamlFile, 'utf8');
9
8
  return yaml.safeLoad(data);
10
9
  };
10
+
11
+ exports.toFile = function (data, yamlFile) {
12
+ const yaml = require('js-yaml');
13
+ data = yaml.safeDump(data, {lineWith: 999});
14
+ fse.writeFileSync(yamlFile, data, 'utf8');
15
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcraft-core-utils",
3
- "version": "4.19.1",
3
+ "version": "4.21.1",
4
4
  "description": "Xcraft utils",
5
5
  "main": "index.js",
6
6
  "engines": {
package/test/hash.spec.js CHANGED
@@ -3,7 +3,7 @@
3
3
  const {expect} = require('chai');
4
4
  const {computeHash} = require('../lib/hash.js');
5
5
 
6
- describe('goblin.yennefer.hash', function () {
6
+ describe('xcraft.utils.hash', function () {
7
7
  it('string', function () {
8
8
  const objL = {string: 'Roger'};
9
9
  const objR = {string: 'Wilco'};
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const {expect} = require('chai');
4
+ const {CoalescingExecutor} = require('../lib/locks.js');
5
+ const {setTimeout: setTimeoutAsync} = require('node:timers/promises');
6
+
7
+ describe('xcraft.utils.locks', function () {
8
+ it('barrier', async function () {
9
+ const results = [];
10
+ const promises = [];
11
+ const barrier = new CoalescingExecutor();
12
+
13
+ promises.push(
14
+ barrier.run(async () => {
15
+ await setTimeoutAsync(100);
16
+ results.push(0);
17
+ })
18
+ );
19
+ promises.push(
20
+ barrier.run(async () => {
21
+ await setTimeoutAsync(20);
22
+ results.push(1);
23
+ })
24
+ );
25
+ promises.push(
26
+ barrier.run(async () => {
27
+ await setTimeoutAsync(100);
28
+ results.push(2);
29
+ })
30
+ );
31
+
32
+ await Promise.all(promises);
33
+ expect(results).to.deep.equal([0, 2]);
34
+ });
35
+ });