Tone.js/Tone/core/util/Debug.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

/**
2019-09-14 20:39:18 +00:00
* Assert that the statement is true, otherwise invoke an error with the given message.
*/
export function assert(statement: boolean, error: string): void {
if (!statement) {
throw new Error(error);
}
}
2019-10-09 16:38:10 +00:00
/**
* Make sure that the given value is within the range
*/
export function assertRange(value: number, gte: number, lte: number = Infinity): void {
if (!(gte <= value && value <= lte)) {
throw new RangeError(`Value must be within [${gte}, ${lte}], got: ${value}`);
}
}
/**
* Make sure that the given value is within the range
*/
export function assertContextRunning(context: import("../context/BaseContext").BaseContext): void {
// add a warning if the context is not started
if (!context.isOffline && context.state !== "running") {
warn("The AudioContext is \"suspended\". Invoke Tone.start() from a user action to start the audio.");
}
}
/**
* A basic logging interface
*/
interface Logger {
log: (args?: any[]) => void;
warn: (args?: any[]) => void;
}
/**
* The default logger is the console
*/
let defaultLogger: Logger = console;
/**
* Set the logging interface
*/
2019-09-20 21:49:54 +00:00
export function setLogger(logger: Logger): void {
defaultLogger = logger;
}
/**
* Log anything
*/
export function log(...args: any[]): void {
defaultLogger.log(...args);
}
/**
* Warn anything
*/
export function warn(...args: any[]): void {
defaultLogger.warn(...args);
}