Skip to content

Commit

Permalink
Use timeout instead of interval for releasing ports (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jun 4, 2023
1 parent 0760c98 commit bd8a403
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions index.js
Expand Up @@ -20,8 +20,8 @@ const releaseOldLockedPortsIntervalMs = 1000 * 15;
const minPort = 1024;
const maxPort = 65_535;

// Lazily create interval on first use
let interval;
// Lazily create timeout on first use
let timeout;

const getLocalHosts = () => {
const interfaces = os.networkInterfaces();
Expand Down Expand Up @@ -109,15 +109,17 @@ export default async function getPorts(options) {
}
}

if (interval === undefined) {
interval = setInterval(() => {
if (timeout === undefined) {
timeout = setTimeout(() => {
timeout = undefined;

lockedPorts.old = lockedPorts.young;
lockedPorts.young = new Set();
}, releaseOldLockedPortsIntervalMs);

// Does not exist in some environments (Electron, Jest jsdom env, browser, etc).
if (interval.unref) {
interval.unref();
if (timeout.unref) {
timeout.unref();
}
}

Expand Down

0 comments on commit bd8a403

Please sign in to comment.