zaraz 1.0.0 → 2.0.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.
- package/lib/zaraz.js +21 -20
- package/package.json +4 -2
- package/index.js +0 -1
package/lib/zaraz.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import Debug from 'debug';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const debug = Debug('zaraz');
|
|
4
4
|
|
|
5
5
|
const queues = {
|
|
6
6
|
timer: undefined,
|
|
@@ -9,29 +9,30 @@ const queues = {
|
|
|
9
9
|
later: []
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
class Item {
|
|
13
|
+
#fn;
|
|
14
|
+
#params;
|
|
15
|
+
constructor(fn, params) {
|
|
16
|
+
this.#fn = fn;
|
|
17
|
+
this.#params = params;
|
|
18
|
+
}
|
|
18
19
|
|
|
19
20
|
run() {
|
|
20
21
|
// bail if nothing to run
|
|
21
|
-
if (!this
|
|
22
|
+
if (!this.#fn) return;
|
|
22
23
|
|
|
23
|
-
const fn = this
|
|
24
|
+
const fn = this.#fn;
|
|
24
25
|
this.clear();
|
|
25
|
-
fn(...this
|
|
26
|
-
}
|
|
26
|
+
fn(...this.#params);
|
|
27
|
+
}
|
|
27
28
|
|
|
28
29
|
clear() {
|
|
29
|
-
this
|
|
30
|
+
this.#fn = undefined;
|
|
30
31
|
}
|
|
31
|
-
}
|
|
32
|
+
}
|
|
32
33
|
|
|
33
|
-
function zaraz(fn, ...params) {
|
|
34
|
-
const item =
|
|
34
|
+
export default function zaraz(fn, ...params) {
|
|
35
|
+
const item = new Item(fn, params);
|
|
35
36
|
const q = queues.index > 0 ? queues.later : queues.current;
|
|
36
37
|
q.push(item);
|
|
37
38
|
|
|
@@ -39,9 +40,9 @@ function zaraz(fn, ...params) {
|
|
|
39
40
|
return item;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
zaraz.DELAY = 10;
|
|
43
|
-
zaraz.ACTIVE = 100;
|
|
44
|
-
zaraz.MAX_ITEMS = 1000;
|
|
43
|
+
zaraz.DELAY = 10; // default delay
|
|
44
|
+
zaraz.ACTIVE = 100; // how long before we yield after processing items
|
|
45
|
+
zaraz.MAX_ITEMS = 1000; // how many items we can process in a single step
|
|
45
46
|
|
|
46
47
|
function execute() {
|
|
47
48
|
const start = Date.now();
|
|
@@ -49,7 +50,7 @@ function execute() {
|
|
|
49
50
|
|
|
50
51
|
debug('queues before', queues.current.length - queues.index, queues.later.length);
|
|
51
52
|
|
|
52
|
-
while(queues.index < max) {
|
|
53
|
+
while (queues.index < max) {
|
|
53
54
|
queues.current[queues.index++].run();
|
|
54
55
|
if (Date.now() - start > zaraz.ACTIVE) {
|
|
55
56
|
debug('breaking...');
|
package/package.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zaraz",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Cheap way to introduce short async delay.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./index.js",
|
|
5
7
|
"author": {
|
|
6
8
|
"name": "Damian Krzeminski",
|
|
7
9
|
"email": "pirxpilot@furkot.com",
|
|
@@ -22,7 +24,7 @@
|
|
|
22
24
|
"debug": "*"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
25
|
-
"@
|
|
27
|
+
"@biomejs/biome": "^1.9.4"
|
|
26
28
|
},
|
|
27
29
|
"scripts": {
|
|
28
30
|
"test": "make check"
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./lib/zaraz');
|