-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Transport into an abstract class
Create parent destroy() and make sure 'close' always is emitted, child classes implement their own _destroy() logic.
- Loading branch information
1 parent
2c50175
commit 2a22cbd
Showing
4 changed files
with
45 additions
and
42 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 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,33 @@ | ||
import { EventEmitter } from 'events'; | ||
import { Duplex } from 'stream'; | ||
|
||
export type TransportConnectionOptions = { | ||
tunnelId?: string, | ||
port?: number, | ||
}; | ||
|
||
export interface TransportOptions { | ||
max_connections?: number | ||
} | ||
|
||
export default abstract class Transport extends EventEmitter { | ||
public readonly max_connections: number; | ||
public destroyed: boolean = false; | ||
|
||
constructor(opts: TransportOptions) { | ||
super(); | ||
this.max_connections = opts.max_connections || 1; | ||
} | ||
|
||
public abstract createConnection(opts: TransportConnectionOptions, callback: (err: Error | undefined, sock: Duplex) => void): Duplex; | ||
|
||
protected abstract _destroy(): Promise<void>; | ||
|
||
public async destroy(err?: Error): Promise<void> { | ||
this.destroyed = true; | ||
this._destroy(); | ||
this.emit('close', err); | ||
this.removeAllListeners(); | ||
} | ||
|
||
} |
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