Skip to content

Commit 66058d0

Browse files
committed
Update syntax for ember-concurrency tasks; convert search service to native class
1 parent aa651d3 commit 66058d0

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

app/components/search-input.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { inject as service } from '@ember/service';
22
import Component from '@glimmer/component';
33
import { get } from '@ember/object';
44
import { isPresent } from '@ember/utils';
5-
import { task, timeout } from 'ember-concurrency';
5+
import { restartableTask, task, timeout } from 'ember-concurrency';
66
import { action } from '@ember/object';
77
import { tracked } from '@glimmer/tracking';
88

@@ -32,8 +32,8 @@ export default class SearchInput extends Component {
3232
return isPresent(this.query);
3333
}
3434

35-
@task({ restartable: true }) *search(query) {
36-
yield timeout(SEARCH_DEBOUNCE_PERIOD);
35+
search = restartableTask(async (query) => {
36+
await timeout(SEARCH_DEBOUNCE_PERIOD);
3737

3838
this.query = query;
3939

@@ -46,14 +46,14 @@ export default class SearchInput extends Component {
4646
// ensure search results are visible if the menu was previously closed above
4747
this._focused = true;
4848

49-
yield get(this, 'searchService.search').perform(query);
50-
}
49+
await get(this, 'searchService.search').perform(query);
50+
});
5151

52-
@task *closeMenu() {
53-
yield timeout(SEARCH_CLOSE_PERIOD);
52+
closeMenu = task(async () => {
53+
await timeout(SEARCH_CLOSE_PERIOD);
5454

5555
this._focused = false;
56-
}
56+
});
5757

5858
@action onfocus() {
5959
if (this.query.length > 0 && this.searchService.hasStaleResults()) {

app/services/search.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import Service, { inject as service } from '@ember/service';
2-
import { task } from 'ember-concurrency';
2+
import { restartableTask } from 'ember-concurrency';
33
import { set } from '@ember/object';
44
import { A as emberArray } from '@ember/array';
5+
// eslint-disable-next-line ember/no-computed-properties-in-native-classes
56
import { alias } from '@ember/object/computed';
67

7-
export default Service.extend({
8-
_algoliaService: service('algolia'),
9-
_projectService: service('project'),
10-
_projectVersion: alias('_projectService.version'),
8+
export default class SearchService extends Service {
9+
@service('algolia') _algoliaService;
10+
@service('project') _projectService;
11+
12+
@alias('_projectService.version') _projectVersion;
1113

1214
/** @type {?string} */
13-
_lastQueriedProjectVersion: null,
15+
_lastQueriedProjectVersion = null;
1416

15-
results: emberArray(),
17+
results = emberArray();
1618

17-
search: task(function* (query) {
19+
search = restartableTask(async (query) => {
1820
const projectVersion = this._projectVersion;
1921

2022
const params = {
@@ -35,14 +37,14 @@ export default Service.extend({
3537

3638
this._lastQueriedProjectVersion = projectVersion;
3739

38-
return set(this, 'results', yield this.doSearch(searchObj, params));
39-
}).restartable(),
40+
return set(this, 'results', await this.doSearch(searchObj, params));
41+
});
4042

4143
doSearch(searchObj, params) {
4244
return this._algoliaService
4345
.search(searchObj, params)
4446
.then((results) => results.hits);
45-
},
47+
}
4648

4749
/**
4850
* Whenever the version changes in service:project, the results in this
@@ -55,9 +57,9 @@ export default Service.extend({
5557
this._lastQueriedProjectVersion !== null &&
5658
this._projectVersion !== this._lastQueriedProjectVersion
5759
);
58-
},
60+
}
5961

6062
clearResults() {
6163
set(this, 'results', emberArray());
62-
},
63-
});
64+
}
65+
}

ember-cli-build.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ module.exports = function (defaults) {
3030
},
3131
'ember-cli-babel': {
3232
includePolyfill: true,
33-
}
33+
},
34+
babel: {
35+
plugins: [
36+
// ... any other plugins
37+
require.resolve('ember-concurrency/async-arrow-task-transform'),
38+
39+
// NOTE: put any code coverage plugins last, after the transform.
40+
],
41+
},
3442
});
3543

3644
const { Webpack } = require('@embroider/webpack');

0 commit comments

Comments
 (0)