1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Moved sync scheduling to redux middleware

This commit is contained in:
Laurent Cozic 2017-07-25 18:41:53 +00:00
parent 621baf084b
commit e2af824106
11 changed files with 46 additions and 46 deletions

View File

@ -31,7 +31,7 @@ class ActionButtonComponent extends React.Component {
newTodo_press() { newTodo_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Note', routeName: 'Note',
noteId: null, noteId: null,
folderId: this.props.parentFolderId, folderId: this.props.parentFolderId,
@ -41,7 +41,7 @@ class ActionButtonComponent extends React.Component {
newNote_press() { newNote_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Note', routeName: 'Note',
noteId: null, noteId: null,
folderId: this.props.parentFolderId, folderId: this.props.parentFolderId,
@ -51,7 +51,7 @@ class ActionButtonComponent extends React.Component {
newFolder_press() { newFolder_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Folder', routeName: 'Folder',
folderId: null, folderId: null,
}); });

View File

@ -33,7 +33,7 @@ class NoteItemComponent extends Component {
noteItem_press(noteId) { noteItem_press(noteId) {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Note', routeName: 'Note',
noteId: noteId, noteId: noteId,
}); });

View File

@ -69,12 +69,11 @@ class NoteListComponent extends Component {
async todoCheckbox_change(itemId, checked) { async todoCheckbox_change(itemId, checked) {
let note = await Note.load(itemId); let note = await Note.load(itemId);
await Note.save({ id: note.id, todo_completed: checked ? time.unixMs() : 0 }); await Note.save({ id: note.id, todo_completed: checked ? time.unixMs() : 0 });
reg.scheduleSync();
} }
listView_itemPress(noteId) { listView_itemPress(noteId) {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Note', routeName: 'Note',
noteId: noteId, noteId: noteId,
}); });

View File

@ -114,12 +114,12 @@ class ScreenHeaderComponent extends Component {
} }
backButton_press() { backButton_press() {
this.props.dispatch({ type: 'Navigation/BACK' }); this.props.dispatch({ type: 'NAV_BACK' });
} }
searchButton_press() { searchButton_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Search', routeName: 'Search',
}); });
} }
@ -132,21 +132,21 @@ class ScreenHeaderComponent extends Component {
log_press() { log_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Log', routeName: 'Log',
}); });
} }
status_press() { status_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Status', routeName: 'Status',
}); });
} }
config_press() { config_press() {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Config', routeName: 'Config',
}); });
} }

View File

@ -66,8 +66,6 @@ class FolderScreenComponent extends BaseScreenComponent {
try { try {
folder = await Folder.save(folder, { userSideValidation: true }); folder = await Folder.save(folder, { userSideValidation: true });
reg.scheduleSync();
} catch (error) { } catch (error) {
dialogs.error(this, _('The notebook could not be saved: %s', error.message)); dialogs.error(this, _('The notebook could not be saved: %s', error.message));
return; return;
@ -79,7 +77,7 @@ class FolderScreenComponent extends BaseScreenComponent {
}); });
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Notes', routeName: 'Notes',
folderId: folder.id, folderId: folder.id,
}); });

View File

@ -181,8 +181,6 @@ class NoteScreenComponent extends BaseScreenComponent {
}); });
if (isNew) Note.updateGeolocation(note.id); if (isNew) Note.updateGeolocation(note.id);
this.refreshNoteMetadata(); this.refreshNoteMetadata();
reg.scheduleSync();
} }
async saveOneProperty(name, value) { async saveOneProperty(name, value) {
@ -204,8 +202,6 @@ class NoteScreenComponent extends BaseScreenComponent {
lastSavedNote: Object.assign({}, note), lastSavedNote: Object.assign({}, note),
note: note, note: note,
}); });
reg.scheduleSync();
} else { } else {
note[name] = value; note[name] = value;
this.setState({ note: note }); this.setState({ note: note });
@ -224,12 +220,10 @@ class NoteScreenComponent extends BaseScreenComponent {
await Note.delete(note.id); await Note.delete(note.id);
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Notes', routeName: 'Notes',
folderId: folderId, folderId: folderId,
}); });
reg.scheduleSync();
} }
attachFile_onPress() { attachFile_onPress() {
@ -274,7 +268,6 @@ class NoteScreenComponent extends BaseScreenComponent {
async todoCheckbox_change(checked) { async todoCheckbox_change(checked) {
await this.saveOneProperty('todo_completed', checked ? time.unixMs() : 0); await this.saveOneProperty('todo_completed', checked ? time.unixMs() : 0);
reg.scheduleSync();
} }
render() { render() {
@ -485,8 +478,6 @@ class NoteScreenComponent extends BaseScreenComponent {
note: note, note: note,
folder: folder, folder: folder,
}); });
reg.scheduleSync();
} }
}} }}
menuOptions={this.menuOptions()} menuOptions={this.menuOptions()}

View File

@ -64,11 +64,9 @@ class NotesScreenComponent extends BaseScreenComponent {
Folder.delete(folderId).then(() => { Folder.delete(folderId).then(() => {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Welcome', routeName: 'Welcome',
}); });
reg.scheduleSync();
}).catch((error) => { }).catch((error) => {
alert(error.message); alert(error.message);
}); });
@ -77,7 +75,7 @@ class NotesScreenComponent extends BaseScreenComponent {
editFolder_onPress(folderId) { editFolder_onPress(folderId) {
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Folder', routeName: 'Folder',
folderId: folderId, folderId: folderId,
}); });

View File

