1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-17 19:51:42 +00:00

Refactoring Set initialization methods

This commit is contained in:
bimlas
2018-05-25 14:08:44 +02:00
parent d78b94f4e8
commit 89850c0b22

View File

@@ -38,27 +38,14 @@ function data (state = defaultDataMap(), action) {
if (note.isTrashed) { if (note.isTrashed) {
state.trashedSet.add(uniqueKey) state.trashedSet.add(uniqueKey)
} }
const storageNoteList = getOrInitItem(state.storageNoteMap, note.storage)
let storageNoteList = state.storageNoteMap.get(note.storage)
if (storageNoteList == null) {
storageNoteList = new Set(storageNoteList)
state.storageNoteMap.set(note.storage, storageNoteList)
}
storageNoteList.add(uniqueKey) storageNoteList.add(uniqueKey)
let folderNoteSet = state.folderNoteMap.get(folderKey) const folderNoteSet = getOrInitItem(state.folderNoteMap, folderKey)
if (folderNoteSet == null) {
folderNoteSet = new Set(folderNoteSet)
state.folderNoteMap.set(folderKey, folderNoteSet)
}
folderNoteSet.add(uniqueKey) folderNoteSet.add(uniqueKey)
note.tags.forEach((tag) => { note.tags.forEach((tag) => {
let tagNoteList = state.tagNoteMap.get(tag) const tagNoteList = getOrInitItem(state.tagNoteMap, tag)
if (tagNoteList == null) {
tagNoteList = new Set(tagNoteList)
state.tagNoteMap.set(tag, tagNoteList)
}
tagNoteList.add(uniqueKey) tagNoteList.add(uniqueKey)
}) })
}) })
@@ -146,11 +133,7 @@ function data (state = defaultDataMap(), action) {
} else { } else {
state.tagNoteMap = new Map(state.tagNoteMap) state.tagNoteMap = new Map(state.tagNoteMap)
note.tags.forEach((tag) => { note.tags.forEach((tag) => {
let tagNoteList = state.tagNoteMap.get(tag) const tagNoteList = getOrInitItem(state.tagNoteMap, tag)
if (tagNoteList == null) {
tagNoteList = new Set(tagNoteList)
state.tagNoteMap.set(tag, tagNoteList)
}
tagNoteList.add(uniqueKey) tagNoteList.add(uniqueKey)
}) })
} }
@@ -262,11 +245,7 @@ function data (state = defaultDataMap(), action) {
} else { } else {
state.tagNoteMap = new Map(state.tagNoteMap) state.tagNoteMap = new Map(state.tagNoteMap)
note.tags.forEach((tag) => { note.tags.forEach((tag) => {
let tagNoteList = state.tagNoteMap.get(tag) const tagNoteList = getOrInitItem(state.tagNoteMap, tag)
if (tagNoteList == null) {
tagNoteList = new Set(tagNoteList)
state.tagNoteMap.set(tag, tagNoteList)
}
tagNoteList.add(uniqueKey) tagNoteList.add(uniqueKey)
}) })
} }
@@ -380,9 +359,7 @@ function data (state = defaultDataMap(), action) {
// Delete key from tag map // Delete key from tag map
state.tagNoteMap = new Map(state.tagNoteMap) state.tagNoteMap = new Map(state.tagNoteMap)
note.tags.forEach((tag) => { note.tags.forEach((tag) => {
let tagNoteSet = state.tagNoteMap.get(tag) const tagNoteSet = getOrInitItem(state.tagNoteMap, tag)
tagNoteSet = new Set(tagNoteSet)
state.tagNoteMap.set(tag, tagNoteSet)
tagNoteSet.delete(noteKey) tagNoteSet.delete(noteKey)
}) })
} }
@@ -409,11 +386,7 @@ function data (state = defaultDataMap(), action) {
state.starredSet.add(uniqueKey) state.starredSet.add(uniqueKey)
} }
let storageNoteList = state.storageNoteMap.get(note.storage) const storageNoteList = getOrInitItem(state.tagNoteMap, note.storage)
if (storageNoteList == null) {
storageNoteList = new Set(storageNoteList)
state.storageNoteMap.set(note.storage, storageNoteList)
}
storageNoteList.add(uniqueKey) storageNoteList.add(uniqueKey)
let folderNoteSet = state.folderNoteMap.get(folderKey) let folderNoteSet = state.folderNoteMap.get(folderKey)
@@ -424,11 +397,7 @@ function data (state = defaultDataMap(), action) {
folderNoteSet.add(uniqueKey) folderNoteSet.add(uniqueKey)
note.tags.forEach((tag) => { note.tags.forEach((tag) => {
let tagNoteSet = state.tagNoteMap.get(tag) const tagNoteSet = getOrInitItem(state.tagNoteMap, tag)
if (tagNoteSet == null) {
tagNoteSet = new Set(tagNoteSet)
state.tagNoteMap.set(tag, tagNoteSet)
}
tagNoteSet.add(uniqueKey) tagNoteSet.add(uniqueKey)
}) })
}) })
@@ -534,15 +503,21 @@ function updateTagChanges (oldNote, note, state, uniqueKey) {
} }
}) })
addedTags.forEach((tag) => { addedTags.forEach((tag) => {
let tagNoteList = state.tagNoteMap.get(tag) const tagNoteList = getOrInitItem(state.tagNoteMap, tag)
tagNoteList = new Set(tagNoteList)
tagNoteList.add(uniqueKey) 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({ const reducer = combineReducers({
data, data,
config, config,