TagSpaces is an offline, open source, document manager with tagging support
optimizing state update performance
Ilian Sapundshiev4639c7a33f78
diff --git a/app/reducers/app.js b/app/reducers/app.jsindex 39f7cd37e..8210160b7 100644--- a/app/reducers/app.js+++ b/app/reducers/app.js@@ -306,47 +306,25 @@ export default (state: Object = initialState, action: Object) => {return state;}case types.REFLECT_CREATE_ENTRY: {- let currentDirectoryEntries = state.currentDirectoryEntries;- // TODO Case changed current directory- if (state.currentDirectoryPath === action.newEntry.path) {- // Reindex ??- }-- // Workarround for double update caused by the watcher- let entryAlreadyAdded = false;- state.currentDirectoryEntries.forEach((entry) => {- if (entry.path === action.newEntry.path) {- entryAlreadyAdded = true;- }- });+ // Prevent adding entry twice e.g. by the watcher+ const entryIndex = state.currentDirectoryEntries.findIndex((entry) => entry.path === action.newEntry.path);if (- !entryAlreadyAdded &&+ entryIndex < 0 &&extractParentDirectoryPath(action.newEntry.path).replace(/(^\/)|(\/$)/g, '') === state.currentDirectoryPath.replace(/(^\/)|(\/$)/g, '')) {- currentDirectoryEntries = [ // TODO evtl. apply sorting- ...state.currentDirectoryEntries,- action.newEntry- ];+ return {+ ...state,+ currentDirectoryEntries: [+ ...state.currentDirectoryEntries,+ action.newEntry+ ]+ };}- return {- ...state,- currentDirectoryEntries- };+ return state;}case types.REFLECT_RENAME_ENTRY: {- let indexForRenaming = -1;- let indexForRenamingInOpenedFiles = -1;- // Updating entries in the current directory- state.currentDirectoryEntries.forEach((entry, index) => {- if (entry.path === action.path) {- indexForRenaming = index;- }- });- state.openedFiles.forEach((entry, index) => {- if (entry.path === action.path) { // TODO handle directory case, update contained files and dirs too- indexForRenamingInOpenedFiles = index;- }- });+ const indexForRenaming = state.currentDirectoryEntries.findIndex((entry) => entry.path === action.path);+ const indexForRenamingInOpenedFiles = state.openedFiles.findIndex((entry) => entry.path === action.path);let directoryEntries = state.currentDirectoryEntries;let openedFiles = state.openedFiles;if (indexForRenaming >= 0) {@@ -388,19 +366,8 @@ export default (state: Object = initialState, action: Object) => {return state;}case types.REFLECT_UPDATE_SIDECARTAGS: {- let indexForUpdating = -1;- let indexForUpdatingInOpenedFiles = -1;- // Updating entries in the current directory- state.currentDirectoryEntries.forEach((entry, index) => {- if (entry.path === action.path) {- indexForUpdating = index;- }- });- state.openedFiles.forEach((entry, index) => {- if (entry.path === action.path) {- indexForUpdatingInOpenedFiles = index;- }- });+ const indexForUpdating = state.currentDirectoryEntries.findIndex((entry) => entry.path === action.path);+ const indexForUpdatingInOpenedFiles = state.openedFiles.findIndex((entry) => entry.path === action.path);let directoryEntries = state.currentDirectoryEntries;let openedFiles = state.openedFiles;if (indexForUpdating >= 0) {@@ -438,19 +405,8 @@ export default (state: Object = initialState, action: Object) => {return state;}case types.REFLECT_UPDATE_SIDECARMETA: {- let indexForUpdating = -1;- let indexForUpdatingInOpenedFiles = -1;- // Updating entries in the current directory- state.currentDirectoryEntries.forEach((entry, index) => {- if (entry.path === action.path) {- indexForUpdating = index;- }- });- state.openedFiles.forEach((entry, index) => {- if (entry.path === action.path) {- indexForUpdatingInOpenedFiles = index;- }- });+ const indexForUpdating = state.currentDirectoryEntries.findIndex((entry) => entry.path === action.path);+ const indexForUpdatingInOpenedFiles = state.openedFiles.findIndex((entry) => entry.path === action.path);let directoryEntries = state.currentDirectoryEntries;let openedFiles = state.openedFiles;if (indexForUpdating >= 0) {diff --git a/app/reducers/location-index.js b/app/reducers/location-index.jsindex bb7dde3b6..2c815d0fc 100644--- a/app/reducers/location-index.js+++ b/app/reducers/location-index.js@@ -110,18 +110,12 @@ export default (state: Object = initialState, action: Object) => {};}case types.REFLECT_RENAME_ENTRY: {- let indexForRenamingInIndex = -1;- state.currentDirectoryIndex.forEach((entry, index) => {- if (entry.path === action.path) { // TODO handle directory case, update contained files and dirs too- indexForRenamingInIndex = index;- }- });- let directoryIndex = state.currentDirectoryIndex;+ const indexForRenamingInIndex = state.currentDirectoryIndex.findIndex((entry) => entry.path === action.path);if (indexForRenamingInIndex >= 0) {const updateEntry = {...state.currentDirectoryIndex[indexForRenamingInIndex],- path: action.newPath, // TODO handle thumbPath- thumbPath: getThumbFileLocationForFile(action.newPath),+ path: action.newPath,+ // thumbPath: getThumbFileLocationForFile(action.newPath), // disabled due performance concernsname: extractFileName(action.newPath),extension: extractFileExtension(action.newPath),tags: [@@ -129,27 +123,19 @@ export default (state: Object = initialState, action: Object) => {...extractTagsAsObjects(action.newPath) // , getTagDelimiter(state))]};- directoryIndex = [- ...state.currentDirectoryIndex.slice(0, indexForRenamingInIndex),- updateEntry,- ...state.currentDirectoryIndex.slice(indexForRenamingInIndex + 1)- ];- }- if (indexForRenamingInIndex >= 0) {return {...state,- currentDirectoryIndex: directoryIndex,+ currentDirectoryIndex: [+ ...state.currentDirectoryIndex.slice(0, indexForRenamingInIndex),+ updateEntry,+ ...state.currentDirectoryIndex.slice(indexForRenamingInIndex + 1)+ ]};}return state;}case types.REFLECT_UPDATE_SIDECARTAGS: {- let indexForUpdatingInIndex = -1;- state.currentDirectoryIndex.forEach((entry, index) => {- if (entry.path === action.path) {- indexForUpdatingInIndex = index;- }- });+ const indexForUpdatingInIndex = state.currentDirectoryIndex.findIndex((entry) => entry.path === action.path);if (indexForUpdatingInIndex >= 0) {const updateEntry = {...state.currentDirectoryIndex[indexForUpdatingInIndex],@@ -170,12 +156,7 @@ export default (state: Object = initialState, action: Object) => {return state;}case types.REFLECT_UPDATE_SIDECARMETA: {- let indexForUpdatingInIndex = -1;- state.currentDirectoryIndex.forEach((entry, index) => {- if (entry.path === action.path) {- indexForUpdatingInIndex = index;- }- });+ const indexForUpdatingInIndex = state.currentDirectoryIndex.findIndex((entry) => entry.path === action.path);if (indexForUpdatingInIndex >= 0) {const updateEntry = {...state.currentDirectoryIndex[indexForUpdatingInIndex],