forked from machty/ember-concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,243 additions
and
747 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,4 @@ | ||
{ | ||
/** | ||
Ember CLI sends analytics information by default. The data is completely | ||
anonymous, but there are times when you might want to disable this behavior. | ||
|
||
Setting `disableAnalytics` to true will prevent any data from being sent. | ||
*/ | ||
"disableAnalytics": false | ||
"disableAnalytics": false, | ||
"open-browser-uri": "http://localhost:4200/docs/examples/autocomplete" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,34 @@ | ||
import $ from 'jquery'; | ||
import { isBlank } from '@ember/utils'; | ||
import Controller from '@ember/controller'; | ||
import { task, timeout } from 'ember-concurrency'; | ||
import { readOnly } from '@ember/object/computed'; | ||
import { task } from 'ember-concurrency'; | ||
import { computed } from '@ember/object'; | ||
|
||
// BEGIN-SNIPPET debounced-search-with-cancelation | ||
const DEBOUNCE_MS = 250; | ||
export default Controller.extend({ | ||
lastSuccessful: readOnly('searchRepo.lastSuccessful.value'), | ||
|
||
// // Uncomment this block and bug will not be present | ||
// lastSuccessful: computed('searchRepo.lastSuccessful.value', function() { | ||
// return this.get('searchRepo.lastSuccessful.value'); | ||
// }).readOnly(), | ||
|
||
hasResults: computed('lastSuccessful', 'searchRepo.isRunning', function() { | ||
// accessing a prop triggers the bug (happens with isIdle as well) | ||
this.get('searchRepo.isRunning'); | ||
|
||
return this.get('lastSuccessful.length') > 0; | ||
}), | ||
|
||
searchRepo: task(function * (term) { | ||
if (isBlank(term)) { return []; } | ||
|
||
// Pause here for DEBOUNCE_MS milliseconds. Because this | ||
// task is `restartable`, if the user starts typing again, | ||
// the current search will be canceled at this point and | ||
// start over from the beginning. This is the | ||
// ember-concurrency way of debouncing a task. | ||
yield timeout(DEBOUNCE_MS); | ||
|
||
let url = `https://api.github.com/search/repositories?q=${term}`; | ||
let res = yield fetch(url); | ||
let json = yield res.json(); | ||
|
||
// We yield an AJAX request and wait for it to complete. If the task | ||
// is restarted before this request completes, the XHR request | ||
// is aborted (open the inspector and see for yourself :) | ||
let json = yield this.get('getJSON').perform(url); | ||
return json.items.slice(0, 10); | ||
}).restartable(), | ||
|
||
getJSON: task(function * (url) { | ||
let xhr; | ||
try { | ||
xhr = $.getJSON(url); | ||
let result = yield xhr.promise(); | ||
return result; | ||
|
||
// NOTE: could also write this as | ||
// return yield xhr; | ||
// | ||
// either way, the important thing is to yield before returning | ||
// so that the `finally` block doesn't run until after the | ||
// promise resolves (or the task is canceled). | ||
} finally { | ||
xhr.abort(); | ||
} | ||
}), | ||
}) | ||
}); | ||
// END-SNIPPET | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.