Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: improve real-time sync reliability

Abdullah Atta29b9c897c480
diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx
index e2a829187..10e82d70b 100644
--- a/apps/web/src/components/editor/index.tsx
+++ b/apps/web/src/components/editor/index.tsx
@@ -73,6 +73,7 @@ export default function EditorManager({
// stored in refs. Update this value to trigger an
// update.
const [timestamp, setTimestamp] = useState<number>(0);
+ const lastSavedTime = useRef<number>(0);
const previewSession = useRef<PreviewSession>();
const [dropRef, overlayRef] = useDragOverlay();
@@ -90,8 +91,13 @@ export default function EditorManager({
useEffect(() => {
const event = db.eventManager.subscribe(
EVENTS.syncItemMerged,
- async (item?: Record<string, string>) => {
- if (!item) return;
+ async (item?: Record<string, string | number>) => {
+ if (
+ !item ||
+ lastSavedTime.current >= item.dateEdited ||
+ isPreviewSession
+ )
+ return;
const { id, contentId, locked } = editorstore.get().session;
const isContent = item.type === "tiptap" && item.id === contentId;
@@ -104,7 +110,7 @@ export default function EditorManager({
else EV.publish(EVENTS.vaultLocked);
}
- editorInstance.current?.updateContent(item.data);
+ editorInstance.current?.updateContent(item.data as string);
db.eventManager.subscribe(
EVENTS.syncCompleted,
@@ -123,11 +129,13 @@ export default function EditorManager({
return () => {
event.unsubscribe();
};
- }, [editorInstance]);
+ }, [editorInstance, isPreviewSession]);
const openSession = useCallback(async (noteId: string | number) => {
await editorstore.get().openSession(noteId);
previewSession.current = undefined;
+
+ lastSavedTime.current = Date.now();
setTimestamp(Date.now());
}, []);
@@ -149,6 +157,7 @@ export default function EditorManager({
(async function () {
await editorstore.newSession(nonce);
+ lastSavedTime.current = 0;
setTimestamp(Date.now());
})();
}, [isNewSession, nonce]);
@@ -171,7 +180,6 @@ export default function EditorManager({
flexDirection: "column"
}}
>
- {/* <UpdatesPendingNotice /> */}
{previewSession.current && (
<PreviewModeNotice
{...previewSession.current}
@@ -184,6 +192,7 @@ export default function EditorManager({
previewSession.current?.content?.data ||
editorstore.get().session?.content?.data
}
+ onContentChange={() => (lastSavedTime.current = Date.now())}
options={{
readonly: isReadonly || isPreviewSession,
onRequestFocus: () => toggleProperties(false),
@@ -217,9 +226,10 @@ type EditorProps = {
content: string;
nonce?: number;
options?: EditorOptions;
+ onContentChange?: () => void;
};
export function Editor(props: EditorProps) {
- const { content, nonce, options } = props;
+ const { content, nonce, options, onContentChange } = props;
const { readonly, headless, onLoadMedia, isMobile } = options || {
headless: false,
readonly: false,
@@ -280,6 +290,7 @@ export function Editor(props: EditorProps) {
onLoad={() => {
if (onLoadMedia) onLoadMedia();
}}
+ onContentChange={onContentChange}
onChange={onEditorChange}
onDownloadAttachment={(attachment) =>
downloadAttachment(attachment.hash)
diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx
index cf1578d3d..d52f9e95e 100644
--- a/apps/web/src/components/editor/tiptap.tsx
+++ b/apps/web/src/components/editor/tiptap.tsx
@@ -56,6 +56,7 @@ type TipTapProps = {
editorContainer: HTMLElement;
onLoad?: () => void;
onChange?: (id: string, sessionId: string, content: string) => void;
+ onContentChange?: () => void;
onInsertAttachment?: (type: AttachmentType) => void;
onDownloadAttachment?: (attachment: Attachment) => void;
onAttachFile?: (file: File) => void;
@@ -101,6 +102,7 @@ function TipTap(props: TipTapProps) {
onInsertAttachment,
onDownloadAttachment,
onAttachFile,
+ onContentChange,
onFocus = () => {},
content,
toolbarContainerId,
@@ -166,6 +168,8 @@ function TipTap(props: TipTapProps) {
if (onLoad) onLoad();
},
onUpdate: ({ editor, transaction }) => {
+ onContentChange?.();
+
const preventSave = transaction?.getMeta("preventSave") as boolean;
const { id, sessionId } = editorstore.get().session;
const content = editor.state.doc.content;