-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from mlbiam/main
added dialogs for pulling URLs, throttling requested
- Loading branch information
Showing
4 changed files
with
173 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
class Semaphore { | ||
|
||
|
||
|
||
constructor(maxConcurrency) { | ||
this.maxConcurrency = maxConcurrency; | ||
this.currentCount = 0; | ||
this.queue = []; | ||
this.activeAbortControllers = new Set(); // Track active AbortControllers | ||
|
||
} | ||
|
||
async acquire() { | ||
if (this.currentCount < this.maxConcurrency) { | ||
this.currentCount++; | ||
} else { | ||
await new Promise(resolve => this.queue.push(resolve)); | ||
} | ||
} | ||
|
||
release() { | ||
if (this.queue.length > 0) { | ||
const nextResolve = this.queue.shift(); | ||
nextResolve(); | ||
} else { | ||
this.currentCount--; | ||
} | ||
} | ||
|
||
|
||
|
||
async waitUntilEmpty() { | ||
return new Promise(resolve => { | ||
const checkQueue = () => { | ||
if (this.queue.length === 0 && this.currentCount === 0) { | ||
resolve(); | ||
} else { | ||
setTimeout(checkQueue, 50); // Check again after a short delay | ||
} | ||
}; | ||
checkQueue(); | ||
}); | ||
} | ||
|
||
getAbortController() { | ||
const controller = new AbortController(); | ||
this.activeAbortControllers.add(controller); // Add to active controllers | ||
|
||
controller.signal.addEventListener('abort', () => { | ||
this.activeAbortControllers.delete(controller); // Remove when aborted | ||
}); | ||
|
||
return controller; | ||
} | ||
|
||
cancelAll= () => { | ||
for (const controller of this.activeAbortControllers) { | ||
controller.abort(); // Abort all active controllers | ||
} | ||
this.activeAbortControllers.clear(); // Clear the set | ||
} | ||
|
||
} | ||
|
||
export default Semaphore; |