Bug 1517572 - [release 116] Add a way to stop an ongoing project search (#7621). r=dwalsh

This commit is contained in:
Gary Blackwood 2019-01-03 15:11:51 -05:00 committed by Jason Laster
parent c7da8412df
commit 92ff36d425
2 changed files with 60 additions and 5 deletions

View File

@ -13,14 +13,23 @@ import { findSourceMatches } from "../workers/search";
import { getSource, hasPrettySource, getSourceList } from "../selectors";
import { isThirdParty } from "../utils/source";
import { loadSourceText } from "./sources/loadSourceText";
import { statusType } from "../reducers/project-text-search";
import {
statusType,
getTextSearchOperation,
getTextSearchStatus
} from "../reducers/project-text-search";
import type { Action, ThunkArgs } from "./types";
import type { SearchOperation } from "../reducers/project-text-search";
export function addSearchQuery(query: string): Action {
return { type: "ADD_QUERY", query };
}
export function addOngoingSearch(ongoingSearch: SearchOperation): Action {
return { type: "ADD_ONGOING_SEARCH", ongoingSearch };
}
export function clearSearchQuery(): Action {
return { type: "CLEAR_QUERY" };
}
@ -48,12 +57,31 @@ export function updateSearchStatus(status: string): Action {
return { type: "UPDATE_STATUS", status };
}
export function closeProjectSearch(): Action {
return { type: "CLOSE_PROJECT_SEARCH" };
export function closeProjectSearch() {
return ({ dispatch, getState }: ThunkArgs) => {
dispatch(stopOngoingSearch());
dispatch({ type: "CLOSE_PROJECT_SEARCH" });
};
}
export function stopOngoingSearch() {
return ({ dispatch, getState }: ThunkArgs) => {
const state = getState();
const ongoingSearch = getTextSearchOperation(state);
const status = getTextSearchStatus(state);
if (ongoingSearch && status !== statusType.done) {
ongoingSearch.cancel();
dispatch(updateSearchStatus(statusType.cancelled));
}
};
}
export function searchSources(query: string) {
return async ({ dispatch, getState }: ThunkArgs) => {
let cancelled = false;
const search = async ({ dispatch, getState }: ThunkArgs) => {
dispatch(stopOngoingSearch());
await dispatch(addOngoingSearch(search));
await dispatch(clearSearchResults());
await dispatch(addSearchQuery(query));
dispatch(updateSearchStatus(statusType.fetching));
@ -61,11 +89,20 @@ export function searchSources(query: string) {
source => !hasPrettySource(getState(), source.id) && !isThirdParty(source)
);
for (const source of validSources) {
if (cancelled) {
return;
}
await dispatch(loadSourceText(source));
await dispatch(searchSource(source.id, query));
}
dispatch(updateSearchStatus(statusType.done));
};
search.cancel = () => {
cancelled = true;
};
return search;
}
export function searchSource(sourceId: string, query: string) {

View File

@ -11,16 +11,26 @@
*/
import type { Action } from "../actions/types";
import type { Cancellable } from "../types";
export type Search = {
+sourceId: string,
+filepath: string,
+matches: any[]
};
export type StatusType = "INITIAL" | "FETCHING" | "DONE" | "ERROR";
export type SearchOperation = Cancellable;
export type StatusType =
| "INITIAL"
| "FETCHING"
| "CANCELLED"
| "DONE"
| "ERROR";
export const statusType = {
initial: "INITIAL",
fetching: "FETCHING",
cancelled: "CANCELLED",
done: "DONE",
error: "ERROR"
};
@ -28,6 +38,7 @@ export const statusType = {
export type ResultList = Search[];
export type ProjectTextSearchState = {
+query: string,
+ongoingSearch?: SearchOperation,
+results: ResultList,
+status: string
};
@ -74,6 +85,9 @@ function update(
case "CLEAR_SEARCH_RESULTS":
return { ...state, results: [] };
case "ADD_ONGOING_SEARCH":
return { ...state, ongoingSearch: action.ongoingSearch };
case "CLEAR_SEARCH":
case "CLOSE_PROJECT_SEARCH":
case "NAVIGATE":
@ -84,6 +98,10 @@ function update(
type OuterState = { projectTextSearch: ProjectTextSearchState };
export function getTextSearchOperation(state: OuterState) {
return state.projectTextSearch.ongoingSearch;
}
export function getTextSearchResults(state: OuterState) {
return state.projectTextSearch.results;
}