1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
/**
* Redux actions for the search state
* @module actions/search
*/
import { isFulfilled } from "../utils/async-value";
import {
getFirstSourceActorForGeneratedSource,
hasPrettySource,
getSourceList,
getSettledSourceTextContent,
} from "../selectors";
import { isThirdParty } from "../utils/source";
import { createLocation } from "../utils/location";
import { loadSourceText } from "./sources/loadSourceText";
import {
getTextSearchOperation,
getTextSearchStatus,
} from "../selectors/project-text-search";
import { statusType } from "../reducers/project-text-search";
export function addSearchQuery(cx, query) {
return { type: "ADD_QUERY", cx, query };
}
export function addOngoingSearch(cx, ongoingSearch) {
return { type: "ADD_ONGOING_SEARCH", cx, ongoingSearch };
}
export function addSearchResult(cx, sourceId, filepath, matches) {
return {
type: "ADD_SEARCH_RESULT",
cx,
result: { sourceId, filepath, matches },
};
}
export function clearSearchResults(cx) {
return { type: "CLEAR_SEARCH_RESULTS", cx };
}
export function clearSearch(cx) {
return { type: "CLEAR_SEARCH", cx };
}
export function updateSearchStatus(cx, status) {
return { type: "UPDATE_STATUS", cx, status };
}
export function closeProjectSearch(cx) {
return ({ dispatch, getState }) => {
dispatch(stopOngoingSearch(cx));
dispatch({ type: "CLOSE_PROJECT_SEARCH" });
};
}
export function stopOngoingSearch(cx) {
return ({ dispatch, getState }) => {
const state = getState();
const ongoingSearch = getTextSearchOperation(state);
const status = getTextSearchStatus(state);
if (ongoingSearch && status !== statusType.done) {
ongoingSearch.cancel();
dispatch(updateSearchStatus(cx, statusType.cancelled));
}
};
}
export function searchSources(cx, query) {
let cancelled = false;
const search = async ({ dispatch, getState }) => {
dispatch(stopOngoingSearch(cx));
await dispatch(addOngoingSearch(cx, search));
await dispatch(clearSearchResults(cx));
await dispatch(addSearchQuery(cx, query));
dispatch(updateSearchStatus(cx, statusType.fetching));
let validSources = getSourceList(getState()).filter(
source => !hasPrettySource(getState(), source.id) && !isThirdParty(source)
);
// Sort original entries first so that search results are more useful.
// See bug 1642778.
validSources = [
...validSources.filter(x => x.isOriginal),
...validSources.filter(x => !x.isOriginal),
];
for (const source of validSources) {
if (cancelled) {
return;
}
const sourceActor = getFirstSourceActorForGeneratedSource(
getState(),
source.id
);
await dispatch(loadSourceText(cx, source, sourceActor));
await dispatch(searchSource(cx, source, sourceActor, query));
}
dispatch(updateSearchStatus(cx, statusType.done));
};
search.cancel = () => {
cancelled = true;
};
return search;
}
export function searchSource(cx, source, sourceActor, query) {
return async ({ dispatch, getState, searchWorker }) => {
if (!source) {
return;
}
const location = createLocation({
sourceId: source.id,
sourceActorId: sourceActor ? sourceActor.actor : null,
});
const content = getSettledSourceTextContent(getState(), location);
let matches = [];
if (content && isFulfilled(content) && content.value.type === "text") {
matches = await searchWorker.findSourceMatches(
source.id,
content.value,
query
);
}
if (!matches.length) {
return;
}
dispatch(addSearchResult(cx, source.id, source.url, matches));
};
}
|