Skip to content

Commit 5aa0ac5

Browse files
committed
refactor: import sleep directly; dedup randomIndex
Drop the clock.js re-export — consumers import src/sleep.js directly (global-setup already did). Extract the byte-identical _randomIndex bodies in Bottleneck and Job into src/random-index.js. No utils junk drawer: one concept per module stays the house style.
1 parent 23787ad commit 5aa0ac5

9 files changed

Lines changed: 16 additions & 14 deletions

File tree

src/Bottleneck.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Events = require("./Events");
1010
const States = require("./States");
1111
const Sync = require("./Sync");
1212
const BottleneckError = require("./BottleneckError");
13+
const randomIndex = require("./random-index");
1314
const Group = require("./Group");
1415
const RedisConnection = require("./cluster/RedisConnection");
1516
const IORedisConnection = require("./cluster/IORedisConnection");
@@ -179,7 +180,7 @@ class Bottleneck {
179180
}
180181

181182
_randomIndex() {
182-
return Math.random().toString(36).slice(2);
183+
return randomIndex();
183184
}
184185

185186
check(weight = 1) {

src/Job.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const DEFAULT_PRIORITY = 5;
33

44
const parser = require("./parser");
55
const BottleneckError = require("./BottleneckError");
6+
const randomIndex = require("./random-index");
67

78
class Job {
89
constructor(task, args, options, jobDefaults, rejectOnDrop, Events, _states) {
@@ -14,7 +15,7 @@ class Job {
1415
this.options = parser.load(options, jobDefaults);
1516
this.options.priority = this._sanitizePriority(this.options.priority);
1617
if (this.options.id === jobDefaults.id) {
17-
this.options.id = `${this.options.id}-${this._randomIndex()}`;
18+
this.options.id = `${this.options.id}-${randomIndex()}`;
1819
}
1920
this.promise = new Promise((_resolve, _reject) => {
2021
this._resolve = _resolve;
@@ -34,10 +35,6 @@ class Job {
3435
}
3536
}
3637

37-
_randomIndex() {
38-
return Math.random().toString(36).slice(2);
39-
}
40-
4138
doDrop(params) {
4239
const { error, message = "This job has been dropped by Bottleneck" } = params || {};
4340
if (this._states.remove(this.options.id)) {

src/random-index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** Compact random id used for job indexes and datastore client ids. */
2+
const randomIndex = () => Math.random().toString(36).slice(2);
3+
4+
module.exports = randomIndex;

test/batcher.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect } from "vitest";
2-
import { useFakeClock, sleep } from "./helpers/clock.js";
2+
import sleep from "../src/sleep.js";
3+
import { useFakeClock } from "./helpers/clock.js";
34
import { test } from "./helpers/test-api.js";
45
const Bottleneck = require("./bottleneck");
56

test/cluster-coordination.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect } from "vitest";
2-
import { sleep } from "./helpers/clock.js";
2+
import sleep from "../src/sleep.js";
33
import { test, waitForState, deferred } from "./helpers/test-api.js";
44
const Bottleneck = require("./bottleneck");
55
const Scripts = require("../src/cluster/Scripts.js");

test/general-traffic.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect } from "vitest";
2-
import { sleep, useFakeClock, useRealClockForThisTest } from "./helpers/clock.js";
2+
import sleep from "../src/sleep.js";
3+
import { useFakeClock, useRealClockForThisTest } from "./helpers/clock.js";
34
import { test, waitForState, deferred } from "./helpers/test-api.js";
45

56
const path = require("path");

test/group.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect } from "vitest";
2-
import { useFakeClock, sleep } from "./helpers/clock.js";
2+
import sleep from "../src/sleep.js";
3+
import { useFakeClock } from "./helpers/clock.js";
34
import { test, waitForState } from "./helpers/test-api.js";
45
const Bottleneck = require("./bottleneck");
56

test/helpers/clock.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,3 @@ export function useFakeClock() {
3737
export function useRealClockForThisTest() {
3838
vi.useRealTimers();
3939
}
40-
41-
// Shared with src: reads the global setTimeout at call time, so it respects fake timers when installed.
42-
export { default as sleep } from "../../src/sleep.js";

test/helpers/job-tasks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sleep } from "./clock.js";
1+
import sleep from "../../src/sleep.js";
22

33
/**
44
* Manually-released signal for {@link createTaskFns}'s deferredJob/deferredPromise.

0 commit comments

Comments
 (0)