(
state = defaultState,
action: UnknownAction,
)
| 73 | // This is racy: We may see flows/add events for flows that are already in the view, or update/remove events |
| 74 | // for flows that aren't known at all. The reducer needs to handle these cases gracefully. |
| 75 | export default function flowsReducer( |
| 76 | state = defaultState, |
| 77 | action: UnknownAction, |
| 78 | ): FlowsState { |
| 79 | if (FLOWS_RECEIVE.match(action)) { |
| 80 | const { sort } = state; |
| 81 | const list = action.payload; |
| 82 | const _listIndex = buildIndex(list); |
| 83 | const byId = new Map(list.map((f) => [f.id, f])); |
| 84 | // No filter information yet, we expect that to come immediately after. |
| 85 | const view = toSorted(list, makeSort(sort)); |
| 86 | const _viewIndex = buildIndex(view); |
| 87 | const selected = state.selected |
| 88 | .map((flow) => byId.get(flow.id)) |
| 89 | .filter((f) => f !== undefined); |
| 90 | const selectedIds = buildLookup(selected); |
| 91 | const highlightedIds = new Set<string>(); |
| 92 | |
| 93 | return { |
| 94 | list, |
| 95 | _listIndex, |
| 96 | byId, |
| 97 | view, |
| 98 | _viewIndex, |
| 99 | sort, |
| 100 | selected, |
| 101 | selectedIds, |
| 102 | highlightedIds, |
| 103 | }; |
| 104 | } else if (FLOWS_ADD.match(action)) { |
| 105 | const { flow, matching_filters } = action.payload; |
| 106 | if (state._listIndex.has(flow.id)) { |
| 107 | return state; // WebSocket/HTTP race |
| 108 | } |
| 109 | const { sort, selected, selectedIds } = state; |
| 110 | let { view, _viewIndex, highlightedIds } = state; |
| 111 | // Update list |
| 112 | const _listIndex = new Map(state._listIndex); |
| 113 | _listIndex.set(flow.id, state.list.length); |
| 114 | const list = [...state.list, flow]; |
| 115 | const byId = new Map(state.byId); |
| 116 | byId.set(flow.id, flow); |
| 117 | // Update view if filter matches (true) or is unset (undefined). |
| 118 | if ( |
| 119 | matching_filters[FilterName.Search] === true || |
| 120 | matching_filters[FilterName.Search] === undefined |
| 121 | ) { |
| 122 | ({ view, _viewIndex } = insertViewItem( |
| 123 | view, |
| 124 | _viewIndex, |
| 125 | flow, |
| 126 | makeSort(sort), |
| 127 | )); |
| 128 | } |
| 129 | // Update highlight |
| 130 | if (matching_filters[FilterName.Highlight] === true) { |
| 131 | highlightedIds = new Set(highlightedIds); |
| 132 | highlightedIds.add(flow.id); |
nothing calls this directly
no test coverage detected
searching dependent graphs…