From d78b94f4e83210edd2592afbd4a186bfaffb9cdf Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 12:19:50 +0200 Subject: [PATCH 1/6] Refactoring update of tag changes --- browser/main/store.js | 68 +++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index 27796d30..965d848c 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -142,27 +142,7 @@ function data (state = defaultDataMap(), action) { } if (oldNote != null) { - const discardedTags = _.difference(oldNote.tags, note.tags) - const addedTags = _.difference(note.tags, oldNote.tags) - if (discardedTags.length + addedTags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - - discardedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.delete(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) - addedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - tagNoteList = new Set(tagNoteList) - tagNoteList.add(uniqueKey) - - state.tagNoteMap.set(tag, tagNoteList) - }) - } + updateTagChanges(oldNote, note, state, uniqueKey) } else { state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { @@ -278,27 +258,7 @@ function data (state = defaultDataMap(), action) { // Remove from old folder map if (oldNote != null) { - const discardedTags = _.difference(oldNote.tags, note.tags) - const addedTags = _.difference(note.tags, oldNote.tags) - if (discardedTags.length + addedTags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - - discardedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.delete(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) - addedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - tagNoteList = new Set(tagNoteList) - tagNoteList.add(uniqueKey) - - state.tagNoteMap.set(tag, tagNoteList) - }) - } + updateTagChanges(oldNote, note, state, uniqueKey) } else { state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { @@ -559,6 +519,30 @@ function status (state = defaultStatus, action) { return state } +function updateTagChanges (oldNote, note, state, uniqueKey) { + const discardedTags = _.difference(oldNote.tags, note.tags) + const addedTags = _.difference(note.tags, oldNote.tags) + if (discardedTags.length + addedTags.length > 0) { + state.tagNoteMap = new Map(state.tagNoteMap) + + discardedTags.forEach((tag) => { + let tagNoteList = state.tagNoteMap.get(tag) + if (tagNoteList != null) { + tagNoteList = new Set(tagNoteList) + tagNoteList.delete(uniqueKey) + state.tagNoteMap.set(tag, tagNoteList) + } + }) + addedTags.forEach((tag) => { + let tagNoteList = state.tagNoteMap.get(tag) + tagNoteList = new Set(tagNoteList) + tagNoteList.add(uniqueKey) + + state.tagNoteMap.set(tag, tagNoteList) + }) + } +} + const reducer = combineReducers({ data, config, From 89850c0b22baee7c08ef6b88054f96837e0a8dde Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 14:08:44 +0200 Subject: [PATCH 2/6] Refactoring Set initialization methods --- browser/main/store.js | 61 +++++++++++++------------------------------ 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index 965d848c..087a8aa6 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -38,27 +38,14 @@ function data (state = defaultDataMap(), action) { if (note.isTrashed) { state.trashedSet.add(uniqueKey) } - - let storageNoteList = state.storageNoteMap.get(note.storage) - if (storageNoteList == null) { - storageNoteList = new Set(storageNoteList) - state.storageNoteMap.set(note.storage, storageNoteList) - } + const storageNoteList = getOrInitItem(state.storageNoteMap, note.storage) storageNoteList.add(uniqueKey) - let folderNoteSet = state.folderNoteMap.get(folderKey) - if (folderNoteSet == null) { - folderNoteSet = new Set(folderNoteSet) - state.folderNoteMap.set(folderKey, folderNoteSet) - } + const folderNoteSet = getOrInitItem(state.folderNoteMap, folderKey) folderNoteSet.add(uniqueKey) note.tags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList == null) { - tagNoteList = new Set(tagNoteList) - state.tagNoteMap.set(tag, tagNoteList) - } + const tagNoteList = getOrInitItem(state.tagNoteMap, tag) tagNoteList.add(uniqueKey) }) }) @@ -146,11 +133,7 @@ function data (state = defaultDataMap(), action) { } else { state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList == null) { - tagNoteList = new Set(tagNoteList) - state.tagNoteMap.set(tag, tagNoteList) - } + const tagNoteList = getOrInitItem(state.tagNoteMap, tag) tagNoteList.add(uniqueKey) }) } @@ -262,11 +245,7 @@ function data (state = defaultDataMap(), action) { } else { state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList == null) { - tagNoteList = new Set(tagNoteList) - state.tagNoteMap.set(tag, tagNoteList) - } + const tagNoteList = getOrInitItem(state.tagNoteMap, tag) tagNoteList.add(uniqueKey) }) } @@ -380,9 +359,7 @@ function data (state = defaultDataMap(), action) { // Delete key from tag map state.tagNoteMap = new Map(state.tagNoteMap) note.tags.forEach((tag) => { - let tagNoteSet = state.tagNoteMap.get(tag) - tagNoteSet = new Set(tagNoteSet) - state.tagNoteMap.set(tag, tagNoteSet) + const tagNoteSet = getOrInitItem(state.tagNoteMap, tag) tagNoteSet.delete(noteKey) }) } @@ -409,11 +386,7 @@ function data (state = defaultDataMap(), action) { state.starredSet.add(uniqueKey) } - let storageNoteList = state.storageNoteMap.get(note.storage) - if (storageNoteList == null) { - storageNoteList = new Set(storageNoteList) - state.storageNoteMap.set(note.storage, storageNoteList) - } + const storageNoteList = getOrInitItem(state.tagNoteMap, note.storage) storageNoteList.add(uniqueKey) let folderNoteSet = state.folderNoteMap.get(folderKey) @@ -424,11 +397,7 @@ function data (state = defaultDataMap(), action) { folderNoteSet.add(uniqueKey) note.tags.forEach((tag) => { - let tagNoteSet = state.tagNoteMap.get(tag) - if (tagNoteSet == null) { - tagNoteSet = new Set(tagNoteSet) - state.tagNoteMap.set(tag, tagNoteSet) - } + const tagNoteSet = getOrInitItem(state.tagNoteMap, tag) tagNoteSet.add(uniqueKey) }) }) @@ -534,15 +503,21 @@ function updateTagChanges (oldNote, note, state, uniqueKey) { } }) addedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - tagNoteList = new Set(tagNoteList) + const tagNoteList = getOrInitItem(state.tagNoteMap, tag) tagNoteList.add(uniqueKey) - - state.tagNoteMap.set(tag, tagNoteList) }) } } +function getOrInitItem (target, key) { + let results = target.get(key) + if (results == null) { + results = new Set() + target.set(key, results) + } + return results +} + const reducer = combineReducers({ data, config, From 0cb5554ae529b55c6d651f0a8b10202c8003cd95 Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 19:37:20 +0200 Subject: [PATCH 3/6] Refactoring assignment of unique key to tags --- browser/main/store.js | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index 087a8aa6..9f9d65e2 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -44,10 +44,7 @@ function data (state = defaultDataMap(), action) { const folderNoteSet = getOrInitItem(state.folderNoteMap, folderKey) folderNoteSet.add(uniqueKey) - note.tags.forEach((tag) => { - const tagNoteList = getOrInitItem(state.tagNoteMap, tag) - tagNoteList.add(uniqueKey) - }) + assignToTags(note.tags, state, uniqueKey) }) return state case 'UPDATE_NOTE': @@ -87,14 +84,7 @@ function data (state = defaultDataMap(), action) { } else { state.trashedSet.delete(uniqueKey) - note.tags.forEach(tag => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.add(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) + assignToTags(note.tags, state, uniqueKey) if (note.isStarred) { state.starredSet.add(uniqueKey) @@ -131,11 +121,7 @@ function data (state = defaultDataMap(), action) { if (oldNote != null) { updateTagChanges(oldNote, note, state, uniqueKey) } else { - state.tagNoteMap = new Map(state.tagNoteMap) - note.tags.forEach((tag) => { - const tagNoteList = getOrInitItem(state.tagNoteMap, tag) - tagNoteList.add(uniqueKey) - }) + assignToTags(note.tags, state, uniqueKey) } return state @@ -243,11 +229,7 @@ function data (state = defaultDataMap(), action) { if (oldNote != null) { updateTagChanges(oldNote, note, state, uniqueKey) } else { - state.tagNoteMap = new Map(state.tagNoteMap) - note.tags.forEach((tag) => { - const tagNoteList = getOrInitItem(state.tagNoteMap, tag) - tagNoteList.add(uniqueKey) - }) + assignToTags(note.tags, state, uniqueKey) } return state @@ -502,13 +484,18 @@ function updateTagChanges (oldNote, note, state, uniqueKey) { state.tagNoteMap.set(tag, tagNoteList) } }) - addedTags.forEach((tag) => { - const tagNoteList = getOrInitItem(state.tagNoteMap, tag) - tagNoteList.add(uniqueKey) - }) + assignToTags(addedTags, state, uniqueKey) } } +function assignToTags (tags, state, uniqueKey) { + state.tagNoteMap = new Map(state.tagNoteMap) + tags.forEach((tag) => { + const tagNoteList = getOrInitItem(state.tagNoteMap, tag) + tagNoteList.add(uniqueKey) + }) +} + function getOrInitItem (target, key) { let results = target.get(key) if (results == null) { From ed2698ecc3bc33a93811b56be482e4dee405c681 Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 19:56:58 +0200 Subject: [PATCH 4/6] Refactoring removing of unique key from tags --- browser/main/store.js | 55 +++++++++++++------------------------------ 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index 9f9d65e2..ed1b08d7 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -72,15 +72,7 @@ function data (state = defaultDataMap(), action) { if (note.isTrashed) { state.trashedSet.add(uniqueKey) state.starredSet.delete(uniqueKey) - - note.tags.forEach(tag => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.delete(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) + removeFromTags(note.tags, state, uniqueKey) } else { state.trashedSet.delete(uniqueKey) @@ -169,16 +161,7 @@ function data (state = defaultDataMap(), action) { originFolderList.delete(originKey) state.folderNoteMap.set(originFolderKey, originFolderList) - // From tagMap - if (originNote.tags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - originNote.tags.forEach((tag) => { - let noteSet = state.tagNoteMap.get(tag) - noteSet = new Set(noteSet) - noteSet.delete(originKey) - state.tagNoteMap.set(tag, noteSet) - }) - } + removeFromTags(originNote.tags, state, originKey) } if (oldNote == null || oldNote.isStarred !== note.isStarred) { @@ -268,16 +251,7 @@ function data (state = defaultDataMap(), action) { folderSet.delete(uniqueKey) state.folderNoteMap.set(folderKey, folderSet) - // From tagMap - if (targetNote.tags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - targetNote.tags.forEach((tag) => { - let noteSet = state.tagNoteMap.get(tag) - noteSet = new Set(noteSet) - noteSet.delete(uniqueKey) - state.tagNoteMap.set(tag, noteSet) - }) - } + removeFromTags(targetNote.tags, state, uniqueKey) } state.noteMap = new Map(state.noteMap) state.noteMap.delete(uniqueKey) @@ -474,16 +448,7 @@ function updateTagChanges (oldNote, note, state, uniqueKey) { const discardedTags = _.difference(oldNote.tags, note.tags) const addedTags = _.difference(note.tags, oldNote.tags) if (discardedTags.length + addedTags.length > 0) { - state.tagNoteMap = new Map(state.tagNoteMap) - - discardedTags.forEach((tag) => { - let tagNoteList = state.tagNoteMap.get(tag) - if (tagNoteList != null) { - tagNoteList = new Set(tagNoteList) - tagNoteList.delete(uniqueKey) - state.tagNoteMap.set(tag, tagNoteList) - } - }) + removeFromTags(discardedTags, state, uniqueKey) assignToTags(addedTags, state, uniqueKey) } } @@ -496,6 +461,18 @@ function assignToTags (tags, state, uniqueKey) { }) } +function removeFromTags (tags, state, uniqueKey) { + state.tagNoteMap = new Map(state.tagNoteMap) + tags.forEach(tag => { + let tagNoteList = state.tagNoteMap.get(tag) + if (tagNoteList != null) { + tagNoteList = new Set(tagNoteList) + tagNoteList.delete(uniqueKey) + state.tagNoteMap.set(tag, tagNoteList) + } + }) +} + function getOrInitItem (target, key) { let results = target.get(key) if (results == null) { From 06f33d9a631dc5301a4b24fa954c3181296496f1 Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 20:01:34 +0200 Subject: [PATCH 5/6] Refactoring changing of "starred" --- browser/main/store.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index ed1b08d7..a4c1594d 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -58,14 +58,7 @@ function data (state = defaultDataMap(), action) { state.noteMap = new Map(state.noteMap) state.noteMap.set(uniqueKey, note) - if (oldNote == null || oldNote.isStarred !== note.isStarred) { - state.starredSet = new Set(state.starredSet) - if (note.isStarred) { - state.starredSet.add(uniqueKey) - } else { - state.starredSet.delete(uniqueKey) - } - } + updateStarredChange(oldNote, note, state, uniqueKey) if (oldNote == null || oldNote.isTrashed !== note.isTrashed) { state.trashedSet = new Set(state.trashedSet) @@ -164,14 +157,7 @@ function data (state = defaultDataMap(), action) { removeFromTags(originNote.tags, state, originKey) } - if (oldNote == null || oldNote.isStarred !== note.isStarred) { - state.starredSet = new Set(state.starredSet) - if (note.isStarred) { - state.starredSet.add(uniqueKey) - } else { - state.starredSet.delete(uniqueKey) - } - } + updateStarredChange(oldNote, note, state, uniqueKey) if (oldNote == null || oldNote.isTrashed !== note.isTrashed) { state.trashedSet = new Set(state.trashedSet) @@ -444,6 +430,17 @@ function status (state = defaultStatus, action) { return state } +function updateStarredChange (oldNote, note, state, uniqueKey) { + if (oldNote == null || oldNote.isStarred !== note.isStarred) { + state.starredSet = new Set(state.starredSet) + if (note.isStarred) { + state.starredSet.add(uniqueKey) + } else { + state.starredSet.delete(uniqueKey) + } + } +} + function updateTagChanges (oldNote, note, state, uniqueKey) { const discardedTags = _.difference(oldNote.tags, note.tags) const addedTags = _.difference(note.tags, oldNote.tags) From 53728a0f4a68b778c52482be504bcb0456108844 Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 20:07:38 +0200 Subject: [PATCH 6/6] Refactoring changing of folder --- browser/main/store.js | 50 +++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/browser/main/store.js b/browser/main/store.js index a4c1594d..7ea6decb 100644 --- a/browser/main/store.js +++ b/browser/main/store.js @@ -87,21 +87,7 @@ function data (state = defaultDataMap(), action) { } // Update foldermap if folder changed or post created - if (oldNote == null || oldNote.folder !== note.folder) { - state.folderNoteMap = new Map(state.folderNoteMap) - let folderNoteSet = state.folderNoteMap.get(folderKey) - folderNoteSet = new Set(folderNoteSet) - folderNoteSet.add(uniqueKey) - state.folderNoteMap.set(folderKey, folderNoteSet) - - if (oldNote != null) { - const oldFolderKey = oldNote.storage + '-' + oldNote.folder - let oldFolderNoteList = state.folderNoteMap.get(oldFolderKey) - oldFolderNoteList = new Set(oldFolderNoteList) - oldFolderNoteList.delete(uniqueKey) - state.folderNoteMap.set(oldFolderKey, oldFolderNoteList) - } - } + updateFolderChange(oldNote, note, state, folderKey, uniqueKey) if (oldNote != null) { updateTagChanges(oldNote, note, state, uniqueKey) @@ -178,21 +164,7 @@ function data (state = defaultDataMap(), action) { } // Update foldermap if folder changed or post created - if (oldNote == null || oldNote.folder !== note.folder) { - state.folderNoteMap = new Map(state.folderNoteMap) - let folderNoteList = state.folderNoteMap.get(folderKey) - folderNoteList = new Set(folderNoteList) - folderNoteList.add(uniqueKey) - state.folderNoteMap.set(folderKey, folderNoteList) - - if (oldNote != null) { - const oldFolderKey = oldNote.storage + '-' + oldNote.folder - let oldFolderNoteList = state.folderNoteMap.get(oldFolderKey) - oldFolderNoteList = new Set(oldFolderNoteList) - oldFolderNoteList.delete(uniqueKey) - state.folderNoteMap.set(oldFolderKey, oldFolderNoteList) - } - } + updateFolderChange(oldNote, note, state, folderKey, uniqueKey) // Remove from old folder map if (oldNote != null) { @@ -441,6 +413,24 @@ function updateStarredChange (oldNote, note, state, uniqueKey) { } } +function updateFolderChange (oldNote, note, state, folderKey, uniqueKey) { + if (oldNote == null || oldNote.folder !== note.folder) { + state.folderNoteMap = new Map(state.folderNoteMap) + let folderNoteList = state.folderNoteMap.get(folderKey) + folderNoteList = new Set(folderNoteList) + folderNoteList.add(uniqueKey) + state.folderNoteMap.set(folderKey, folderNoteList) + + if (oldNote != null) { + const oldFolderKey = oldNote.storage + '-' + oldNote.folder + let oldFolderNoteList = state.folderNoteMap.get(oldFolderKey) + oldFolderNoteList = new Set(oldFolderNoteList) + oldFolderNoteList.delete(uniqueKey) + state.folderNoteMap.set(oldFolderKey, oldFolderNoteList) + } + } +} + function updateTagChanges (oldNote, note, state, uniqueKey) { const discardedTags = _.difference(oldNote.tags, note.tags) const addedTags = _.difference(note.tags, oldNote.tags)