-
Notifications
You must be signed in to change notification settings - Fork 30
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 #20 from binance/rc-v0.3.7
Release v0.3.7
- Loading branch information
Showing
24 changed files
with
585 additions
and
467 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { WebsocketStream } from '../../src/index'; | ||
|
||
const callbacks = { | ||
open: () => console.debug('Connected to WebSocket server'), | ||
close: () => console.debug('Disconnected from WebSocket server'), | ||
message: (data: string) => console.info(data) | ||
}; | ||
|
||
const websocketStreamClient = new WebsocketStream({ callbacks }); | ||
|
||
websocketStreamClient.subscribe(['bnbusdt@depth', 'btcusdt@depth']); | ||
|
||
setTimeout(() => websocketStreamClient.disconnect(), 6000); |
Large diffs are not rendered by default.
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
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,74 @@ | ||
export enum LogLevel { | ||
NONE = '', | ||
DEBUG = 'debug', | ||
INFO = 'info', | ||
WARN = 'warn', | ||
ERROR = 'error', | ||
} | ||
|
||
export class Logger { | ||
private static instance: Logger; | ||
private minLogLevel: LogLevel = LogLevel.INFO; | ||
private readonly levelsOrder: LogLevel[] = [ | ||
LogLevel.NONE, | ||
LogLevel.DEBUG, | ||
LogLevel.INFO, | ||
LogLevel.WARN, | ||
LogLevel.ERROR, | ||
]; | ||
|
||
constructor() { } | ||
|
||
public static getInstance(): Logger { | ||
if (!Logger.instance) { | ||
Logger.instance = new Logger(); | ||
} | ||
return Logger.instance; | ||
} | ||
|
||
public setMinLogLevel(level: LogLevel): void { | ||
if (!this.isValidLogLevel(level)) { | ||
throw new Error(`Invalid log level: ${level}`); | ||
} | ||
this.minLogLevel = level; | ||
} | ||
|
||
private isValidLogLevel(level: LogLevel): boolean { | ||
return this.levelsOrder.includes(level); | ||
} | ||
|
||
private log(level: LogLevel, ...message: unknown[]): void { | ||
if (level === LogLevel.NONE || !this.allowLevelLog(level)) { | ||
return; | ||
} | ||
|
||
const timestamp = new Date().toISOString(); | ||
console[level](`[${timestamp}] [${level.toUpperCase()}]`, ...message); | ||
} | ||
|
||
private allowLevelLog(level: LogLevel): boolean { | ||
if (!this.isValidLogLevel(level)) { | ||
throw new Error(`Invalid log level: ${level}`); | ||
} | ||
|
||
const currentLevelIndex = this.levelsOrder.indexOf(level); | ||
const minLevelIndex = this.levelsOrder.indexOf(this.minLogLevel); | ||
return currentLevelIndex >= minLevelIndex; | ||
} | ||
|
||
public debug(...message: unknown[]): void { | ||
this.log(LogLevel.DEBUG, ...message); | ||
} | ||
|
||
public info(...message: unknown[]): void { | ||
this.log(LogLevel.INFO, ...message); | ||
} | ||
|
||
public warn(...message: unknown[]): void { | ||
this.log(LogLevel.WARN, ...message); | ||
} | ||
|
||
public error(...message: unknown[]): void { | ||
this.log(LogLevel.ERROR, ...message); | ||
} | ||
} |
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
Oops, something went wrong.