@ -49,7 +49,7 @@ class OneDriveLoginScreenComponent extends BaseScreenComponent {
try { try {
await reg.oneDriveApi().execTokenRequest(this.authCode_, this.redirectUrl(), true); await reg.oneDriveApi().execTokenRequest(this.authCode_, this.redirectUrl(), true);
this.props.dispatch({ type: 'Navigation/BACK' }); this.props.dispatch({ type: 'NAV_BACK' });
reg.scheduleSync(0); reg.scheduleSync(0);
} catch (error) { } catch (error) {
alert(error.message); alert(error.message);

View File

@ -67,7 +67,7 @@ class SideMenuContentComponent extends Component {
this.props.dispatch({ type: 'SIDE_MENU_CLOSE' }); this.props.dispatch({ type: 'SIDE_MENU_CLOSE' });
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Notes', routeName: 'Notes',
folderId: folder.id, folderId: folder.id,
}); });
@ -78,7 +78,7 @@ class SideMenuContentComponent extends Component {
this.props.dispatch({ type: 'SIDE_MENU_CLOSE' }); this.props.dispatch({ type: 'SIDE_MENU_CLOSE' });
this.props.dispatch({ this.props.dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'OneDriveLogin', routeName: 'OneDriveLogin',
}); });
return; return;

View File

@ -32,6 +32,9 @@ class PoorManIntervals {
} }
static update() { static update() {
// Don't update more than once a second
if (PoorManIntervals.lastUpdateTime_ + 1000 > time.unixMs()) return;
for (let i = 0; i < PoorManIntervals.intervals_.length; i++) { for (let i = 0; i < PoorManIntervals.intervals_.length; i++) {
let interval = PoorManIntervals.intervals_[i]; let interval = PoorManIntervals.intervals_[i];
const now = time.unixMs(); const now = time.unixMs();
@ -40,10 +43,13 @@ class PoorManIntervals {
interval.callback(); interval.callback();
} }
} }
PoorManIntervals.lastUpdateTime_ = time.unixMs();
} }
} }
PoorManIntervals.lastUpdateTime_ = 0;
PoorManIntervals.intervalId_ = 0; PoorManIntervals.intervalId_ = 0;
PoorManIntervals.intervals_ = []; PoorManIntervals.intervals_ = [];

View File

@ -1,7 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { BackHandler, Keyboard } from 'react-native'; import { BackHandler, Keyboard } from 'react-native';
import { connect, Provider } from 'react-redux' import { connect, Provider } from 'react-redux'
import { createStore } from 'redux'; import { createStore, applyMiddleware } from 'redux';
import { shimInit } from 'lib/shim-init-react.js'; import { shimInit } from 'lib/shim-init-react.js';
import { Log } from 'lib/log.js' import { Log } from 'lib/log.js'
import { AppNav } from 'lib/components/app-nav.js' import { AppNav } from 'lib/components/app-nav.js'
@ -56,7 +56,7 @@ let defaultState = {
}; };
const initialRoute = { const initialRoute = {
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Welcome', routeName: 'Welcome',
params: {} params: {}
}; };
@ -84,8 +84,6 @@ function reducerActionsAreSame(a1, a2) {
} }
const reducer = (state = defaultState, action) => { const reducer = (state = defaultState, action) => {
reg.logger().info('Reducer action', action.type);
let newState = state; let newState = state;
let historyGoingBack = false; let historyGoingBack = false;
@ -93,7 +91,7 @@ const reducer = (state = defaultState, action) => {
switch (action.type) { switch (action.type) {
case 'Navigation/BACK': case 'NAV_BACK':
if (!navHistory.length) break; if (!navHistory.length) break;
@ -109,7 +107,7 @@ const reducer = (state = defaultState, action) => {
// Fall throught // Fall throught
case 'Navigation/NAVIGATE': case 'NAV_GO':
const currentRoute = state.route; const currentRoute = state.route;
const currentRouteName = currentRoute ? currentRoute.routeName : ''; const currentRouteName = currentRoute ? currentRoute.routeName : '';
@ -164,7 +162,6 @@ const reducer = (state = defaultState, action) => {
Setting.setValue('activeFolderId', newState.selectedFolderId); Setting.setValue('activeFolderId', newState.selectedFolderId);
} }
Keyboard.dismiss(); // TODO: should probably be in some middleware
break; break;
// Replace all the notes with the provided array // Replace all the notes with the provided array
@ -311,14 +308,25 @@ const reducer = (state = defaultState, action) => {
throw error; throw error;
} }
// Check the registered intervals here since we know this function
// will be called regularly.
PoorManIntervals.update();
return newState; return newState;
} }
let store = createStore(reducer); const generalMiddleware = store => next => action => {
reg.logger().info('Reducer action', action.type);
PoorManIntervals.update(); // This function needs to be called regularly so put it here
const result = next(action);
if (action.type == 'NAV_GO') Keyboard.dismiss();
if (['NOTES_UPDATE_ONE', 'NOTES_DELETE', 'FOLDERS_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
reg.scheduleSync();
}
return result;
}
let store = createStore(reducer, applyMiddleware(generalMiddleware));
let initializationState_ = 'waiting'; let initializationState_ = 'waiting';
@ -410,12 +418,12 @@ async function initialize(dispatch, backButtonHandler) {
if (!folder) { if (!folder) {
dispatch({ dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Welcome', routeName: 'Welcome',
}); });
} else { } else {
dispatch({ dispatch({
type: 'Navigation/NAVIGATE', type: 'NAV_GO',
routeName: 'Notes', routeName: 'Notes',
folderId: folder.id, folderId: folder.id,
}); });
@ -462,7 +470,7 @@ class AppComponent extends React.Component {
} }
if (this.props.historyCanGoBack) { if (this.props.historyCanGoBack) {
this.props.dispatch({ type: 'Navigation/BACK' }); this.props.dispatch({ type: 'NAV_BACK' });
return true; return true;
} }