Skip to content

Commit

Permalink
Add global debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
hansottowirtz committed Jan 6, 2023
1 parent 86ce4ff commit c6bb49a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions .storybook/preview-head.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<script>
window.global = window;
window.__debug_react_done_tracker = true;
</script>
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ Unlike Suspense, with done trackers you cannot re-suspend from inside the tree,

You can easily use Done Trackers and Suspense together, see [this example](https://react-done-tracker.vercel.app?path=/docs/contextual-api-suspense--docs).

## How can I debug this?

Run `window.__debug_react_done_tracker = true` before importing the library, and you will see logs of done tracker events, as well as the state of a done tracker tree when its doneness is being checked.

You can print the state of a done tracker tree to the console with `doneTracker.log()`.

Next to that, the `useDoneTrackerRaw` hook uses `useDebugValue` which displays the done tracker state in React DevTools.

## More examples

It's best to take a look at [Storybook](https://react-done-tracker.vercel.app) first to get a feeling of how this library can be used.
Expand Down
5 changes: 5 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare global {
// eslint-disable-next-line no-var
var __debug_react_done_tracker: boolean;
}
export const DEBUG = typeof globalThis === "object" && globalThis.__debug_react_done_tracker;
14 changes: 11 additions & 3 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export const debug = console.debug.bind(window.console, "[Done Tracker]");
export const log = console.log.bind(window.console, "[Done Tracker]");
export const warn = console.warn.bind(window.console, "[Done Tracker]");
import { DEBUG } from "./debug";

export const debug = DEBUG
? console.debug.bind(window.console, "[Done Tracker]")
: () => undefined;
export const log = DEBUG
? console.log.bind(window.console, "[Done Tracker]")
: () => undefined;
export const warn = DEBUG
? console.warn.bind(window.console, "[Done Tracker]")
: () => undefined;
17 changes: 10 additions & 7 deletions src/node-done-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseDoneTracker } from "./base-done-tracker";
import { DEBUG } from "./debug";
import { DoneTracker } from "./done-tracker-interface";
import { getUniqueId } from "./get-unique-id";
import { log, warn, debug } from "./log";
Expand Down Expand Up @@ -156,13 +157,15 @@ export class NodeDoneTracker extends BaseDoneTracker implements DoneTracker {
(child) => child.done
).length;

console.groupCollapsed(
"[Done Tracker]",
"🧮 Calculating doneness",
this.id
);
this.log();
console.groupEnd();
if (DEBUG) {
console.groupCollapsed(
"[Done Tracker]",
"🧮 Calculating doneness",
this.id
);
this.log();
console.groupEnd();
}
if (this._done) return;
if (this._willHaveChildren && this.children.size === 0) {
log("🚧 Will have children so not done yet", this.id);
Expand Down

0 comments on commit c6bb49a

Please sign in to comment.