Skip to content

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

fix race conditions in note closing/opening

ammarahm-ed3ad6eab45026
diff --git a/apps/mobile/src/views/Editor/EditorHeader.js b/apps/mobile/src/views/Editor/EditorHeader.js
index 4fc140f9b..da773b9fe 100644
--- a/apps/mobile/src/views/Editor/EditorHeader.js
+++ b/apps/mobile/src/views/Editor/EditorHeader.js
@@ -40,9 +40,11 @@ import {EditorTitle} from './EditorTitle';
import {
clearEditor,
clearTimer,
+ EditorWebView,
getNote,
loadNote,
- setColors
+ setColors,
+ startClosingSession
} from './Functions';
import HistoryComponent from './HistoryComponent';
import {ProgressCircle} from './ProgressCircle';
@@ -72,7 +74,6 @@ const EditorHeader = () => {
eSendEvent(eCloseFullscreenEditor);
return;
}
-
if (deviceMode === 'mobile') {
editing.movedAway = true;
}
@@ -89,6 +90,7 @@ const EditorHeader = () => {
eSendEvent(eCloseFullscreenEditor);
}
} else {
+ startClosingSession();
if (deviceMode === 'mobile') {
tabBarRef.current?.goToPage(0);
}
diff --git a/apps/mobile/src/views/Editor/EditorOverlay.js b/apps/mobile/src/views/Editor/EditorOverlay.js
index 91a47e6be..8919c9313 100644
--- a/apps/mobile/src/views/Editor/EditorOverlay.js
+++ b/apps/mobile/src/views/Editor/EditorOverlay.js
@@ -8,13 +8,14 @@ import {useTracked} from '../../provider';
import {
eSendEvent,
eSubscribeEvent,
- eUnSubscribeEvent,
+ eUnSubscribeEvent
} from '../../services/EventManager';
import {SIZE} from '../../utils/SizeUtils';
import {sleep, timeConverter} from '../../utils/TimeUtils';
let timer = null;
let timerError = null;
+let timerClosing = null;
const EditorOverlay = () => {
const [state] = useTracked();
const {colors} = state;
@@ -25,6 +26,7 @@ const EditorOverlay = () => {
const load = async _loading => {
clearTimeout(timer);
clearTimeout(timerError);
+ clearTimeout(timerClosing);
if (_loading) {
opacity.setValue(1);
setLoading(_loading);
@@ -34,13 +36,15 @@ const EditorOverlay = () => {
} else {
clearTimeout(timer);
clearTimeout(timerError);
+ clearTimeout(timerClosing);
setError(false);
timing(opacity, {
toValue: 0,
duration: 150,
- easing: Easing.in(Easing.ease),
+ easing: Easing.in(Easing.ease)
}).start();
- setTimeout(() => {
+ timerClosing = setTimeout(() => {
+ console.log('clearing now');
opacity.setValue(1);
setLoading(null);
}, 150);
@@ -66,10 +70,10 @@ const EditorOverlay = () => {
opacity: opacity,
transform: [
{
- translateY:loading? 0 : 6000,
- },
+ translateY: loading ? 0 : 6000
+ }
],
- zIndex: 100,
+ zIndex: 100
}}>
<View
style={{
@@ -78,7 +82,7 @@ const EditorOverlay = () => {
alignItems: 'center',
backgroundColor: colors.bg,
borderRadius: 5,
- paddingVertical: 20,
+ paddingVertical: 20
}}>
<View
style={{
@@ -88,14 +92,14 @@ const EditorOverlay = () => {
marginBottom: 15,
borderRadius: 5,
overflow: 'hidden',
- backgroundColor: colors.nav,
+ backgroundColor: colors.nav
}}>
<Animated.View
style={{
height: 10,
borderRadius: 5,
width: 100,
- backgroundColor: colors.accent,
+ backgroundColor: colors.accent
}}
/>
</View>
@@ -122,12 +126,12 @@ const EditorOverlay = () => {
<View
style={{
position: 'absolute',
- bottom: 25,
+ bottom: 25
}}>
<Button
type="errorShade"
style={{
- marginTop: 10,
+ marginTop: 10
}}
onPress={() => {
setError(false);
@@ -142,7 +146,7 @@ const EditorOverlay = () => {
style={{
textAlign: 'center',
maxWidth: '100%',
- marginTop: 5,
+ marginTop: 5
}}>
If the editor fails to load even after reloading. Try restarting the
app.
diff --git a/apps/mobile/src/views/Editor/Functions.js b/apps/mobile/src/views/Editor/Functions.js
index 69664e7c9..c438267fe 100644
--- a/apps/mobile/src/views/Editor/Functions.js
+++ b/apps/mobile/src/views/Editor/Functions.js
@@ -27,6 +27,7 @@ import {normalize} from '../../utils/SizeUtils';
import {sleep, timeConverter} from '../../utils/TimeUtils';
import tiny from './tiny/tiny';
import {IMAGE_TOOLTIP_CONFIG} from './tiny/toolbar/config';
+import {parse} from 'node-html-parser';
export let EditorWebView = createRef();
export const editorTitleInput = createRef();
@@ -57,6 +58,11 @@ let cTimeout = null;
let disableSaving = false;
let isSaving = false;
let waitForContent = false;
+let prevNoteContent = null;
+
+export function startClosingSession() {
+ closingSession = true;
+}
export function setWebviewInit(init) {
webviewInit = init;
@@ -104,7 +110,7 @@ export async function clearTimer(clear) {
clearTimeout(timer);
timer = null;
if (waitForContent && noteEdited) {
- await waitForEvent('content_event', () => {
+ await waitForEvent('content_event', () => {
tiny.call(EditorWebView, request_content);
});
}
@@ -142,12 +148,15 @@ export const CHECK_STATUS = `(function() {
const request_content = `(function() {
if (window.ReactNativeWebView) {
- window.ReactNativeWebView.postMessage(
- JSON.stringify({
- type: 'tiny',
- value: editor.getHTML()
- })
- );
+ editor.getHTML().then(function(html) {
+ window.ReactNativeWebView.postMessage(
+ JSON.stringify({
+ type: 'tiny',
+ value:html,
+ caller:"request_content"
+ })
+ );
+ })
}
})();`;
@@ -166,6 +175,10 @@ export function post(type, value = null) {
type,
value
};
+ if (type === 'html') {
+ console.log('message type', type);
+ }
+
EditorWebView.current?.postMessage(JSON.stringify(message));
}
@@ -213,6 +226,8 @@ function clearNote() {
note = null;
title = '';
noteEdited = false;
+ console.log('closing note id', id);
+ prevNoteContent = content.data;
id = null;
content = {
data: '',
@@ -227,10 +242,11 @@ export const loadNote = async item => {
if (closingSession) {
eSendEvent('loadingNote', item);
+ console.log('waiting item');
await waitForEvent('session_ended');
console.log('session ended');
}
-
+ closingSession = true;
if (editing.isFocused) {
tiny.call(EditorWebView, tiny.blur);
}
@@ -239,6 +255,7 @@ export const loadNote = async item => {
if (getNote()) {
await clearEditor(true, true, true);
}
+ closingSession = false;
disableSaving = false;
lastEditTime = 0;
clearNote();
@@ -274,6 +291,7 @@ export const loadNote = async item => {
if (getNote()) {
await clearEditor(true, false, true);
}
+ closingSession = false;
disableSaving = false;
noteEdited = false;
await setNote(item);
@@ -309,6 +327,7 @@ const checkStatus = async noreset => {
webviewTimer = setTimeout(() => {
if (!webviewOK && !noreset) {
+ console.log('webview not ok', 'ERROR');
webviewInit = false;
EditorWebView = createRef();
eSendEvent('webviewreset');
@@ -329,16 +348,18 @@ export const _onMessage = async evt => {
}
switch (message.type) {
case 'history':
- console.log('history', message.value);
eSendEvent('historyEvent', message.value);
break;
- case 'noteedited':
- console.log('noteedited');
- break;
case 'tiny':
+ if (message.value === '<br>') return;
if (message.value !== content.data) {
+ if (prevNoteContent && message.value === prevNoteContent) {
+ prevNoteContent = null;
+ noteEdited = false;
+ return;
+ }
+
noteEdited = true;
- console.log(message.value, '\nold: ', content.data);
lastEditTime = Date.now();
content = {
type: message.type,
@@ -439,7 +460,7 @@ function onNoteChange() {
if (noteEdited) {
saveNote();
}
- }, 150);
+ }, 300);
}
export async function clearEditor(
@@ -612,7 +633,7 @@ export async function saveNote(preventUpdate) {
tiny.call(EditorWebView, tiny.updateSavingState('Saved'));
}
} catch (e) {
- console.log('note save error', e);
+ console.log('note save error', e, e.stack);
}
isSaving = false;
}
@@ -623,6 +644,9 @@ export async function onWebViewLoad(premium, colors) {
} else {
tiny.call(EditorWebView, tiny.removeMarkdown, true);
}
+ if (getNote()?.id) {
+ loadNoteInEditor();
+ }
setColors(colors);
}
@@ -670,6 +694,8 @@ export const presentResolveConflictDialog = _note => {
};
const loadNoteInEditor = async (keepHistory = true) => {
+ console.log('loading note', id, webviewInit);
+
if (!webviewInit) return;
saveCounter = 0;
if (note?.id) {
@@ -689,6 +715,7 @@ const loadNoteInEditor = async (keepHistory = true) => {
`
);
} else {
+ console.log('opening in editor');
post('html', content.data);
}
if (id) {
@@ -704,6 +731,9 @@ const loadNoteInEditor = async (keepHistory = true) => {
} else {
await restoreEditorState();
}
+ await sleep(500);
+ loadingNote = null;
+ disableSaving = false;
if (keepHistory) {
tiny.call(EditorWebView, tiny.clearHistory);
}