-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delegate control to process queue. (#313)
* Reduce LoadProgress object type. * Create microtask for process queue. * Add event dispatcher class. * Create Request class. * Move Request file to separate file. * Change style of bittorrent-tracker declarations event handlers types. * Create P2PTrackerClient class to encapsulate peer connection logic. * Add TODO task. * Add requests error handling. * Fix types errors. * P2P announce segments on http requests state change. * Fix bugs. * Make broadcastAnnouncement method as function expression. * Refactor Request code. Create separate flows for each type of abort. * Remove event subscriptions to request instance. * Fix type errors. * Fix bugs. * Fix lint warnings. * Move P2P tracker client to separate file. * Fix more bugs. Rewrite loggers. * Remove unused code. * Use function declaration instead of expression. --------- Co-authored-by: Igor Zolotarenko <[email protected]>
- Loading branch information
1 parent
3b0b69a
commit 4a72e36
Showing
26 changed files
with
1,175 additions
and
1,160 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
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
export class EventDispatcher< | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
T extends { [key: string]: (...args: any[]) => void | Promise<void> }, | ||
K extends keyof T = keyof T | ||
> { | ||
private readonly listeners = new Map<keyof T, Set<T[K]>>(); | ||
|
||
subscribe(eventType: K, ...listeners: T[K][]) { | ||
let eventListeners = this.listeners.get(eventType); | ||
if (!eventListeners) { | ||
eventListeners = new Set(); | ||
this.listeners.set(eventType, eventListeners); | ||
} | ||
for (const listener of listeners) eventListeners.add(listener); | ||
} | ||
|
||
unsubscribe(eventType: K, listener: T[K]) { | ||
const eventListeners = this.listeners.get(eventType); | ||
if (!eventListeners) return; | ||
eventListeners.delete(listener); | ||
if (!eventListeners.size) this.listeners.delete(eventType); | ||
} | ||
|
||
dispatch(eventType: K, ...args: Parameters<T[K]>) { | ||
const eventListeners = this.listeners.get(eventType); | ||
if (!eventListeners) return; | ||
for (const listener of eventListeners) { | ||
listener(...args); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.