[web] fix: Flow update changes list order #36

This commit is contained in:
Jason 2016-07-24 18:22:40 +08:00
parent a3fa9e14ae
commit 3a3305b9ac

View File

@ -57,16 +57,9 @@ export default function reduce(state = defaultState, action) {
if (state.indexOf[action.item.id] == null) { if (state.indexOf[action.item.id] == null) {
return return
} }
const nextState = {
...state,
...sortedRemove(state, action.item.id),
}
if (!action.filter(action.item)) {
return nextState
}
return { return {
...nextState, ...state,
...sortedInsert(nextState, action.item, action.sort) ...sortedUpdate(state, action.item, action.sort),
} }
case RECEIVE: case RECEIVE:
@ -110,7 +103,7 @@ export function receive(list, filter = defaultFilter, sort = defaultSort) {
function sortedInsert(state, item, sort) { function sortedInsert(state, item, sort) {
const index = sortedIndex(state.data, item, sort) const index = sortedIndex(state.data, item, sort)
const data = [...state.data] const data = [ ...state.data ]
const indexOf = { ...state.indexOf } const indexOf = { ...state.indexOf }
data.splice(index, 0, item) data.splice(index, 0, item)
@ -134,6 +127,28 @@ function sortedRemove(state, id) {
return { data, indexOf } 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) { function sortedIndex(list, item, sort) {
let low = 0 let low = 0
let high = list.length let high = list.length