Skip to content

Commit

Permalink
chore rename util.js to Logger.js and refactor functions to a class
Browse files Browse the repository at this point in the history
  • Loading branch information
asieduernest12 committed Jun 3, 2022
1 parent e1c20b3 commit d804139
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
7 changes: 4 additions & 3 deletions src/ContentScript/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logError, logTime } from '../utills';
import Logger from '../Logger';
import NodeObserver from './observer';

const runTimeHandler = typeof browser === 'undefined' ? chrome : browser;
Expand Down Expand Up @@ -69,6 +69,7 @@ function parseNode(/** @type Element */ node) {
}

function mutationCallback(/** @type MutationRecord[] */ mutationRecords) {
Logger.logInfo('mutationCallback fired ', mutationRecords.length);
mutationRecords.forEach(({ type, addedNodes }) => {
if (type !== 'childList') return;

Expand All @@ -77,7 +78,7 @@ function mutationCallback(/** @type MutationRecord[] */ mutationRecords) {
}

const ToggleReading = (enableReading) => {
const endTimer = logTime('ToggleReading-Time');
const endTimer = Logger.logTime('ToggleReading-Time');
try {
const boldedElements = document.getElementsByTagName('br-bold');

Expand Down Expand Up @@ -107,7 +108,7 @@ const ToggleReading = (enableReading) => {
}
}
} catch (error) {
logError(error);
Logger.logError(error);
} finally {
endTimer();
}
Expand Down
40 changes: 40 additions & 0 deletions src/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
* provide development utility functions for reporting errors, logging data and logging time
*/

/* eslint-disable no-console */
export default class Logger {
static #isProductionEnv = () => process.env.NODE_ENV === 'production';

static logError = (error) => {
if (this.#isProductionEnv()) return;
console.error(error);
};

/**
*
* @param {...any} data
* @returns {void}
*/
static logInfo = (...data) => {
if (this.#isProductionEnv()) return;

console.log(...data);
};

/**
*
* @param {String} label
* @returns {Function} end and display time when called in non production environment
*/
static logTime = (label) => {
if (this.#isProductionEnv()) {
return () => {
// no-op}
};
}
console.time(label);
return () => console.timeEnd(label);
};
}
39 changes: 0 additions & 39 deletions src/utills.js

This file was deleted.

0 comments on commit d804139

Please sign in to comment.