Ghost/ghost/admin/app/services/search.js
Kevin Ansfield 83ee649e82 Added improved search into internal linking beta
closes https://linear.app/tryghost/issue/MOM-117
closes https://linear.app/tryghost/issue/MOM-70

- updated flag handling to move the improved search into the `internalLinking` beta flag
- removed now-unused `internalLinkingSearchImprovements` flag
2024-06-13 17:30:15 +01:00

61 lines
1.5 KiB
JavaScript

import Service from '@ember/service';
import {action} from '@ember/object';
import {isBlank} from '@ember/utils';
import {inject as service} from '@ember/service';
import {task, timeout} from 'ember-concurrency';
export default class SearchService extends Service {
@service ajax;
@service feature;
@service notifications;
@service searchProvider;
@service searchProviderBeta;
@service store;
isContentStale = true;
get provider() {
return this.feature.internalLinking
? this.searchProviderBeta
: this.searchProvider;
}
@action
expireContent() {
this.isContentStale = true;
}
@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) {
yield this.refreshContentTask.lastRunning;
}
return yield this.provider.searchTask.perform(term);
}
@task({drop: true})
*refreshContentTask({forceRefresh = false} = {}) {
if (!forceRefresh && !this.isContentStale) {
return true;
}
this.isContentStale = true;
yield this.provider.refreshContentTask.perform();
this.isContentStale = false;
}
}