diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-06-04 18:53:41 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-06-04 18:53:41 -0700 |
commit | d53a2de0ba69bea6c7aefa87782ad249cfb4ea76 (patch) | |
tree | 25e474bf95e4f83022708e84184d8d7d56a6e3ce /web/src/js/ducks | |
parent | e880f532ad3c66ebfded4655b7fa67a367a83cc7 (diff) | |
download | mitmproxy-d53a2de0ba69bea6c7aefa87782ad249cfb4ea76.tar.gz mitmproxy-d53a2de0ba69bea6c7aefa87782ad249cfb4ea76.tar.bz2 mitmproxy-d53a2de0ba69bea6c7aefa87782ad249cfb4ea76.zip |
web: completely move flow state to redux
Diffstat (limited to 'web/src/js/ducks')
-rw-r--r-- | web/src/js/ducks/eventLog.js | 2 | ||||
-rw-r--r-- | web/src/js/ducks/flows.js | 51 | ||||
-rw-r--r-- | web/src/js/ducks/utils/view.js | 77 |
3 files changed, 110 insertions, 20 deletions
diff --git a/web/src/js/ducks/eventLog.js b/web/src/js/ducks/eventLog.js index e3661fe7..44b67b2c 100644 --- a/web/src/js/ducks/eventLog.js +++ b/web/src/js/ducks/eventLog.js @@ -35,7 +35,7 @@ export default function reducer(state = defaultState, action) { ...state, filter, filteredEvents: updateViewFilter( - state.events.list, + state.events, x => filter[x.level] ) } diff --git a/web/src/js/ducks/flows.js b/web/src/js/ducks/flows.js index fb934489..fdbc42ee 100644 --- a/web/src/js/ducks/flows.js +++ b/web/src/js/ducks/flows.js @@ -1,6 +1,11 @@ import makeList from "./utils/list" +import Filt from "../filt/filt" +import {updateViewFilter, updateViewList} from "./utils/view" export const UPDATE_FLOWS = "UPDATE_FLOWS" +export const SET_FILTER = "SET_FLOW_FILTER" +export const SET_HIGHLIGHT = "SET_FLOW_HIGHLIGHT" +export const SELECT_FLOW = "SELECT_FLOW" const { reduceList, @@ -11,6 +16,14 @@ const { const defaultState = { all: reduceList(), + selected: [], + view: [], + filter: undefined, + highlight: undefined, +} + +function makeFilterFn(filter) { + return filter ? Filt.parse(filter) : () => true; } export default function reducer(state = defaultState, action) { @@ -20,10 +33,48 @@ export default function reducer(state = defaultState, action) { return { ...state, all, + view: updateViewList(state.view, state.all, all, action, makeFilterFn(action.filter)) + } + case SET_FILTER: + return { + ...state, + filter: action.filter, + view: updateViewFilter(state.all, makeFilterFn(action.filter)) + } + case SET_HIGHLIGHT: + return { + ...state, + highlight: action.highlight + } + case SELECT_FLOW: + return { + ...state, + selected: [action.flowId] } default: return state } } + +export function setFilter(filter) { + return { + type: SET_FILTER, + filter + } +} +export function setHighlight(highlight) { + return { + type: SET_HIGHLIGHT, + highlight + } +} +export function selectFlow(flowId) { + return { + type: SELECT_FLOW, + flowId + } +} + + export {updateList as updateFlows, fetchList as fetchFlows}
\ No newline at end of file diff --git a/web/src/js/ducks/utils/view.js b/web/src/js/ducks/utils/view.js index 55fdf6c7..5535ed83 100644 --- a/web/src/js/ducks/utils/view.js +++ b/web/src/js/ducks/utils/view.js @@ -15,13 +15,15 @@ const makeCompareFn = sortFn => { return 0 } } - if (sortFn.reverse) - return (a, b) => compareFn(b, a) + // need to adjust sortedIndexOf as well + // if (sortFn.reverse) + // return (a, b) => compareFn(b, a) return compareFn } const sortedInsert = (list, sortFn, item) => { let l = [...list, item] + l.indexOf = x => sortedIndexOf(l, x, sortFn) let compareFn = makeCompareFn(sortFn) // only sort if sorting order is not correct yet @@ -35,21 +37,54 @@ const sortedInsert = (list, sortFn, item) => { const sortedRemove = (list, sortFn, item) => { let itemId = item.id - return list.filter(x => x.id !== itemId) + let l = list.filter(x => x.id !== itemId) + l.indexOf = x => sortedIndexOf(l, x, sortFn) + return l +} + +export function sortedIndexOf(list, value, sortFn) { + if (sortFn === false){ + let i = 0 + while (i < list.length && list[i].id !== value.id){ + i++ + } + return i + } + + let low = 0, + high = list.length, + val = sortFn(value), + mid; + while (low < high) { + mid = (low + high) >>> 1; + if ((sortFn(list[mid]) < val) ) { + low = mid + 1 + } else { + high = mid + } + } + + // Two flows may have the same sort value. + // we previously determined the leftmost flow with the same sort value, + // so no we need to scan linearly + while (list[low].id !== value.id && sortFn(list[low + 1]) === val) { + low++ + } + return low; } // for when the list changes -export function updateViewList(state, currentList, nextList, action, filterFn = defaultFilterFn, sortFn = defaultSortFn) { +export function updateViewList(currentView, currentList, nextList, action, filterFn = defaultFilterFn, sortFn = defaultSortFn) { switch (action.cmd) { case REQUEST_LIST: - return state + return currentView case RECEIVE_LIST: - return updateViewFilter(nextList.list, filterFn, sortFn) + return updateViewFilter(nextList, filterFn, sortFn) case ADD: if (filterFn(action.item)) { - return sortedInsert(state, sortFn, action.item) + return sortedInsert(currentView, sortFn, action.item) } - return state + return currentView case UPDATE: // let's determine if it's in the view currently and if it should be in the view. let currentItemState = currentList.byId[action.item.id], @@ -58,30 +93,34 @@ export function updateViewList(state, currentList, nextList, action, filterFn = shouldBeInView = filterFn(nextItemState) if (!isInView && shouldBeInView) - return sortedInsert(state, sortFn, action.item) + return sortedInsert(currentView, sortFn, action.item) if (isInView && !shouldBeInView) - return sortedRemove(state, sortFn, action.item) - if (isInView && shouldBeInView && sortFn(currentItemState) !== sortFn(nextItemState)) { - let s = [...state] - s.sort(sortFn) + return sortedRemove(currentView, sortFn, action.item) + if (isInView && shouldBeInView && sortFn && sortFn(currentItemState) !== sortFn(nextItemState)) { + let s = [...currentView] + s.sort(makeCompareFn(sortFn)) + s.indexOf = x => sortedIndexOf(s, x, sortFn) return s } - return state + return currentView case REMOVE: let isInView_ = filterFn(currentList.byId[action.item.id]) if (isInView_) { - return sortedRemove(state, sortFn, action.item) + return sortedRemove(currentView, sortFn, action.item) } - return state + return currentView default: console.error("Unknown list action: ", action) - return state + return currentView } } export function updateViewFilter(list, filterFn = defaultFilterFn, sortFn = defaultSortFn) { - let filtered = list.filter(filterFn) - if (sortFn) + let filtered = list.list.filter(filterFn) + if (sortFn){ filtered.sort(makeCompareFn(sortFn)) + } + filtered.indexOf = x => sortedIndexOf(filtered, x, sortFn) + return filtered }
\ No newline at end of file |