Open FLecorps opened 7 years ago
Hi,
That may be a useful feature to support asynchronous database queries.
What if there was also an async suggestion generator that instead of returning the suggestion list provides a Consumer
that can later be called when the results arrive? Something like the following. Note the use of callback
.
autocomplete.setSuggestionGenerator((query, limit, callback) -> {
// Start db query
asyncDbQuery(query, limit, callback);
});
private void asyncDbQuery(String query, int limit, Consumer callback) {
// Start async db query and call returnResults() when query is done
}
private void returnResults(List<String> results, Consumer callback) {
// Query done, return results to the extension
callback.accept(results);
}
Would a similar solution be useful to you?
Hi Adam,
I think your proposed solution with the consumer should solve our problem.
Would it be possible to use it with a callback = null, and in this case it would work as now? (some static suggestions lists do not need any asychronism)
It would be then perfect.
Thanks, Fred
It should be possible to trigger the callback immediately if you don't need to wait for a thread.
I pushed these changes to the async-generator branch, you could try it out if it's working with your software.
Adam, I just tested it: it works and it's a very good solution for us. awesome. Thanks! Fred PS I'll write you soon another very small & easy api enhancement proposition... about the size of the suggestion list.
Hi Fred,
I was testing this feature and there are some difficulties.
The server to client RPC, by default, only reaches the client when there is an ongoing request towards the server. When the RPC is sent asynchronously, the call only reaches the server during the next call and not during the one that initiated it.
In this case it means that the suggestions possibly arrive only after they loose relevance (e.g. query changed), and are not necessarily get displayed.
This issue can easily be solved when the server uses Push or a more frequent poll interval is set for the UI but the latter results in unwanted overhead.
Without Push, perhaps the best solution is to change UI poll interval just for the short time of the database query as the following example suggests:
/**
* Asynchronous suggestion generator function
*/
private void suggest(String query, int cap, Consumer<List<String>> callback) {
getUI().setPollInterval(300); // This will send a client to server request every 300 ms
new Thread(() -> {
// Expensive query on a background thread
// ...
// Send back suggestions to the client through the callback function
callback.accept(...);
// Important to modify UI using access() when on a background thread
getUI().access() {
getUI().setPollInterval(-1); // Turn off polling
}
}).start();
}
I'm still considering other, less manual and more general solutions before publishing but you should be able to use this method for now.
Regards, Adam
Hi Adam,
Thanks for your detailed answer. For long queries we already have a polling mecanism and basically we implemented the same stuff as you described in your answer. It's working great.
Best regards, Fred
No worries, good if it's working.
I'll keep this issue open as I may make a release out of it later on.
I would like to add a use case to this issue that empowers this need of being able to trigger the suggestion box programmatically.
Here's the thing: When you click the button while the suggestion box is still visible, the button acquires focus and thus the suggestions are dismissed. However, with the shortcutlistener, no focus change is involved, so that means the the results will be displayed in the view, but the suggestion box will still be visible, which is annoying.
The workaround in my case is to place focus on another component in the same view as the extended textfield, however this is no behaviour I'd like to implement, as I would like to keep the focus on the textfield.
Now, if you provided some solution to programmatically display/dismiss the suggestion box, I could just explicitly dismiss it in my shortcutlistener.
Hello Adam,
We use the autocomplete for some "server query search". Sometimes our requests trigger into an asynchronous modus with a polling mecanism.
In this case we'd like to be able to programmatically force the display of the Suggestions list with a new content.
We tried to use the Rpc (getRpcProxy(AutocompleteExtensionClientRpc.class).showSuggestions()) but it does not properly work, or maybe it's simply wrong.
It would be great if you could add a method into the AutocompleteExtension extension like:
AutocompleteExtension.forceShowSuggestions(List suggestions)
(?)
Thanks alot for your efforts, Fred
PS I tried to mark this "Issue" as "Enhancement", but not managed to do ing it, sorry....