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

Refactored Redux middleware and trigger sync after saving note

This commit is contained in:
Laurent Cozic 2017-11-10 18:58:00 +00:00
parent 17309b0fac
commit 9b86eeacb3
3 changed files with 40 additions and 28 deletions

View File

@ -81,6 +81,14 @@ class Application extends BaseApplication {
return super.reducer(newState, action);
}
async generalMiddleware(store, next, action) {
if (['NOTE_UPDATE_ONE', 'NOTE_DELETE', 'FOLDER_UPDATE_ONE', 'FOLDER_DELETE'].indexOf(action.type) >= 0) {
if (!await reg.syncStarted()) reg.scheduleSync();
}
return super.generalMiddleware(store, next, action);
}
async start(argv) {
argv = await super.start(argv);

View File

@ -217,7 +217,7 @@ class NoteTextComponent extends React.Component {
webviewReady: true,
});
this.webview_.openDevTools();
// this.webview_.openDevTools();
}
webview_ref(element) {

View File

@ -196,37 +196,41 @@ class BaseApplication {
return false;
}
generalMiddleware() {
const middleware = store => next => async (action) => {
this.logger().debug('Reducer action', this.reducerActionToString(action));
const result = next(action);
const newState = store.getState();
if (action.type == 'FOLDER_SELECT' || action.type === 'FOLDER_DELETE') {
Setting.setValue('activeFolderId', newState.selectedFolderId);
this.currentFolder_ = newState.selectedFolderId ? await Folder.load(newState.selectedFolderId) : null;
await this.refreshNotes(Folder.modelType(), newState.selectedFolderId);
}
if (action.type == 'TAG_SELECT') {
await this.refreshNotes(Tag.modelType(), action.id);
}
if (action.type == 'SEARCH_SELECT') {
await this.refreshNotes(BaseModel.TYPE_SEARCH, action.id);
}
if (this.hasGui() && action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTING_UPDATE_ALL') {
reg.setupRecurrentSync();
}
return result;
generalMiddlewareFn() {
const middleware = store => next => (action) => {
return this.generalMiddleware(store, next, action);
}
return middleware;
}
async generalMiddleware(store, next, action) {
this.logger().debug('Reducer action', this.reducerActionToString(action));
const result = next(action);
const newState = store.getState();
if (action.type == 'FOLDER_SELECT' || action.type === 'FOLDER_DELETE') {
Setting.setValue('activeFolderId', newState.selectedFolderId);
this.currentFolder_ = newState.selectedFolderId ? await Folder.load(newState.selectedFolderId) : null;
await this.refreshNotes(Folder.modelType(), newState.selectedFolderId);
}
if (action.type == 'TAG_SELECT') {
await this.refreshNotes(Tag.modelType(), action.id);
}
if (action.type == 'SEARCH_SELECT') {
await this.refreshNotes(BaseModel.TYPE_SEARCH, action.id);
}
if (this.hasGui() && action.type == 'SETTING_UPDATE_ONE' && action.key == 'sync.interval' || action.type == 'SETTING_UPDATE_ALL') {
reg.setupRecurrentSync();
}
return result;
}
dispatch(action) {
if (this.store()) return this.store().dispatch(action);
}
@ -236,7 +240,7 @@ class BaseApplication {
}
initRedux() {
this.store_ = createStore(this.reducer, applyMiddleware(this.generalMiddleware()));
this.store_ = createStore(this.reducer, applyMiddleware(this.generalMiddlewareFn()));
BaseModel.dispatch = this.store().dispatch;
FoldersScreenUtils.dispatch = this.store().dispatch;
reg.dispatch = this.store().dispatch;