Add explicit types for return in methods

This commit is contained in:
RMidhunSuresh 2021-11-12 23:12:15 +05:30
parent 8c7a765e11
commit 29a8260514
2 changed files with 20 additions and 17 deletions

View file

@ -21,7 +21,7 @@ import type {BaseLogger} from "./BaseLogger";
interface ISerializedItem { interface ISerializedItem {
s: number; s: number;
d: number | null; d?: number;
v: LogItemValues; v: LogItemValues;
l: LogLevel; l: LogLevel;
e?: { e?: {
@ -70,7 +70,7 @@ export class LogItem {
} }
/** 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: LogLevelOrNull, filterCreator: FilterCreator) { runDetached(labelOrValues: LabelOrValues, callback: LogCallback, logLevel: LogLevelOrNull, filterCreator: FilterCreator): LogItem {
return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator); return this._logger.runDetached(labelOrValues, callback, logLevel, filterCreator);
} }
@ -83,7 +83,7 @@ export class LogItem {
This is useful if the referenced operation can't be awaited. */ This is useful if the referenced operation can't be awaited. */
refDetached(logItem: LogItem, logLevel: LogLevelOrNull = null) { refDetached(logItem: LogItem, logLevel: LogLevelOrNull = null) {
logItem.ensureRefId(); logItem.ensureRefId();
return this.log({ref: logItem._values.refId as number}, logLevel); this.log({ref: logItem._values.refId as number}, logLevel);
} }
ensureRefId() { ensureRefId() {
@ -100,27 +100,30 @@ export class LogItem {
return item.run(callback); return item.run(callback);
} }
get duration() { get duration(): number | undefined{
if (this.end) { if (this.end) {
return this.end - this.start; return this.end - this.start;
} else { } else {
return null; return undefined;
} }
} }
durationWithoutType(type: string) { durationWithoutType(type: string): number | undefined{
if (this.duration) { const durationOfType = this.durationOfType(type);
return this.duration - this.durationOfType(type); if (this.duration && durationOfType) {
return this.duration - durationOfType;
} }
return null;
} }
durationOfType(type: string) { durationOfType(type: string): number | undefined {
if (this._values.t === type) { if (this._values.t === type) {
return this.duration; return this.duration;
} else if (this._children) { } else if (this._children) {
return this._children.reduce((sum, c) => { return this._children.reduce((sum, c) => {
return sum + c.durationOfType(type); const duration = c.durationOfType(type);
if (duration) {
return sum + duration;
}
}, 0); }, 0);
} else { } else {
return 0; return 0;
@ -213,11 +216,11 @@ export class LogItem {
* @param {Function} callback [description] * @param {Function} callback [description]
* @return {[type]} [description] * @return {[type]} [description]
*/ */
run(callback: LogCallback) { run(callback: LogCallback): unknown {
if (this.end !== null) { if (this.end !== null) {
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; let result: unknown;
try { try {
result = callback(this); result = callback(this);
if (result instanceof Promise) { if (result instanceof Promise) {
@ -252,18 +255,18 @@ export class LogItem {
} }
// expose log level without needing import everywhere // expose log level without needing import everywhere
get level() { get level(): typeof LogLevel {
return LogLevel; return LogLevel;
} }
catch(err: Error) { catch(err: Error): Error {
this.error = err; this.error = err;
this.logLevel = LogLevel.Error; this.logLevel = LogLevel.Error;
this.finish(); this.finish();
return err; return err;
} }
child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator: FilterCreator) { child(labelOrValues: LabelOrValues, logLevel: LogLevelOrNull, filterCreator: FilterCreator): LogItem {
if (this.end !== null) { if (this.end !== null) {
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");
} }

View file

@ -101,5 +101,5 @@ async function createStores(db: IDBDatabase, txn: IDBTransaction, oldVersion: nu
const migrationFunc = schema[i]; const migrationFunc = schema[i];
await log.wrap(`v${i + 1}`, log => migrationFunc(db, txn, localStorage, log)); await log.wrap(`v${i + 1}`, log => migrationFunc(db, txn, localStorage, log));
} }
}); }) as Promise<void>;
} }