-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
event dispatch pattern, connection status (#29)
- Loading branch information
Showing
8 changed files
with
118 additions
and
39 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 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,45 @@ | ||
import { OpaqueTransportMessage } from './message'; | ||
import { Connection } from './transport'; | ||
|
||
export interface EventMap { | ||
message: OpaqueTransportMessage; | ||
connectionStatus: { | ||
status: 'connect' | 'disconnect'; | ||
conn: Connection; | ||
}; | ||
} | ||
|
||
export type EventTypes = keyof EventMap; | ||
export type EventHandler<K extends EventTypes> = (event: EventMap[K]) => void; | ||
|
||
export class EventDispatcher<T extends EventTypes> { | ||
private eventListeners: { [K in T]?: Set<EventHandler<K>> } = {}; | ||
|
||
numberOfListeners<K extends T>(eventType: K) { | ||
return this.eventListeners[eventType]?.size ?? 0; | ||
} | ||
|
||
addEventListener<K extends T>(eventType: K, handler: EventHandler<K>) { | ||
if (!this.eventListeners[eventType]) { | ||
this.eventListeners[eventType] = new Set(); | ||
} | ||
|
||
this.eventListeners[eventType]?.add(handler); | ||
} | ||
|
||
removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>) { | ||
const handlers = this.eventListeners[eventType]; | ||
if (handlers) { | ||
this.eventListeners[eventType]?.delete(handler); | ||
} | ||
} | ||
|
||
dispatchEvent<K extends T>(eventType: K, event: EventMap[K]) { | ||
const handlers = this.eventListeners[eventType]; | ||
if (handlers) { | ||
for (const handler of handlers) { | ||
handler(event); | ||
} | ||
} | ||
} | ||
} |
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