Skip to content

Commit

Permalink
refactor(client): Improve debug logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
darkobits committed Oct 31, 2020
1 parent 157e674 commit 7fef53a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/contexts/Inspirat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export const Provider = (props: React.PropsWithChildren<React.ReactNode>) => {
Reflect.defineProperty(window.debug, 'expiresIn', {
get: () => prettyMs(midnight() - now())
});
}, { once: 'setPhotoUpdateTimer::expiresIn' });
}, { once: true });

const timeoutHandle = setTimeout(() => {
ifDebug(() => console.debug('[setPhotoUpdateTimer] Updating photo.'));
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/lib/photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export async function getPhotoCollections() {
ifDebug(() => {
const expiresIn = CACHE_TTL - (now() - (photoCache?.updatedAt ?? 0));
console.debug(`[getPhotoCollections] Photo collection will be updated in ${prettyMs(expiresIn)}.`);
}, { once: 'getPhotoCollections::expiresIn' });
}, { once: true });
}

// If photoCache is still null at this point, we had no cached data and
Expand Down
53 changes: 28 additions & 25 deletions packages/client/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,27 @@ export function capitalizeWords(input: string): string {


/**
* @private
*
* Tracks debug callbacks provided to ifDebug.
*/
const debugOnceStatements: Array<string | Array<any>> = [];
const debugFns: Array<any> = [];


/**
* @private
*
* Invokes the provided callback and, if it makes any calls to console.debug,
* will only log the debug statement if it has not already been seen. If key is
* of type string, it will be used to make this determination. Otherwise, the
* arguments provided to console.debug will be used.
* Provided a string or a function, returns a string used as a de-duplication
* key.
*/
function debugOnce(key: boolean | string, cb: GenericFunction) {
const origDebug = console.debug;

console.debug = (...args: Array<any>) => {
const id = typeof key === 'string' ? key : args;

if (!R.includes(id, debugOnceStatements)) {
Reflect.apply(origDebug, console, args);
debugOnceStatements.push(id);
}
};
function computeDebugKey(value: string | GenericFunction) {
if (typeof value === 'string') {
return value;
}

cb();
if (typeof value === 'function') {
return value.toString();
}

console.debug = origDebug;
throw new Error(`[computeDebugKey] Expected first argument to be of type "string" or "function", got "${typeof value}".`);
}


Expand All @@ -95,13 +88,23 @@ export interface IfDebugOptions {
* query string param has been set.
*/
export function ifDebug(cb: GenericFunction, options?: IfDebugOptions) {
if (process.env.NODE_ENV === 'development' || queryString.parse(location.search).debug === 'true') {
if (options?.once) {
debugOnce(options.once, cb);
} else {
cb();
const hasDebugParam = Object.keys(queryString.parse(location.search)).includes('debug');

if (process.env.NODE_ENV !== 'development' && !hasDebugParam) {
return;
}

if (options?.once) {
const key = options.once === true ? computeDebugKey(cb) : computeDebugKey(options.once);

if (R.includes(key, debugFns)) {
return;
}

debugFns.push(key);
}

cb();
}


Expand Down

0 comments on commit 7fef53a

Please sign in to comment.