Skip to content

Commit

Permalink
Rewrite app core to remove data races and fix data flow (#2148)
Browse files Browse the repository at this point in the history
co-authored by @belcherj and @codebykat with design work by @SylvesterWilmott 

## Description

We've been unable to resolve the worst sync bugs in the application and feature development has been artificially limited by the fact that we have to fight a number of data-flow issues tightly coupling different levels of the application's architecture together.

In this branch, we're ripping apart the entire app state and Simperium data flow to rebuild it in order to remove a number of those couplings and races. This commit changes most of the app and was rolled out in staging to test the changes.

We're also replacing `draft-js` with `Monaco` which is almost as big of a change conceptually as changing the state. Moving to `Monaco` allows us to remove a copy of the note data from the app and allows us to maintain a fully-synchronous update cycle, eliminating a race condition between the Simperium copy of the data, the app copy, and the contents in the text buffer.

## Major code changes

 - The interface between `node-simperium` and this app has been moved into Redux state and out of `indexedDB`. The `indexedDB` interface as asynchronous and led to sync issues under a variety of race conditions with the network data, remote updates, browser tab scheduling, whether the browser was focused or hidden behind other windows, and more. The new synchronous interface guarantees that updates occur when we expect them to occur and therefore will be updated by the time we continue processing changes and updates from the server.
 - `Monaco`'s synchronous plaintext interface allows us to extend the atomic "all updates occur together and instantaneously" paradigm to the text buffer. By sharing the note data in the text editor, in the app, and in `node-simperium`, we will guarantee that we won't accidentally apply new edits to old data.
 - In many cases we have been dispatching multiple actions in order to perform one real action. For example, edit a note and then clear out the search state. Because these presented intermediate states for the app, partial updates, they have been removed. Now we have created new Redux actions for these real actions and app state has been adjusted so that each kind of data has its own reducer and those reducers listen to all the actions which could affect them.  Now we will see a single dispatch that multiple reducers listen to instead of dispatching one action type for each reducer.
 - The Simperium connection has been moved into a new centralized middleware. All Simperium interactions take place in this middleware and are no longer woven into the app. This allows the app to update so that there's only ever one copy of a note's data and it's always up to date with the text editor. This resolves longstanding issues with the note list showing expired data. It also allows us to model the Simperium connection as a reactive system that responds to changes in the app and injects new events from the server in a way that's independent from local operation. This has dramatically simplified many different subsystems.
 - The persistence layer has now been created as a separate subsystem from before, when it was integrated with the Simperium code. It now operates as a kind of background worker that persists the Redux state into `indexedDB` and stores the entire contents atomically. Previously, integrated into the note bucket, the persistence layer would update each note, ghost, and bucket value separately leading to mismatches between a note and the base version it was built from, leading to sync issues.
 - So-called "prop drilling" has been replaced by connecting components in the React tree to state. This was done to simplify the interfaces around the app. It was previously difficult to understand what exactly was executing with the `onAction` props because there wasn't a clear way to walk up the component tree in an editor without resorting to text searching. Any `onEdit` type actions have been replaced with `dispatchProps` that directly dispatch the intended actions.

## Related or fixed issues

### Defects
Resolves #2171 (note display doesn't update height immediately upon change from menu bar)
Resolves #2074 (floating IME on Korean input)
Resolves #2014 (when opened note changes so it's not in the search, it still stays open)
Resolves #1953 (when renaming tag, update in search bar if tag opened)
Resolves #1942 (can't easily select start of note content)
Resolves #1887 (renaming a tag removes it from a note)

#### Cursor position and movement
Resolves #2085 (cursor jumps to new note)
Resolves #2035 (cursor hidden at bottom of note)
Resolves #1595 (restore cursor position when flipping between edit/preview mode)
Resolves #1477 (cursor jumping in Windows)

#### Synchronization
Resolves #1938 (not restoring tags when reverting to an earlier revision)
Resolves #1641 (updates to a tag in one session don't update in other sessions until they reload)
Resolves #1640 (infinite duplication of tag name when editing tag)
Resolves #1562 (content not syncing)
Resolves #1520 (only syncing one note at a time/per session)
Resolves #1291 (quirky unsynced changes info)
Resolves #502 (data loss when editing simultaneous with iOS)
Resolves #459 (show note sync indicator)

#### Force-sync
#1897
#800

#### Ghost-Writing
Resolves #2030
Resolves #1787

### Enhancements
Resolves #2162 (mostly - additional syncing metadata for notes)
Resolves #1836 (remove `app-state`)
Resolves #1816 (confusing revision history ordering)
#1537 (add indicator to show syncing status)
Resolves #2036 (logging-out in one sessions logs out all sessions)
Resolves #1410 (logout is buried in app settings and hard to find)

#### Private Mode
#1924 - functionality in Firefox private mode other than offline persistence

### Performance
Resolves #2172 (slow app)
Resolves #501 (slow loading large notes)

### Deprecations
Resolves #2117 (audit use of draftJS)
Resolves #1762 (decorator performance in draftJS)
Resolves #1026 (use of `token` in `localStorage` in `boot.js`)

### Possibly - check these
#1698 (problems with Korean input and lists at start of note)
#1619 (buggy Japanese IME conversion)
#1572 (RTL languages - checked tasks moving to the right)
#1511 (missing characters on Korean IME conversion)
#1456 (slow, possibly 4K-resolution-related)
  • Loading branch information
dmsnell authored Sep 21, 2020
1 parent 0ecb23e commit fc89212
Show file tree
Hide file tree
Showing 202 changed files with 13,749 additions and 10,136 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ release/
.idea
.DS_Store
dev-app-update.yml
lib/state/data/test_account.json
9 changes: 3 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type OwnProps = {
}

type StateProps = {
notes: T.NoteEntity[];
notes: T.Note[];
}

type DispatchProps = {
Expand Down Expand Up @@ -156,17 +156,14 @@ const loginAttempts: A.Reducer<number> = (state = 0, action) => {
}
}

const selectedNote: A.Reducer<T.NoteEntity[] | null> = (state = null, action) => {
const selectedNote: A.Reducer<T.EntityId | null> = (state = null, action) => {
switch (action.type) {
case 'CREATE_NOTE':
return makeNote();
return action.noteId;

case 'TRASH_NOTE':
return null;

case 'FILTER_NOTES':
return action.filteredNotes.has(state) ? state : null;

default:
return state;
}
Expand Down
8 changes: 4 additions & 4 deletions desktop/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const importNotes = require('./evernote-import');
const platform = require('./detect/platform');
const updater = require('./updater');
const { isDev } = require('./env');
const spellcheck = require('./spellchecker');
const contextMenu = require('./context-menu');

require('module').globalPaths.push(path.resolve(path.join(__dirname)));

Expand Down Expand Up @@ -77,7 +77,7 @@ module.exports = function main() {
mainWindow.loadUrl(url);
}

spellcheck(mainWindow);
contextMenu(mainWindow);

if (
'test' !== process.env.NODE_ENV &&
Expand Down Expand Up @@ -124,8 +124,8 @@ module.exports = function main() {
mainWindow.setMenuBarVisibility(!autoHideMenuBar);
});

ipcMain.on('wpLogin', function (event, url) {
shell.openExternal(url);
ipcMain.on('wpLogin', function (event, wpLoginUrl) {
shell.openExternal(wpLoginUrl);
});

ipcMain.on('importNotes', function (event, filePath) {
Expand Down
43 changes: 43 additions & 0 deletions desktop/context-menu/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

/**
* External dependencies
*/
const { Menu } = require('electron');

const { editorCommandSender } = require('../menus/utils');

module.exports = function (mainWindow) {
mainWindow.webContents.on('context-menu', (event, params) => {
const { editFlags } = params;
Menu.buildFromTemplate([
{
id: 'selectAll',
label: 'Select All',
click: editorCommandSender({ action: 'selectAll' }),
enabled: editFlags.canSelectAll,
},
{
id: 'cut',
label: 'Cut',
role: 'cut',
enabled: editFlags.canCut,
},
{
id: 'copy',
label: 'Copy',
role: 'copy',
enabled: editFlags.canCopy,
},
{
id: 'paste',
label: 'Paste',
role: 'paste',
enabled: editFlags.canPaste,
},
{
type: 'separator',
},
]).popup({});
});
};
24 changes: 20 additions & 4 deletions desktop/menus/edit-menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { appCommandSender } = require('./utils');
const { appCommandSender, editorCommandSender } = require('./utils');

const buildEditMenu = (settings, isAuthenticated) => {
settings = settings || {};
Expand All @@ -9,11 +9,13 @@ const buildEditMenu = (settings, isAuthenticated) => {
submenu: [
{
label: '&Undo',
role: 'undo',
click: editorCommandSender({ action: 'undo' }),
accelerator: 'CommandOrControl+Z',
},
{
label: '&Redo',
role: 'redo',
click: editorCommandSender({ action: 'redo' }),
accelerator: 'CommandOrControl+Shift+Z',
},
{
type: 'separator',
Expand All @@ -32,7 +34,8 @@ const buildEditMenu = (settings, isAuthenticated) => {
},
{
label: '&Select All',
role: 'selectall',
click: editorCommandSender({ action: 'selectAll' }),
accelerator: 'CommandOrControl+A',
},
{ type: 'separator' },
{
Expand All @@ -45,6 +48,19 @@ const buildEditMenu = (settings, isAuthenticated) => {
label: 'Search &Notes…',
visible: isAuthenticated,
click: appCommandSender({ action: 'focusSearchField' }),
accelerator: 'CommandOrControl+Shift+S',
},
{
label: '&Find in Note',
visible: isAuthenticated,
click: editorCommandSender({ action: 'find' }),
accelerator: 'CommandOrControl+F',
},
{
label: 'Find A&gain',
visible: isAuthenticated,
click: editorCommandSender({ action: 'findAgain' }),
accelerator: 'CommandOrControl+G',
},
{ type: 'separator' },
{
Expand Down
2 changes: 1 addition & 1 deletion desktop/menus/file-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const buildFileMenu = (isAuthenticated) => {
visible: isAuthenticated,
accelerator: 'CommandOrControl+Shift+E',
click: appCommandSender({
action: 'exportZipArchive',
action: 'exportNotes',
}),
},
{ type: 'separator' },
Expand Down
4 changes: 2 additions & 2 deletions desktop/menus/format-menu.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { appCommandSender } = require('./utils');
const { editorCommandSender } = require('./utils');

const buildFormatMenu = (isAuthenticated) => {
isAuthenticated = isAuthenticated || false;
const submenu = [
{
label: 'Insert &Checklist',
accelerator: 'CommandOrControl+Shift+C',
click: appCommandSender({ action: 'insertChecklist' }),
click: editorCommandSender({ action: 'insertChecklist' }),
},
];

Expand Down
5 changes: 4 additions & 1 deletion desktop/menus/help-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const buildHelpMenu = (mainWindow, isAuthenticated) => {
{
label: '&Keyboard Shortcuts',
visible: isAuthenticated,
click: appCommandSender({ action: 'showDialog', dialog: 'KEYBINDINGS' }),
click: appCommandSender({
action: 'showDialog',
dialog: 'KEYBINDINGS',
}),
},
{ type: 'separator' },
{
Expand Down
7 changes: 7 additions & 0 deletions desktop/menus/mac-app-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const buildMacAppMenu = (isAuthenticated) => {
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
...(isAuthenticated
? [
{ type: 'separator' },
menuItems.emptyTrash(isAuthenticated),
menuItems.signout(isAuthenticated),
]
: []),
{ type: 'separator' },
{ role: 'quit' },
];
Expand Down
20 changes: 20 additions & 0 deletions desktop/menus/menu-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const checkForUpdates = {
click: updater.pingAndShowProgress.bind(updater),
};

const emptyTrash = (isAuthenticated) => {
return {
label: '&Empty Trash',
visible: isAuthenticated,
click: appCommandSender({ action: 'emptyTrash' }),
};
};

const preferences = (isAuthenticated) => {
return {
label: 'P&references…',
Expand All @@ -28,8 +36,20 @@ const preferences = (isAuthenticated) => {
};
};

const signout = (isAuthenticated) => {
return {
label: '&Sign Out',
visible: isAuthenticated,
click: appCommandSender({
action: 'logout',
}),
};
};

module.exports = {
about,
checkForUpdates,
emptyTrash,
preferences,
signout,
};
11 changes: 10 additions & 1 deletion desktop/menus/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ const buildRadioGroup = ({ action, propName, settings }) => {
};

const appCommandSender = (arg) => {
return commandSender('appCommand', arg);
};

const editorCommandSender = (arg) => {
return commandSender('editorCommand', arg);
};

const commandSender = (commandName, arg) => {
return (item, focusedWindow) => {
if (focusedWindow) {
focusedWindow.webContents.send('appCommand', arg);
focusedWindow.webContents.send(commandName, arg);
}
};
};

module.exports = {
buildRadioGroup,
appCommandSender,
editorCommandSender,
};
8 changes: 6 additions & 2 deletions desktop/menus/view-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const buildViewMenu = (settings, isAuthenticated) => {
label: '&Reversed',
type: 'checkbox',
checked: settings.sortReversed,
click: appCommandSender({ action: 'toggleSortOrder' }),
click: appCommandSender({
action: 'toggleSortOrder',
}),
},
]),
},
Expand Down Expand Up @@ -96,7 +98,9 @@ const buildViewMenu = (settings, isAuthenticated) => {
label: '&Sort Alphabetically',
type: 'checkbox',
checked: settings.sortTagsAlpha,
click: appCommandSender({ action: 'toggleSortTagsAlpha' }),
click: appCommandSender({
action: 'toggleSortTagsAlpha',
}),
},
],
},
Expand Down
29 changes: 28 additions & 1 deletion desktop/preload.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { contextBridge, ipcRenderer } = require('electron');
const { contextBridge, ipcRenderer, remote } = require('electron');

const validChannels = [
'appCommand',
'clearCookies',
'editorCommand',
'importNotes',
'noteImportChannel',
'setAutoHideMenuBar',
Expand All @@ -11,6 +12,32 @@ const validChannels = [
];

contextBridge.exposeInMainWorld('electron', {
confirmLogout: (changes) => {
const response = remote.dialog.showMessageBoxSync({
type: 'warning',
buttons: [
'Export Unsynced Notes',
"Don't Logout Yet",
'Lose Changes and Logout',
],
title: 'Unsynced Notes Detected',
message:
'Logging out will delete any unsynced notes. ' +
'Do you want to continue or give it a little more time to finish trying to sync?\n\n' +
changes,
});

switch (response) {
case 0:
return 'export';

case 1:
return 'reconsider';

case 2:
return 'logout';
}
},
send: (channel, data) => {
// whitelist channels
if (validChannels.includes(channel)) {
Expand Down
63 changes: 0 additions & 63 deletions desktop/spellchecker/index.js

This file was deleted.

Loading

0 comments on commit fc89212

Please sign in to comment.