From 43f5c9955a63ce983f05f63559166b61ada97679 Mon Sep 17 00:00:00 2001
From: dakedres <ramondolive@gmail.com>
Date: Sat, 10 Feb 2024 19:59:59 -0700
Subject: [PATCH] Simpler delayedFetch

---
 lib.js | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/lib.js b/lib.js
index e157292..a74baba 100644
--- a/lib.js
+++ b/lib.js
@@ -126,16 +126,14 @@ export async function fetchRss(source) {
 let waitingList = new Map()
 export const sleep = delay => new Promise(resolve => setTimeout(() => resolve(), delay) )
 export const delayedFetch = async (url, options, courtesyWait = 5 * 1000) => {
-  let [ domain ] = /[\w-]+.[\w-]+$/.exec(url.hostname)
-  let timeout = waitingList.get(domain) ?? 0
-  let now = Date.now()
+  let [ domain ] = /[\w-]+.[\w-]+$/.exec(new URL(url).hostname)
+  let waitFor = waitingList.get(domain) ?? 0
 
-  if(timeout == null || timeout <= now) {
-  } else {
-    await sleep(timeout - now)
+  waitingList.set(domain, waitFor + courtesyWait)
+  if(waitFor !== 0) {
+    await sleep(waitFor)
   }
 
-  waitingList.set(domain, now + courtesyWait)
   return await fetch(url, options)
 }