mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-17 11:41:44 +00:00
Merge pull request #1932 from bimlas/fix-logic-of-state-notemap-set
Refactoring of store.js
This commit is contained in:
@@ -38,29 +38,13 @@ 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) => {
|
assignToTags(note.tags, state, uniqueKey)
|
||||||
let tagNoteList = state.tagNoteMap.get(tag)
|
|
||||||
if (tagNoteList == null) {
|
|
||||||
tagNoteList = new Set(tagNoteList)
|
|
||||||
state.tagNoteMap.set(tag, tagNoteList)
|
|
||||||
}
|
|
||||||
tagNoteList.add(uniqueKey)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
return state
|
return state
|
||||||
case 'UPDATE_NOTE':
|
case 'UPDATE_NOTE':
|
||||||
@@ -74,40 +58,18 @@ function data (state = defaultDataMap(), action) {
|
|||||||
state.noteMap = new Map(state.noteMap)
|
state.noteMap = new Map(state.noteMap)
|
||||||
state.noteMap.set(uniqueKey, note)
|
state.noteMap.set(uniqueKey, note)
|
||||||
|
|
||||||
if (oldNote == null || oldNote.isStarred !== note.isStarred) {
|
updateStarredChange(oldNote, note, state, uniqueKey)
|
||||||
state.starredSet = new Set(state.starredSet)
|
|
||||||
if (note.isStarred) {
|
|
||||||
state.starredSet.add(uniqueKey)
|
|
||||||
} else {
|
|
||||||
state.starredSet.delete(uniqueKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldNote == null || oldNote.isTrashed !== note.isTrashed) {
|
if (oldNote == null || oldNote.isTrashed !== note.isTrashed) {
|
||||||
state.trashedSet = new Set(state.trashedSet)
|
state.trashedSet = new Set(state.trashedSet)
|
||||||
if (note.isTrashed) {
|
if (note.isTrashed) {
|
||||||
state.trashedSet.add(uniqueKey)
|
state.trashedSet.add(uniqueKey)
|
||||||
state.starredSet.delete(uniqueKey)
|
state.starredSet.delete(uniqueKey)
|
||||||
|
removeFromTags(note.tags, state, 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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
state.trashedSet.delete(uniqueKey)
|
state.trashedSet.delete(uniqueKey)
|
||||||
|
|
||||||
note.tags.forEach(tag => {
|
assignToTags(note.tags, state, uniqueKey)
|
||||||
let tagNoteList = state.tagNoteMap.get(tag)
|
|
||||||
if (tagNoteList != null) {
|
|
||||||
tagNoteList = new Set(tagNoteList)
|
|
||||||
tagNoteList.add(uniqueKey)
|
|
||||||
state.tagNoteMap.set(tag, tagNoteList)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (note.isStarred) {
|
if (note.isStarred) {
|
||||||
state.starredSet.add(uniqueKey)
|
state.starredSet.add(uniqueKey)
|
||||||
@@ -125,54 +87,12 @@ function data (state = defaultDataMap(), action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update foldermap if folder changed or post created
|
// Update foldermap if folder changed or post created
|
||||||
if (oldNote == null || oldNote.folder !== note.folder) {
|
updateFolderChange(oldNote, note, state, folderKey, uniqueKey)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldNote != null) {
|
if (oldNote != null) {
|
||||||
const discardedTags = _.difference(oldNote.tags, note.tags)
|
updateTagChanges(oldNote, note, state, uniqueKey)
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
state.tagNoteMap = new Map(state.tagNoteMap)
|
assignToTags(note.tags, state, uniqueKey)
|
||||||
note.tags.forEach((tag) => {
|
|
||||||
let tagNoteList = state.tagNoteMap.get(tag)
|
|
||||||
if (tagNoteList == null) {
|
|
||||||
tagNoteList = new Set(tagNoteList)
|
|
||||||
state.tagNoteMap.set(tag, tagNoteList)
|
|
||||||
}
|
|
||||||
tagNoteList.add(uniqueKey)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@@ -220,26 +140,10 @@ function data (state = defaultDataMap(), action) {
|
|||||||
originFolderList.delete(originKey)
|
originFolderList.delete(originKey)
|
||||||
state.folderNoteMap.set(originFolderKey, originFolderList)
|
state.folderNoteMap.set(originFolderKey, originFolderList)
|
||||||
|
|
||||||
// From tagMap
|
removeFromTags(originNote.tags, state, originKey)
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oldNote == null || oldNote.isStarred !== note.isStarred) {
|
updateStarredChange(oldNote, note, state, uniqueKey)
|
||||||
state.starredSet = new Set(state.starredSet)
|
|
||||||
if (note.isStarred) {
|
|
||||||
state.starredSet.add(uniqueKey)
|
|
||||||
} else {
|
|
||||||
state.starredSet.delete(uniqueKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldNote == null || oldNote.isTrashed !== note.isTrashed) {
|
if (oldNote == null || oldNote.isTrashed !== note.isTrashed) {
|
||||||
state.trashedSet = new Set(state.trashedSet)
|
state.trashedSet = new Set(state.trashedSet)
|
||||||
@@ -260,55 +164,13 @@ function data (state = defaultDataMap(), action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update foldermap if folder changed or post created
|
// Update foldermap if folder changed or post created
|
||||||
if (oldNote == null || oldNote.folder !== note.folder) {
|
updateFolderChange(oldNote, note, state, folderKey, uniqueKey)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove from old folder map
|
// Remove from old folder map
|
||||||
if (oldNote != null) {
|
if (oldNote != null) {
|
||||||
const discardedTags = _.difference(oldNote.tags, note.tags)
|
updateTagChanges(oldNote, note, state, uniqueKey)
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
state.tagNoteMap = new Map(state.tagNoteMap)
|
assignToTags(note.tags, state, uniqueKey)
|
||||||
note.tags.forEach((tag) => {
|
|
||||||
let tagNoteList = state.tagNoteMap.get(tag)
|
|
||||||
if (tagNoteList == null) {
|
|
||||||
tagNoteList = new Set(tagNoteList)
|
|
||||||
state.tagNoteMap.set(tag, tagNoteList)
|
|
||||||
}
|
|
||||||
tagNoteList.add(uniqueKey)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return state
|
return state
|
||||||
@@ -347,16 +209,7 @@ function data (state = defaultDataMap(), action) {
|
|||||||
folderSet.delete(uniqueKey)
|
folderSet.delete(uniqueKey)
|
||||||
state.folderNoteMap.set(folderKey, folderSet)
|
state.folderNoteMap.set(folderKey, folderSet)
|
||||||
|
|
||||||
// From tagMap
|
removeFromTags(targetNote.tags, state, uniqueKey)
|
||||||
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
state.noteMap = new Map(state.noteMap)
|
state.noteMap = new Map(state.noteMap)
|
||||||
state.noteMap.delete(uniqueKey)
|
state.noteMap.delete(uniqueKey)
|
||||||
@@ -420,9 +273,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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -449,11 +300,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)
|
||||||
@@ -464,11 +311,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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -559,6 +402,73 @@ function status (state = defaultStatus, action) {
|
|||||||
return state
|
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 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)
|
||||||
|
if (discardedTags.length + addedTags.length > 0) {
|
||||||
|
removeFromTags(discardedTags, state, 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 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) {
|
||||||
|
results = new Set()
|
||||||
|
target.set(key, results)
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
const reducer = combineReducers({
|
const reducer = combineReducers({
|
||||||
data,
|
data,
|
||||||
config,
|
config,
|
||||||
|
|||||||
Reference in New Issue
Block a user