diff --git a/web/src/js/ducks/utils/view.js b/web/src/js/ducks/utils/view.js index c00f00bd1..2f1e03fa8 100755 --- a/web/src/js/ducks/utils/view.js +++ b/web/src/js/ducks/utils/view.js @@ -57,16 +57,9 @@ export default function reduce(state = defaultState, action) { if (state.indexOf[action.item.id] == null) { return } - const nextState = { - ...state, - ...sortedRemove(state, action.item.id), - } - if (!action.filter(action.item)) { - return nextState - } return { - ...nextState, - ...sortedInsert(nextState, action.item, action.sort) + ...state, + ...sortedUpdate(state, action.item, action.sort), } case RECEIVE: @@ -110,7 +103,7 @@ export function receive(list, filter = defaultFilter, sort = defaultSort) { function sortedInsert(state, item, sort) { const index = sortedIndex(state.data, item, sort) - const data = [...state.data] + const data = [ ...state.data ] const indexOf = { ...state.indexOf } data.splice(index, 0, item) @@ -134,6 +127,28 @@ function sortedRemove(state, id) { return { data, indexOf } } +function sortedUpdate(state, item, sort) { + let data = [ ...state.data ] + let indexOf = { ...state.indexOf } + let index = indexOf[item.id] + data[index] = item + while (index + 1 < data.length && sort(data[index], data[index + 1]) > 0) { + data[index] = data[index + 1] + data[index + 1] = item + indexOf[item.id] = index + 1 + indexOf[data[index].id] = index + ++index + } + while (index > 0 && sort(data[index], data[index - 1]) < 0) { + data[index] = data[index - 1] + data[index - 1] = item + indexOf[item.id] = index - 1 + indexOf[data[index].id] = index + --index + } + return { data, indexOf } +} + function sortedIndex(list, item, sort) { let low = 0 let high = list.length