Skip to content

Commit

Permalink
fix: abstracted away from exposing the provider directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Palanikannan1437 committed Nov 26, 2024
1 parent 2d648c7 commit 5d34895
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/editor/src/core/hooks/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
IMentionSuggestion,
TEditorCommands,
TFileHandler,
TDocumentEventEmitter,
} from "@/types";

export interface CustomEditorProps {
Expand Down Expand Up @@ -296,7 +297,7 @@ export const useEditor = (props: CustomEditorProps) => {
Y.applyUpdate(document, value);
},
emitRealTimeUpdate: (message: TDocumentEventsServer) => provider?.sendStateless(message),
listenToRealTimeUpdate: () => provider,
listenToRealTimeUpdate: () => provider && { on: provider.on.bind(provider), off: provider.off.bind(provider) },
}),
[editorRef, savedSelection]
);
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/core/hooks/use-read-only-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const useReadOnlyEditor = (props: CustomReadOnlyEditorProps) => {
};
},
emitRealTimeUpdate: (message: TDocumentEventsServer) => provider?.sendStateless(message),
listenToRealTimeUpdate: () => provider,
listenToRealTimeUpdate: () => provider && { on: provider.on.bind(provider), off: provider.off.bind(provider) },
getHeadings: () => editorRef?.current?.storage.headingList.headings,
}));

Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/core/types/document-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ import { DocumentEventKey, DocumentRealtimeEvents } from "@/helpers/document-eve

export type TDocumentEventsClient = (typeof DocumentRealtimeEvents)[DocumentEventKey]["client"];
export type TDocumentEventsServer = (typeof DocumentRealtimeEvents)[DocumentEventKey]["server"];

export type TDocumentEventEmitter = {
on: (event: string, callback: (message: { payload: TDocumentEventsClient }) => void) => void;
off: (event: string, callback: (message: { payload: TDocumentEventsClient }) => void) => void;
};
6 changes: 3 additions & 3 deletions packages/editor/src/core/types/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
TServerHandler,
} from "@/types";
import { TTextAlign } from "@/extensions";
import { HocuspocusProvider, HocuspocusProviderWebsocket } from "@hocuspocus/provider";
import { TDocumentEventsServer } from "src/lib";
import { HocuspocusProviderWebsocket } from "@hocuspocus/provider";
import { TDocumentEventsServer, TDocumentEventEmitter } from "src/lib";

export type TEditorCommands =
| "text"
Expand Down Expand Up @@ -86,7 +86,7 @@ export type EditorReadOnlyRefApi = {
onHeadingChange: (callback: (headings: IMarking[]) => void) => () => void;
getHeadings: () => IMarking[];
emitRealTimeUpdate: (action: TDocumentEventsServer) => void;
listenToRealTimeUpdate: () => HocuspocusProvider;
listenToRealTimeUpdate: () => TDocumentEventEmitter | undefined;
};

export interface EditorRefApi extends EditorReadOnlyRefApi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ExportPageModal } from "@/components/pages";
// helpers
import { copyTextToClipboard, copyUrlToClipboard } from "@/helpers/string.helper";
// hooks
import { useCollaborativePageActions } from "@/hooks/use-live-server-realtime";
import { useCollaborativePageActions } from "@/hooks/use-collaborative-page-actions";
import { usePageFilters } from "@/hooks/use-page-filters";
import { useQueryParams } from "@/hooks/use-query-params";
// store
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ export const useCollaborativePageActions = (editorRef: EditorRefApi | EditorRead
}, [currentActionBeingProcessed, editorRef]);

useEffect(() => {
const provider = editorRef?.listenToRealTimeUpdate();
const realTimeStatelessMessageListener = editorRef?.listenToRealTimeUpdate();
// __AUTO_GENERATED_PRINT_VAR_START__
console.log(
"useCollaborativePageActions#(anon) realTimeStatelessMessageListener:",
realTimeStatelessMessageListener
); // __AUTO_GENERATED_PRINT_VAR_END__

const handleStatelessMessage = (message: { payload: TDocumentEventsClient }) => {
if (currentActionBeingProcessed === message.payload) {
Expand All @@ -86,10 +91,10 @@ export const useCollaborativePageActions = (editorRef: EditorRefApi | EditorRead
}
};

provider?.on("stateless", handleStatelessMessage);
realTimeStatelessMessageListener?.on("stateless", handleStatelessMessage);

return () => {
provider?.off("stateless", handleStatelessMessage);
realTimeStatelessMessageListener?.off("stateless", handleStatelessMessage);
};
}, [editorRef, currentActionBeingProcessed, executeCollaborativeAction, actionHandlerMap]);

Expand Down

0 comments on commit 5d34895

Please sign in to comment.