mobile: add editor lock safety

This commit is contained in:
Ammar Ahmed
2024-05-08 11:03:50 +05:00
committed by Ammar Ahmed
parent a965cd694b
commit 001cf8ee92
3 changed files with 134 additions and 116 deletions

View File

@@ -145,8 +145,9 @@ export const useEditor = (
useEffect(() => { useEffect(() => {
const event = eSubscribeEvent(eEditorTabFocused, (tabId) => { const event = eSubscribeEvent(eEditorTabFocused, (tabId) => {
console.log("Editot tab focus changed", lastTabFocused.current, tabId);
if (lastTabFocused.current !== tabId) lock.current = false;
lastTabFocused.current = tabId as number; lastTabFocused.current = tabId as number;
console.log(tabId);
}); });
return () => { return () => {
event?.unsubscribe(); event?.unsubscribe();
@@ -573,6 +574,8 @@ export const useEditor = (
data: Note | ContentItem | TrashItem | DeletedItem, data: Note | ContentItem | TrashItem | DeletedItem,
isLocal?: boolean isLocal?: boolean
) => { ) => {
try {
await (async () => {
if (SettingsService.get().disableRealtimeSync && !isLocal) return; if (SettingsService.get().disableRealtimeSync && !isLocal) return;
if (!data) return; if (!data) return;
@@ -596,7 +599,8 @@ export const useEditor = (
const tab = useTabStore.getState().getTab(tabId); const tab = useTabStore.getState().getTab(tabId);
const note = data.type === "note" ? data : await db.notes?.note(noteId); const note =
data.type === "note" ? data : await db.notes?.note(noteId);
lock.current = true; lock.current = true;
@@ -654,12 +658,14 @@ export const useEditor = (
if (data.type === "tiptap" && note && !isLocal) { if (data.type === "tiptap" && note && !isLocal) {
if (lastContentChangeTime.current[noteId] >= data.dateEdited) { if (lastContentChangeTime.current[noteId] >= data.dateEdited) {
lock.current = false;
return; return;
} }
if (locked && isEncryptedContent(data)) { if (locked && isEncryptedContent(data)) {
const decryptedContent = await db.vault?.decryptContent(data); const decryptedContent = await db.vault?.decryptContent(
data,
noteId
);
if (!decryptedContent) { if (!decryptedContent) {
useTabStore.getState().updateTab(tabId, { useTabStore.getState().updateTab(tabId, {
locked: true, locked: true,
@@ -680,17 +686,22 @@ export const useEditor = (
} else { } else {
const _nextContent = data.data; const _nextContent = data.data;
if (_nextContent === currentContents.current?.data) { if (_nextContent === currentContents.current?.data) {
lock.current = false;
return; return;
} }
lastContentChangeTime.current[note.id] = note.dateEdited; lastContentChangeTime.current[note.id] = note.dateEdited;
await postMessage(EditorEvents.updatehtml, _nextContent, tabId); await postMessage(EditorEvents.updatehtml, _nextContent, tabId);
if (!isEncryptedContent(data)) { if (!isEncryptedContent(data)) {
currentContents.current[note.id] = data as UnencryptedContentItem; currentContents.current[note.id] =
data as UnencryptedContentItem;
} }
} }
} }
})();
} catch (e) {
DatabaseLogger.error(e as Error, "Error when applying sync changes");
} finally {
lock.current = false; lock.current = false;
}
}, },
[postMessage, commands] [postMessage, commands]
); );
@@ -728,11 +739,19 @@ export const useEditor = (
(currentLoadingNoteId.current && (currentLoadingNoteId.current &&
currentLoadingNoteId.current === noteId) currentLoadingNoteId.current === noteId)
) { ) {
DatabaseLogger.log(`Skipped saving conent: DatabaseLogger.log(`Skipped saving content:
lock.current: ${lock.current} lock.current: ${lock.current}
currentLoadingNoteId.current: ${currentLoadingNoteId.current} currentLoadingNoteId.current: ${currentLoadingNoteId.current}
`); `);
if (lock.current) {
setTimeout(() => {
if (lock.current) {
DatabaseLogger.warn("Editor force removed lock after 5 seconds");
lock.current = false;
}
}, 5000);
}
return; return;
} }

View File

@@ -958,4 +958,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: 2b8b28a341b202bf3ca5f231b75bb05893486ed8 PODFILE CHECKSUM: 2b8b28a341b202bf3ca5f231b75bb05893486ed8
COCOAPODS: 1.14.2 COCOAPODS: 1.12.1

View File

@@ -219,7 +219,6 @@ export function useEditorController({
tabRef.current.noteId && tabRef.current.noteId &&
tabRef.current.noteId === useTabStore.getState().getCurrentNoteId() tabRef.current.noteId === useTabStore.getState().getCurrentNoteId()
) { ) {
logger("info", tabRef.current.noteId, value);
useTabStore.getState().setNoteState(tabRef.current.noteId, { useTabStore.getState().setNoteState(tabRef.current.noteId, {
top: value top: value
}); });