Skip to content

Commit

Permalink
Mobile: Improve sync by reducing how often note list is sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Jan 3, 2024
1 parent 164e53e commit f95ee68
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/app-mobile/root.tsx
Expand Up @@ -40,7 +40,7 @@ import { Provider as PaperProvider, MD3DarkTheme, MD3LightTheme } from 'react-na
const { BackButtonService } = require('./services/back-button.js');
import NavService from '@joplin/lib/services/NavService';
import { createStore, applyMiddleware } from 'redux';
const reduxSharedMiddleware = require('@joplin/lib/components/shared/reduxSharedMiddleware');
import reduxSharedMiddleware from '@joplin/lib/components/shared/reduxSharedMiddleware';
const { shimInit } = require('./utils/shim-init-react.js');
const { AppNav } = require('./components/app-nav.js');
import Note from '@joplin/lib/models/Note';
Expand Down Expand Up @@ -149,7 +149,7 @@ const generalMiddleware = (store: any) => (next: any) => async (action: any) =>
const result = next(action);
const newState = store.getState();

await reduxSharedMiddleware(store, next, action);
await reduxSharedMiddleware(store, next, action, storeDispatch as any);

if (action.type === 'NAV_GO') Keyboard.dismiss();

Expand Down
4 changes: 2 additions & 2 deletions packages/lib/BaseApplication.ts
Expand Up @@ -24,7 +24,7 @@ import { splitCommandString } from '@joplin/utils';
import { reg } from './registry';
import time from './time';
import BaseSyncTarget from './BaseSyncTarget';
const reduxSharedMiddleware = require('./components/shared/reduxSharedMiddleware');
import reduxSharedMiddleware from './components/shared/reduxSharedMiddleware';
const os = require('os');
import dns = require('dns');
import fs = require('fs-extra');
Expand Down Expand Up @@ -423,7 +423,7 @@ export default class BaseApplication {
let refreshNotesUseSelectedNoteId = false;
let refreshNotesHash = '';

await reduxSharedMiddleware(store, next, action);
await reduxSharedMiddleware(store, next, action, ((action: any) => { this.dispatch(action); }) as any);
const newState = store.getState() as State;

if (this.hasGui() && ['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
Expand Down
32 changes: 27 additions & 5 deletions packages/lib/components/shared/reduxSharedMiddleware.ts
Expand Up @@ -7,13 +7,19 @@ import ResourceFetcher from '../../services/ResourceFetcher';
import DecryptionWorker from '../../services/DecryptionWorker';
import eventManager from '../../eventManager';
import BaseItem from '../../models/BaseItem';
import shim from '../../shim';
import { Dispatch } from 'redux';
import { State } from '../../reducer';

const reduxSharedMiddleware = async function(store: any, _next: any, action: any) {
const newState = store.getState();
let sortNoteListTimeout: any = null;

export default async (store: any, _next: any, action: any, dispatch: Dispatch) => {
const newState: State = store.getState();

eventManager.appStateEmit(newState);

let refreshTags = false;
let sortNoteList = false;

if (action.type === 'FOLDER_SET_COLLAPSED' || action.type === 'FOLDER_TOGGLE') {
Setting.setValue('collapsedFolderIds', newState.collapsedFolderIds);
Expand Down Expand Up @@ -57,6 +63,9 @@ const reduxSharedMiddleware = async function(store: any, _next: any, action: any
refreshTags = true;
}

if (action.type === 'NOTE_UPDATE_ONE') {
sortNoteList = true;
}

if (action.type === 'NOTE_SELECT' || action.type === 'NAV_BACK') {
const noteIds = newState.provisionalNoteIds.slice();
Expand Down Expand Up @@ -98,6 +107,22 @@ const reduxSharedMiddleware = async function(store: any, _next: any, action: any
});
}

if (sortNoteList) {
if (sortNoteListTimeout) shim.clearTimeout(sortNoteListTimeout);
sortNoteListTimeout = null;

// We sort the note lists with two seconds debounce because doing can be
// very slow and would have to be done every time a note is added.
if (Date.now() - newState.noteListLastSortTime > 10000) {
dispatch({ type: 'NOTE_SORT' });
} else {
sortNoteListTimeout = shim.setTimeout(() => {
sortNoteListTimeout = null;
dispatch({ type: 'NOTE_SORT' });
}, 2000);
}
}

if (action.type.startsWith('SHARE_')) {
const serialized = JSON.stringify(newState.shareService);
Setting.setValue('sync.shareCache', serialized);
Expand All @@ -116,6 +141,3 @@ const reduxSharedMiddleware = async function(store: any, _next: any, action: any
}
}
};

module.exports = reduxSharedMiddleware;

15 changes: 12 additions & 3 deletions packages/lib/reducer.ts
Expand Up @@ -102,6 +102,7 @@ export interface State {
needApiAuth: boolean;
profileConfig: ProfileConfig;
noteListRendererIds: string[];
noteListLastSortTime: number;

// Extra reducer keys go here:
pluginService: PluginServiceState;
Expand Down Expand Up @@ -176,6 +177,7 @@ export const defaultState: State = {
needApiAuth: false,
profileConfig: null,
noteListRendererIds: getListRendererIds(),
noteListLastSortTime: 0,

pluginService: pluginServiceDefaultState,
shareService: shareServiceDefaultState,
Expand Down Expand Up @@ -850,6 +852,7 @@ const reducer = produce((draft: Draft<State> = defaultState, action: any) => {
case 'NOTE_UPDATE_ALL':
draft.notes = action.notes;
draft.notesSource = action.notesSource;
draft.noteListLastSortTime = Date.now(); // Notes are already sorted when they are set this way.
updateSelectedNotesFromExistingNotes(draft);
break;

Expand All @@ -869,7 +872,7 @@ const reducer = produce((draft: Draft<State> = defaultState, action: any) => {

let movedNotePreviousIndex = 0;
let noteFolderHasChanged = false;
let newNotes = draft.notes.slice();
const newNotes = draft.notes.slice();
let found = false;
for (let i = 0; i < newNotes.length; i++) {
const n = newNotes[i];
Expand Down Expand Up @@ -909,8 +912,6 @@ const reducer = produce((draft: Draft<State> = defaultState, action: any) => {
}
}

// newNotes = Note.sortNotes(newNotes, draft.notesOrder, draft.settings.uncompletedTodosOnTop);
newNotes = Note.sortNotes(newNotes, stateUtils.notesOrder(draft.settings), draft.settings.uncompletedTodosOnTop);
draft.notes = newNotes;

if (noteFolderHasChanged) {
Expand Down Expand Up @@ -953,6 +954,14 @@ const reducer = produce((draft: Draft<State> = defaultState, action: any) => {
}
break;

case 'NOTE_SORT':

{
draft.notes = Note.sortNotes(draft.notes, stateUtils.notesOrder(draft.settings), draft.settings.uncompletedTodosOnTop);
draft.noteListLastSortTime = Date.now();
}
break;

case 'NOTE_IS_INSERTING_NOTES':

if (draft.isInsertingNotes !== action.value) {
Expand Down

0 comments on commit f95ee68

Please sign in to comment.