Skip to content

Commit 3b29ad3

Browse files
committed
refactor: simplify Searches service
- Trigger search directly, not relying on $location update - Move getCqpExpr() to $rootScope.getActiveCqp() - Await init defs only for initial search
1 parent 291aab8 commit 3b29ad3

File tree

12 files changed

+130
-133
lines changed

12 files changed

+130
-133
lines changed

app/scripts/app.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
IComponentOptions,
77
ILocaleService,
88
ILocationProvider,
9-
IQService,
109
IScope,
1110
ITimeoutService,
1211
ui,
@@ -18,7 +17,7 @@ import * as authenticationProxy from "@/components/auth/auth"
1817
import { initLocales } from "@/data_init"
1918
import { RootScope } from "@/root-scope.types"
2019
import { CorpusTransformed } from "./settings/config-transformed.types"
21-
import { getService, html } from "@/util"
20+
import { deferOk, getService, html } from "@/util"
2221
import { loc, locObj } from "@/i18n"
2322
import "@/components/app-header"
2423
import "@/components/searchtabs"
@@ -121,7 +120,6 @@ korpApp.run([
121120
"$locale",
122121
"tmhDynamicLocale",
123122
"tmhDynamicLocaleCache",
124-
"$q",
125123
"$timeout",
126124
"$uibModal",
127125
async function (
@@ -130,18 +128,25 @@ korpApp.run([
130128
$locale: ILocaleService,
131129
tmhDynamicLocale: tmh.tmh.IDynamicLocale,
132130
tmhDynamicLocaleCache: ICacheObject,
133-
$q: IQService,
134131
$timeout: ITimeoutService,
135132
$uibModal: ui.bootstrap.IModalService
136133
) {
137134
const s = $rootScope
138135

139136
s.extendedCQP = null
140137
s.globalFilterData = {}
141-
s.globalFilterDef = $q.defer<never>()
142-
s.langDef = $q.defer<never>()
138+
$rootScope.globalFilterDef = deferOk()
139+
$rootScope.langDef = deferOk()
143140
$rootScope.wordpicSortProp = "freq"
144141

142+
/** Get CQP corresponding to the current search, if any. */
143+
$rootScope.getActiveCqp = () => {
144+
if (!$rootScope.activeSearch) return undefined
145+
// Simple search puts CQP in `simpleCQP`. Extended/advanced puts it in `activeSearch.val`.
146+
const isSimple = ["word", "lemgram"].includes($rootScope.activeSearch.type)
147+
return isSimple ? $rootScope.simpleCQP : $rootScope.activeSearch.val
148+
}
149+
145150
// Listen to url changes like #?lang=swe
146151
s.$on("$locationChangeSuccess", () => {
147152
// Update current locale. This is async and triggers the "$localeChangeSuccess" event.

app/scripts/components/advanced-search.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "@/services/compare-searches"
66
import "@/components/search-submit"
77
import { LocationService } from "@/urlparams"
88
import { CompareSearches } from "@/services/compare-searches"
9+
import { SearchesService } from "@/services/searches"
910

1011
type AdvancedSearchController = IController & {
1112
cqp: string
@@ -62,15 +63,15 @@ angular.module("korpApp").component("advancedSearch", {
6263
</div>`,
6364
bindings: {},
6465
controller: [
65-
"compareSearches",
6666
"$location",
6767
"$scope",
68-
"$timeout",
68+
"compareSearches",
69+
"searches",
6970
function (
70-
compareSearches: CompareSearches,
7171
$location: LocationService,
7272
$scope: AdvancedSearchScope,
73-
$timeout: ITimeoutService
73+
compareSearches: CompareSearches,
74+
searches: SearchesService
7475
) {
7576
const $ctrl = this as AdvancedSearchController
7677

@@ -92,12 +93,12 @@ angular.module("korpApp").component("advancedSearch", {
9293
)
9394

9495
$ctrl.onSearch = () => {
95-
$location.search("search", null)
9696
$location.search("page", null)
9797
$location.search("within", null)
9898
$location.search("in_order", $ctrl.freeOrder ? false : null)
99-
$timeout(() => $location.search("search", `cqp|${$ctrl.cqp}`), 0)
99+
$location.search("search", `cqp|${$ctrl.cqp}`)
100100
matomoSend("trackEvent", "Search", "Submit search", "Advanced")
101+
searches.doSearch()
101102
}
102103

103104
$ctrl.onSearchSave = (name) => {

app/scripts/components/extended/extended-parallel.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import "@/components/extended/tokens"
99
import { ParallelCorpusListing } from "@/parallel/corpus_listing"
1010
import { LocationService } from "@/urlparams"
1111
import { RootScope } from "@/root-scope.types"
12+
import { SearchesService } from "@/services/searches"
1213

1314
type ExtendedParallelController = IController & {
1415
langs: { lang: string; cqp: string }[]
1516
negates: boolean[]
1617
initialized: boolean
1718
cqpChange: (idx: number) => (cqp: string) => void
18-
negChange: () => void
1919
onLangChange: () => void
2020
getEnabledLangs: (i?: number) => string[]
2121
addLangRow: () => void
@@ -39,13 +39,7 @@ angular.module("korpApp").component("extendedParallel", {
3939
for="negate_chk{{$index}}"
4040
>{{"not_containing" | loc:$root.lang}}</label
4141
>
42-
<input
43-
type="checkbox"
44-
id="negate_chk{{$index}}"
45-
ng-show="!$first"
46-
ng-model="$ctrl.negates[$index]"
47-
ng-change="$ctrl.negChange()"
48-
/>
42+
<input type="checkbox" id="negate_chk{{$index}}" ng-show="!$first" ng-model="$ctrl.negates[$index]" />
4943
<extended-tokens
5044
cqp="l.cqp"
5145
cqp-change="$ctrl.cqpChange($index)(cqp)"
@@ -78,7 +72,13 @@ angular.module("korpApp").component("extendedParallel", {
7872
"$location",
7973
"$rootScope",
8074
"$timeout",
81-
function ($location: LocationService, $rootScope: RootScope, $timeout: ITimeoutService) {
75+
"searches",
76+
function (
77+
$location: LocationService,
78+
$rootScope: RootScope,
79+
$timeout: ITimeoutService,
80+
searches: SearchesService
81+
) {
8282
const ctrl = this as ExtendedParallelController
8383

8484
const corpusListing = settings.corpusListing as ParallelCorpusListing
@@ -111,10 +111,6 @@ angular.module("korpApp").component("extendedParallel", {
111111
}
112112
}
113113

114-
ctrl.negChange = function () {
115-
$location.search("search", null)
116-
}
117-
118114
const onCQPChange = () => {
119115
const currentLangList = _.map(ctrl.langs, "lang")
120116
var struct = corpusListing.getLinksFromLangs(currentLangList)
@@ -163,14 +159,11 @@ angular.module("korpApp").component("extendedParallel", {
163159
}
164160

165161
ctrl.onSubmit = function () {
166-
// Unset and set query in next time step in order to trigger changes correctly in `searches`.
167-
$location.search("search", null)
168162
$location.replace()
169-
$timeout(function () {
170-
$location.search("search", `cqp|${onCQPChange()}`)
171-
$location.search("page", null)
172-
}, 0)
163+
$location.search("search", `cqp|${onCQPChange()}`)
164+
$location.search("page", null)
173165
matomoSend("trackEvent", "Search", "Submit search", "Extended")
166+
searches.doSearch()
174167
}
175168

176169
ctrl.keydown = function ($event: KeyboardEvent) {

app/scripts/components/extended/extended-standard.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import "@/global-filter/global-filters"
1313
import { LocationService } from "@/urlparams"
1414
import { RootScope } from "@/root-scope.types"
1515
import { CompareSearches } from "@/services/compare-searches"
16+
import { SearchesService } from "@/services/searches"
1617

1718
type ExtendedStandardController = IController & {
1819
cqp: string
@@ -72,14 +73,16 @@ angular.module("korpApp").component("extendedStandard", {
7273
"$location",
7374
"$rootScope",
7475
"$scope",
75-
"compareSearches",
7676
"$timeout",
77+
"compareSearches",
78+
"searches",
7779
function (
7880
$location: LocationService,
7981
$rootScope: RootScope,
8082
$scope: ExtendedStandardScope,
83+
$timeout: ITimeoutService,
8184
compareSearches: CompareSearches,
82-
$timeout: ITimeoutService
85+
searches: SearchesService
8386
) {
8487
const ctrl = this as ExtendedStandardController
8588

@@ -91,14 +94,11 @@ angular.module("korpApp").component("extendedStandard", {
9194

9295
// TODO this is *too* weird
9396
function triggerSearch() {
94-
// Unset and set query in next time step in order to trigger changes correctly in `searches`.
95-
$location.search("search", null)
9697
$location.search("page", null)
9798
$location.search("in_order", $scope.freeOrder ? false : null)
9899
$location.search("within", ctrl.within != defaultWithin ? ctrl.within : undefined)
99-
$timeout(function () {
100-
$location.search("search", "cqp")
101-
}, 0)
100+
$location.search("search", "cqp")
101+
searches.doSearch()
102102
}
103103

104104
statemachine.listen("cqp_search", (event) => {

app/scripts/components/search-examples.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import settings from "@/settings"
77
import { SearchExample } from "@/settings/app-settings.types"
88
import { HashParams, LocationService } from "@/urlparams"
99
import { CqpSearchEvent } from "@/statemachine/types"
10+
import { SearchesService } from "@/services/searches"
1011

1112
export default angular.module("korpApp").component("searchExamples", {
1213
template: html`
@@ -26,7 +27,8 @@ export default angular.module("korpApp").component("searchExamples", {
2627
controller: [
2728
"$scope",
2829
"$location",
29-
function ($scope: SearchExamplesScope, $location: LocationService) {
30+
"searches",
31+
function ($scope: SearchExamplesScope, $location: LocationService, searches: SearchesService) {
3032
const $ctrl = this
3133

3234
$scope.examples = undefined
@@ -46,6 +48,7 @@ export default angular.module("korpApp").component("searchExamples", {
4648
}
4749
// Do not use `$location.search(params)` because it will remove existing params (like `corpus`)
4850
Object.keys(params).forEach((key: keyof HashParams) => $location.search(key, params[key]))
51+
searches.doSearch()
4952
}
5053
},
5154
],

app/scripts/components/search-history.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ angular.module("korpApp").component("searchHistory", {
7474
const params = $scope.value.params
7575
getSearchParamNames().forEach((key) => $location.search(key, params[key] ?? null))
7676

77-
// The Searches watcher stupidly only watches the `search` param, so trigger it explicitly
78-
searches.triggerSearch()
77+
// Wait for param changes like corpus selection to propagate to app state
78+
$scope.$applyAsync(() => searches.doSearch())
7979
} else if ($scope.value.id == "_clear") {
8080
searchHistory.clear()
8181
resetValue()

app/scripts/components/simple-search.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,32 @@ angular.module("korpApp").component("simpleSearch", {
152152
$location.search("isCaseInsensitive", "")
153153
}
154154

155-
// triggers watch on activeSearch, via the Searches service
155+
// React to changes in URL params
156+
function readSearchParams() {
157+
const search = $location.search()
158+
ctrl.freeOrder = search.in_order != null
159+
ctrl.prefix = search.prefix != null
160+
ctrl.mid_comp = search.mid_comp != null
161+
ctrl.suffix = search.suffix != null
162+
ctrl.isCaseInsensitive = search.isCaseInsensitive != null
163+
}
164+
readSearchParams()
165+
156166
ctrl.updateSearch = function () {
157167
$location.search("in_order", ctrl.freeOrder && ctrl.freeOrderEnabled ? false : null)
158168
$location.search("prefix", ctrl.prefix ? true : null)
159169
$location.search("mid_comp", ctrl.mid_comp ? true : null)
160170
$location.search("suffix", ctrl.suffix ? true : null)
161171
$location.search("isCaseInsensitive", ctrl.isCaseInsensitive ? true : null)
162172
$location.search("within", null)
163-
164-
// Unset and set query in next time step in order to trigger changes correctly in the Searches service.
165-
$location.search("search", null)
166173
$location.replace()
167-
$timeout(function () {
168-
if (ctrl.currentText) {
169-
$location.search("search", `word|${ctrl.currentText}`)
170-
} else if (ctrl.lemgram) {
171-
$location.search("search", `lemgram|${ctrl.lemgram}`)
172-
}
173-
$location.search("page", null)
174-
}, 0)
174+
175+
if (ctrl.currentText) $location.search("search", `word|${ctrl.currentText}`)
176+
else if (ctrl.lemgram) $location.search("search", `lemgram|${ctrl.lemgram}`)
177+
$location.search("page", null)
178+
175179
matomoSend("trackEvent", "Search", "Submit search", "Simple")
180+
searches.doSearch()
176181
}
177182

178183
ctrl.getCQP = function () {
@@ -286,14 +291,7 @@ angular.module("korpApp").component("simpleSearch", {
286291
})
287292

288293
// React to changes in URL params
289-
$scope.$on("$locationChangeSuccess", () => {
290-
const search = $location.search()
291-
ctrl.freeOrder = search.in_order != null
292-
ctrl.prefix = search.prefix != null
293-
ctrl.mid_comp = search.mid_comp != null
294-
ctrl.suffix = search.suffix != null
295-
ctrl.isCaseInsensitive = search.isCaseInsensitive != null
296-
})
294+
$scope.$on("$locationChangeSuccess", () => readSearchParams())
297295

298296
ctrl.onChange = (value, isPlain) => {
299297
ctrl.currentText = isPlain ? value : undefined

app/scripts/components/statistics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ angular.module("korpApp").component("statistics", {
445445
$ctrl.noRowsError = false
446446

447447
// TODO this is wrong, it should use the previous search
448-
let cqp = searches.getCqpExpr()
448+
let cqp = $rootScope.getActiveCqp()!
449449
try {
450450
cqp = expandOperators(cqp)
451451
} catch {}

app/scripts/controllers/statistics_controller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ angular.module("korpApp").directive("statsResultCtrl", () => ({
166166
} else {
167167
$location.search("show_stats", true)
168168
}
169-
const cqp = searches.getCqpExpr()
170-
s.showStatistics = true
171-
s.makeRequest(cqp)
169+
const cqp = $rootScope.getActiveCqp()
170+
if (cqp) {
171+
s.showStatistics = true
172+
s.makeRequest(cqp)
173+
}
172174
}
173175
},
174176
],

app/scripts/root-scope.types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { CorpusListing } from "./corpus_listing"
77
import { CompareResult, MapRequestResult } from "@/backend/backend"
88
import { RelationsParams } from "@/backend/types/relations"
99
import { Attribute } from "./settings/config.types"
10+
import { DeferredOk } from "./util"
1011

1112
/** Extends the Angular Root Scope interface with properties used by this app. */
1213
export type RootScope = IRootScopeService & {
@@ -16,13 +17,14 @@ export type RootScope = IRootScopeService & {
1617
val: string
1718
} | null
1819
extendedCQP: string | null
20+
getActiveCqp(): string | undefined
1921
/** Filter data by attribute name */
2022
globalFilterData: Record<string, FilterData>
2123
globalFilter: CqpQuery | null
2224
/** This deferred is used to signal that the filter feature is ready. */
23-
globalFilterDef: IDeferred<never>
25+
globalFilterDef: DeferredOk
2426
/** This deferred is resolved when parallel search controller is loaded */
25-
langDef: IDeferred<never>
27+
langDef: DeferredOk
2628
simpleCQP?: string
2729
show_modal: "about" | false
2830
compareTabs: CompareTab[]

0 commit comments

Comments
 (0)