Simpler delayedFetch
This commit is contained in:
		
							
								
								
									
										12
									
								
								lib.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								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) | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user