forked from GitHub/gf-core
GWT: only allow a single compeltion request at a time.
This commit is contained in:
@@ -10,6 +10,9 @@ public class CompletionOracle extends SuggestOracle {
|
|||||||
|
|
||||||
private List<String> inputLangs;
|
private List<String> inputLangs;
|
||||||
|
|
||||||
|
private GFRequest gfRequest = null;
|
||||||
|
|
||||||
|
|
||||||
public CompletionOracle (GF gf) {
|
public CompletionOracle (GF gf) {
|
||||||
this.gf = gf;
|
this.gf = gf;
|
||||||
inputLangs = new ArrayList<String>();
|
inputLangs = new ArrayList<String>();
|
||||||
@@ -39,7 +42,12 @@ public class CompletionOracle extends SuggestOracle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void requestSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) {
|
public void requestSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) {
|
||||||
gf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
|
|
||||||
|
// only allow a single completion request at a time
|
||||||
|
if (gfRequest != null)
|
||||||
|
gfRequest.cancel();
|
||||||
|
|
||||||
|
gfRequest = gf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
|
||||||
new GF.CompleteCallback() {
|
new GF.CompleteCallback() {
|
||||||
public void onCompleteDone(GF.Completions completions) {
|
public void onCompleteDone(GF.Completions completions) {
|
||||||
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
|
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ public class GF {
|
|||||||
|
|
||||||
/* Languages */
|
/* Languages */
|
||||||
|
|
||||||
public void languages (final LanguagesCallback callback) {
|
public GFRequest languages (final LanguagesCallback callback) {
|
||||||
sendRequest("languages", null, new JSONRequestCallback() {
|
return sendRequest("languages", null, new JSONRequestCallback() {
|
||||||
public void onJSONReceived(JSONValue json) {
|
public void onJSONReceived(JSONValue json) {
|
||||||
callback.onLanguagesDone((Languages)json.isArray().getJavaScriptObject().cast());
|
callback.onLanguagesDone((Languages)json.isArray().getJavaScriptObject().cast());
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,7 @@ public class GF {
|
|||||||
|
|
||||||
/* Translation */
|
/* Translation */
|
||||||
|
|
||||||
public void translate (String input, List<String> fromLangs, String cat, List<String> toLangs,
|
public GFRequest translate (String input, List<String> fromLangs, String cat, List<String> toLangs,
|
||||||
final TranslateCallback callback) {
|
final TranslateCallback callback) {
|
||||||
List<Arg> args = new ArrayList<Arg>();
|
List<Arg> args = new ArrayList<Arg>();
|
||||||
args.add(new Arg("input", input));
|
args.add(new Arg("input", input));
|
||||||
@@ -65,7 +65,7 @@ public class GF {
|
|||||||
args.add(new Arg("to", to));
|
args.add(new Arg("to", to));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendRequest("translate", args, new JSONRequestCallback() {
|
return sendRequest("translate", args, new JSONRequestCallback() {
|
||||||
public void onJSONReceived(JSONValue json) {
|
public void onJSONReceived(JSONValue json) {
|
||||||
callback.onTranslateDone((Translations)json.isArray().getJavaScriptObject().cast());
|
callback.onTranslateDone((Translations)json.isArray().getJavaScriptObject().cast());
|
||||||
}
|
}
|
||||||
@@ -89,8 +89,8 @@ public class GF {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Completion */
|
/* Completion */
|
||||||
|
|
||||||
public void complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
|
public GFRequest complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
|
||||||
List<Arg> args = new ArrayList<Arg>();
|
List<Arg> args = new ArrayList<Arg>();
|
||||||
args.add(new Arg("input", input));
|
args.add(new Arg("input", input));
|
||||||
if (fromLangs != null) {
|
if (fromLangs != null) {
|
||||||
@@ -100,7 +100,7 @@ public class GF {
|
|||||||
}
|
}
|
||||||
args.add(new Arg("cat", cat));
|
args.add(new Arg("cat", cat));
|
||||||
args.add(new Arg("limit", limit));
|
args.add(new Arg("limit", limit));
|
||||||
sendRequest("complete", args, new JSONRequestCallback() {
|
return sendRequest("complete", args, new JSONRequestCallback() {
|
||||||
public void onJSONReceived(JSONValue json) {
|
public void onJSONReceived(JSONValue json) {
|
||||||
callback.onCompleteDone((Completions)json.isArray().getJavaScriptObject().cast());
|
callback.onCompleteDone((Completions)json.isArray().getJavaScriptObject().cast());
|
||||||
}
|
}
|
||||||
@@ -129,12 +129,13 @@ public class GF {
|
|||||||
public void onJSONReceived(JSONValue json);
|
public void onJSONReceived(JSONValue json);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendRequest (String resource, List<Arg> vars, final JSONRequestCallback callback) {
|
private GFRequest sendRequest (String resource, List<Arg> vars, final JSONRequestCallback callback) {
|
||||||
String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
|
String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
|
||||||
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
|
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
|
||||||
|
Request request = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Request request = builder.sendRequest(null, new RequestCallback() {
|
request = builder.sendRequest(null, new RequestCallback() {
|
||||||
public void onError(Request request, Throwable e) {
|
public void onError(Request request, Throwable e) {
|
||||||
GWT.log("onError called", e);
|
GWT.log("onError called", e);
|
||||||
}
|
}
|
||||||
@@ -150,6 +151,8 @@ public class GF {
|
|||||||
} catch (RequestException e) {
|
} catch (RequestException e) {
|
||||||
GWT.log("Failed to send request", e);
|
GWT.log("Failed to send request", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new GFRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Arg {
|
private static class Arg {
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package se.chalmers.cs.gf.gwt_translate.client;
|
||||||
|
|
||||||
|
import com.google.gwt.http.client.*;
|
||||||
|
|
||||||
|
public class GFRequest {
|
||||||
|
|
||||||
|
private Request httpRequest;
|
||||||
|
|
||||||
|
GFRequest (Request httpRequest) {
|
||||||
|
this.httpRequest = httpRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cancel() {
|
||||||
|
if (httpRequest != null) {
|
||||||
|
httpRequest.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user