From 0cb5554ae529b55c6d651f0a8b10202c8003cd95 Mon Sep 17 00:00:00 2001 From: bimlas Date: Fri, 25 May 2018 19:37:20 +0200 Subject: [PATCH] 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) {