Use generics over returning unknown
This commit is contained in:
parent
fe69f84c85
commit
4c1d7a8f2d
3 changed files with 19 additions and 18 deletions
|
@ -21,6 +21,7 @@ import type {FilterCreator, LabelOrValues, LogCallback, ILogItem} from "./LogIte
|
||||||
// todo: should this import be here just for getting the type? should it instead be done when Platform.js --> Platform.ts?
|
// todo: should this import be here just for getting the type? should it instead be done when Platform.js --> Platform.ts?
|
||||||
import type {Platform} from "../platform/web/Platform.js";
|
import type {Platform} from "../platform/web/Platform.js";
|
||||||
|
|
||||||
|
type RunResult<T> = T | void | Promise<T | void>;
|
||||||
|
|
||||||
export abstract class BaseLogger {
|
export abstract class BaseLogger {
|
||||||
protected _openItems: Set<ILogItem> = new Set();
|
protected _openItems: Set<ILogItem> = new Set();
|
||||||
|
@ -37,7 +38,7 @@ export abstract class BaseLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */
|
/** if item is a log item, wrap the callback in a child of it, otherwise start a new root log item. */
|
||||||
wrapOrRun(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown {
|
wrapOrRun<T>(item: ILogItem, labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult<T> {
|
||||||
if (item) {
|
if (item) {
|
||||||
return item.wrap(labelOrValues, callback, logLevel, filterCreator);
|
return item.wrap(labelOrValues, callback, logLevel, filterCreator);
|
||||||
} else {
|
} else {
|
||||||
|
@ -50,7 +51,7 @@ export abstract class BaseLogger {
|
||||||
Useful to pair with LogItem.refDetached.
|
Useful to pair with LogItem.refDetached.
|
||||||
|
|
||||||
@return {LogItem} the log item added, useful to pass to LogItem.refDetached */
|
@return {LogItem} the log item added, useful to pass to LogItem.refDetached */
|
||||||
runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem {
|
runDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem {
|
||||||
// todo: Remove jsdoc type?
|
// todo: Remove jsdoc type?
|
||||||
if (!logLevel) {
|
if (!logLevel) {
|
||||||
logLevel = LogLevel.Info;
|
logLevel = LogLevel.Info;
|
||||||
|
@ -63,7 +64,7 @@ export abstract class BaseLogger {
|
||||||
/** run a callback wrapped in a log operation.
|
/** run a callback wrapped in a log operation.
|
||||||
Errors and duration are transparently logged, also for async operations.
|
Errors and duration are transparently logged, also for async operations.
|
||||||
Whatever the callback returns is returned here. */
|
Whatever the callback returns is returned here. */
|
||||||
run(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown {
|
run<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): RunResult<T> {
|
||||||
if (!logLevel) {
|
if (!logLevel) {
|
||||||
logLevel = LogLevel.Info;
|
logLevel = LogLevel.Info;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +72,7 @@ export abstract class BaseLogger {
|
||||||
return this._run(item, callback, logLevel!, true, filterCreator);
|
return this._run(item, callback, logLevel!, true, filterCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
_run(item: ILogItem, callback: LogCallback, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): unknown {
|
_run<T>(item: ILogItem, callback: LogCallback<T>, logLevel: LogLevel, shouldThrow: boolean, filterCreator?: FilterCreator): RunResult<T> {
|
||||||
this._openItems.add(item);
|
this._openItems.add(item);
|
||||||
|
|
||||||
const finishItem = () => {
|
const finishItem = () => {
|
||||||
|
|
|
@ -42,12 +42,12 @@ export interface ILogItem {
|
||||||
children?: Array<ILogItem>;
|
children?: Array<ILogItem>;
|
||||||
values: LogItemValues;
|
values: LogItemValues;
|
||||||
error?: Error;
|
error?: Error;
|
||||||
wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown;
|
wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise<T>;
|
||||||
log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void;
|
log(labelOrValues: LabelOrValues, logLevel?: LogLevel): void;
|
||||||
set(key: string | object, value: unknown): void;
|
set(key: string | object, value: unknown): void;
|
||||||
run(callback: LogCallback): unknown;
|
run<T>(callback: LogCallback<T>): T | Promise<T>;
|
||||||
runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem;
|
runDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem;
|
||||||
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void;
|
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): void;
|
||||||
refDetached(logItem: ILogItem, logLevel?: LogLevel): void;
|
refDetached(logItem: ILogItem, logLevel?: LogLevel): void;
|
||||||
ensureRefId(): void;
|
ensureRefId(): void;
|
||||||
catch(err: Error): Error;
|
catch(err: Error): Error;
|
||||||
|
@ -68,7 +68,7 @@ export type LogItemValues = {
|
||||||
|
|
||||||
export type LabelOrValues = string | LogItemValues;
|
export type LabelOrValues = string | LogItemValues;
|
||||||
export type FilterCreator = ((filter: LogFilter, item: ILogItem) => LogFilter);
|
export type FilterCreator = ((filter: LogFilter, item: ILogItem) => LogFilter);
|
||||||
export type LogCallback = (item: ILogItem) => unknown;
|
export type LogCallback<T> = (item: ILogItem) => T;
|
||||||
|
|
||||||
export class LogItem implements ILogItem {
|
export class LogItem implements ILogItem {
|
||||||
public readonly start: number;
|
public readonly start: number;
|
||||||
|
@ -90,12 +90,12 @@ export class LogItem implements ILogItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** start a new root log item and run it detached mode, see BaseLogger.runDetached */
|
/** start a new root log item and run it detached mode, see BaseLogger.runDetached */
|
||||||
runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem {
|
runDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): ILogItem {
|
||||||
return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator);
|
return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** start a new detached root log item and log a reference to it from this item */
|
/** start a new detached root log item and log a reference to it from this item */
|
||||||
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): void {
|
wrapDetached(labelOrValues: LabelOrValues, callback: LogCallback<unknown>, logLevel?: LogLevel, filterCreator?: FilterCreator): void {
|
||||||
this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator));
|
this.refDetached(this.runDetached(labelOrValues, callback, logLevel, filterCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +115,7 @@ export class LogItem implements ILogItem {
|
||||||
/**
|
/**
|
||||||
* Creates a new child item and runs it in `callback`.
|
* Creates a new child item and runs it in `callback`.
|
||||||
*/
|
*/
|
||||||
wrap(labelOrValues: LabelOrValues, callback: LogCallback, logLevel?: LogLevel, filterCreator?: FilterCreator): unknown {
|
wrap<T>(labelOrValues: LabelOrValues, callback: LogCallback<T>, logLevel?: LogLevel, filterCreator?: FilterCreator): T | Promise<T> {
|
||||||
const item = this.child(labelOrValues, logLevel, filterCreator);
|
const item = this.child(labelOrValues, logLevel, filterCreator);
|
||||||
return item.run(callback);
|
return item.run(callback);
|
||||||
}
|
}
|
||||||
|
@ -237,11 +237,11 @@ export class LogItem implements ILogItem {
|
||||||
* @param {Function} callback [description]
|
* @param {Function} callback [description]
|
||||||
* @return {[type]} [description]
|
* @return {[type]} [description]
|
||||||
*/
|
*/
|
||||||
run(callback: LogCallback): unknown {
|
run<T>(callback: LogCallback<T>): T | Promise<T> {
|
||||||
if (this.end) {
|
if (this.end) {
|
||||||
console.trace("log item is finished, additional logs will likely not be recorded");
|
console.trace("log item is finished, additional logs will likely not be recorded");
|
||||||
}
|
}
|
||||||
let result: unknown;
|
let result: T | Promise<T>;
|
||||||
try {
|
try {
|
||||||
result = callback(this);
|
result = callback(this);
|
||||||
if (result instanceof Promise) {
|
if (result instanceof Promise) {
|
||||||
|
|
|
@ -60,22 +60,22 @@ export class NullLogItem implements ILogItem {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
wrap(_: LabelOrValues, callback: LogCallback): unknown {
|
wrap<T>(_: LabelOrValues, callback: LogCallback<T>): T | Promise<T> {
|
||||||
return callback(this);
|
return callback(this);
|
||||||
}
|
}
|
||||||
log(): void {}
|
log(): void {}
|
||||||
set(): void {}
|
set(): void {}
|
||||||
|
|
||||||
runDetached(_: LabelOrValues, callback: LogCallback): ILogItem {
|
runDetached(_: LabelOrValues, callback: LogCallback<unknown>): ILogItem {
|
||||||
new Promise(r => r(callback(this))).then(noop, noop);
|
new Promise(r => r(callback(this))).then(noop, noop);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
wrapDetached(_: LabelOrValues, _callback: LogCallback): void {
|
wrapDetached(_: LabelOrValues, _callback: LogCallback<unknown>): void {
|
||||||
return this.refDetached();
|
return this.refDetached();
|
||||||
}
|
}
|
||||||
|
|
||||||
run(callback: LogCallback): unknown {
|
run<T>(callback: LogCallback<T>): T | Promise<T> {
|
||||||
return callback(this);
|
return callback(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue