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() | let waitingList = new Map() | ||||||
| export const sleep = delay => new Promise(resolve => setTimeout(() => resolve(), delay) ) | export const sleep = delay => new Promise(resolve => setTimeout(() => resolve(), delay) ) | ||||||
| export const delayedFetch = async (url, options, courtesyWait = 5 * 1000) => { | export const delayedFetch = async (url, options, courtesyWait = 5 * 1000) => { | ||||||
|   let [ domain ] = /[\w-]+.[\w-]+$/.exec(url.hostname) |   let [ domain ] = /[\w-]+.[\w-]+$/.exec(new URL(url).hostname) | ||||||
|   let timeout = waitingList.get(domain) ?? 0 |   let waitFor = waitingList.get(domain) ?? 0 | ||||||
|   let now = Date.now() |  | ||||||
|  |  | ||||||
|   if(timeout == null || timeout <= now) { |   waitingList.set(domain, waitFor + courtesyWait) | ||||||
|   } else { |   if(waitFor !== 0) { | ||||||
|     await sleep(timeout - now) |     await sleep(waitFor) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   waitingList.set(domain, now + courtesyWait) |  | ||||||
|   return await fetch(url, options) |   return await fetch(url, options) | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user