2024-04-10 14:44:24 +03:00
|
|
|
import Service from '@ember/service';
|
2024-05-07 17:24:20 +03:00
|
|
|
import {action} from '@ember/object';
|
2024-06-11 18:18:28 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
2024-04-10 14:44:24 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2024-05-07 00:04:13 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2024-04-10 14:44:24 +03:00
|
|
|
|
|
|
|
export default class SearchService extends Service {
|
|
|
|
@service ajax;
|
2024-06-11 18:18:28 +03:00
|
|
|
@service feature;
|
2024-04-10 14:44:24 +03:00
|
|
|
@service notifications;
|
2024-08-12 13:17:38 +03:00
|
|
|
@service searchProviderBasic;
|
|
|
|
@service searchProviderFlex;
|
|
|
|
@service settings;
|
2024-04-10 14:44:24 +03:00
|
|
|
@service store;
|
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
isContentStale = true;
|
2024-04-10 14:44:24 +03:00
|
|
|
|
2024-06-11 18:18:28 +03:00
|
|
|
get provider() {
|
2024-08-12 13:17:38 +03:00
|
|
|
const isEnglish = this.settings.locale?.toLowerCase().startsWith('en') ?? true;
|
|
|
|
return isEnglish ? this.searchProviderFlex : this.searchProviderBasic;
|
2024-06-11 18:18:28 +03:00
|
|
|
}
|
2024-04-10 14:44:24 +03:00
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
@action
|
|
|
|
expireContent() {
|
|
|
|
this.isContentStale = true;
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:44:24 +03:00
|
|
|
@task({restartable: true})
|
|
|
|
*searchTask(term) {
|
|
|
|
if (isBlank(term)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// start loading immediately in the background
|
|
|
|
this.refreshContentTask.perform();
|
|
|
|
|
|
|
|
// debounce searches to 200ms to avoid thrashing CPU
|
|
|
|
yield timeout(200);
|
|
|
|
|
|
|
|
// wait for any on-going refresh to finish
|
|
|
|
if (this.refreshContentTask.isRunning) {
|
2024-05-07 00:04:13 +03:00
|
|
|
yield this.refreshContentTask.lastRunning;
|
2024-04-10 14:44:24 +03:00
|
|
|
}
|
|
|
|
|
2024-06-11 18:18:28 +03:00
|
|
|
return yield this.provider.searchTask.perform(term);
|
2024-04-10 14:44:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@task({drop: true})
|
2024-05-07 17:24:20 +03:00
|
|
|
*refreshContentTask({forceRefresh = false} = {}) {
|
|
|
|
if (!forceRefresh && !this.isContentStale) {
|
2024-04-10 14:44:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
this.isContentStale = true;
|
|
|
|
|
2024-06-11 18:18:28 +03:00
|
|
|
yield this.provider.refreshContentTask.perform();
|
2024-04-10 14:44:24 +03:00
|
|
|
|
2024-05-07 17:24:20 +03:00
|
|
|
this.isContentStale = false;
|
2024-04-10 14:44:24 +03:00
|
|
|
}
|
|
|
|
}
|