Skip to content

Commit 075022a

Browse files
committed
configurable debug log
1 parent f63b920 commit 075022a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const extensions = [
2222
const EMPTY_DOC: JSONContent = { type: "doc", content: [] };
2323

2424
function App(props: { id: string }) {
25-
const sync = useTiptapSync(api.example, props.id);
25+
const sync = useTiptapSync(api.example, props.id, { debug: true });
2626
if (!sync.isLoading && sync.initialContent === null) {
2727
sync.create(EMPTY_DOC);
2828
}

src/tiptap/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ import { SyncApi } from "../client";
1414
// How many steps we will attempt to sync in one request.
1515
const MAX_STEPS_SYNC = 1000;
1616
const SNAPSHOT_DEBOUNCE_MS = 1000;
17-
const log: typeof console.log = console.debug;
1817

1918
type UseSyncOptions = {
2019
onSyncError?: (error: Error) => void;
2120
snapshotDebounceMs?: number;
21+
debug?: boolean;
2222
};
2323

2424
export function useTiptapSync(
2525
syncApi: SyncApi,
2626
id: string,
2727
opts?: UseSyncOptions
2828
) {
29+
const log: typeof console.log = opts?.debug ? console.debug : () => {};
2930
const convex = useConvex();
3031
const initial = useInitialState(syncApi, id);
3132
const extension = useMemo(() => {
@@ -105,6 +106,7 @@ export function syncExtension(
105106
initialState: InitialState,
106107
opts?: UseSyncOptions
107108
) {
109+
const log: typeof console.log = opts?.debug ? console.debug : () => {};
108110
let snapshotTimer: NodeJS.Timeout | undefined;
109111
const trySubmitSnapshot = (version: number, content: string) => {
110112
if (snapshotTimer) {
@@ -148,7 +150,15 @@ export function syncExtension(
148150

149151
try {
150152
if (
151-
await doSync(editor, convex, syncApi, id, serverVersion, initialState)
153+
await doSync(
154+
editor,
155+
convex,
156+
syncApi,
157+
id,
158+
serverVersion,
159+
initialState,
160+
opts?.debug
161+
)
152162
) {
153163
const version = collab.getVersion(editor.state);
154164
const content = JSON.stringify(editor.state.doc.toJSON());
@@ -219,8 +229,10 @@ async function doSync(
219229
syncApi: SyncApi,
220230
id: string,
221231
serverVersion: number | null,
222-
initialState: InitialState
232+
initialState: InitialState,
233+
debug?: boolean
223234
) {
235+
const log: typeof console.log = debug ? console.debug : () => {};
224236
if (serverVersion === null) {
225237
if (initialState.initialVersion <= 1) {
226238
// This is a new document, so we can create it on the server.

0 commit comments

Comments
 (0)