Skip to content

Instantly share code, notes, and snippets.

@YusukeKokubo
Created May 29, 2012 07:38
Show Gist options
  • Save YusukeKokubo/2823139 to your computer and use it in GitHub Desktop.
Save YusukeKokubo/2823139 to your computer and use it in GitHub Desktop.
Github api via GWT with Xtend
package nyao.client
import com.google.gwt.user.client.rpc.AsyncCallback
class ConfigurableAsyncCallback<T> implements AsyncCallback<T> {
def static <T> AsyncCallback<T> onSuccess((T)=>void onSuccess) {
val x = new ConfigurableAsyncCallback<T>()
x.onSuccessDo(onSuccess)
return x
}
def static <T> AsyncCallback<T> callback((ConfigurableAsyncCallback<T>)=>void init) {
val x = new ConfigurableAsyncCallback<T>()
init.apply(x)
return x
}
(Throwable)=>void onFailure = []
(T)=>void onSuccess = []
def void onSuccessDo((T)=>void onSuccess) {
this.onSuccess = onSuccess
}
def void onFailureDo((Throwable)=>void onFailure) {
this.onFailure = onFailure
}
override onFailure(Throwable caught) {
this.onFailure.apply(caught)
}
override onSuccess(T result) {
this.onSuccess.apply(result)
}
}
package nyao.client
import com.google.gwt.query.client.Function
import com.google.gwt.user.client.Event
class F extends Function {
(Event)=>boolean onSuccess = []
def static Function func((Event)=>boolean onSuccess) {
val x = new F()
x.f(onSuccess)
return x
}
def void f((Event)=>boolean onSuccess) {
this.onSuccess = onSuccess
}
override f(Event result) {
this.onSuccess.apply(result)
}
}
package nyao.client
import static com.google.gwt.query.client.GQuery.*
import static nyao.client.F.*
import static nyao.client.ConfigurableAsyncCallback.*
import com.google.gwt.core.client.EntryPoint
import com.google.gwt.core.client.GWT
import com.github.nyao.gwtgithub.client.GitHubApi
import com.github.nyao.gwtgithub.client.models.Repository
import com.github.nyao.gwtgithub.client.models.Issue
import com.google.gwt.core.client.JsArray
import com.google.gwt.query.client.GQuery
import com.google.gwt.core.client.JavaScriptObject
class Hello implements EntryPoint {
val api = new GitHubApi();
def getValue(GQuery gq) {
gq.vals.get(0)
}
def each(JsArray<? extends JavaScriptObject> items, (JavaScriptObject) => void f) {
var i = 0
while (i < items.length) {
val r = items.get(i)
f.apply(r)
i = i + 1
}
}
override onModuleLoad() {
$("#LoginSubmit").click(func[
api.getRepositories($("#Login").value, callback[
onSuccessDo[addRepositories(it.data)]
onFailureDo[GWT::log("error", it)]
])
true
])
$("#TokenSubmit").click(func[
api.setAuthorization($("#Token").value)
api.getMyRepository(callback[
onSuccessDo[addRepositories(it.data)]
onFailureDo[GWT::log("error", it)]
])
true
])
}
def void addRepositories(JsArray<Repository> rs) {
$("#Repositories tbody tr").remove
rs.each([addRepository(it as Repository)])
}
def addRepository(Repository r) {
$("#Repositories tbody")
.append($("<tr>")
.append($("<td>").text(r.name)
.append($("<a>").attr("href", r.htmlUrl)
.attr("target", "_blank")
.text("(_)")))
.append($("<td>")
.append($("<a>").text(String::valueOf(r.openIssues))
.click(func [
api.getIssues(r, callback[
onSuccessDo[addIssues(it.data)]
onFailureDo[GWT::log("error", it)]
])
true
]))))
}
def void addIssues(JsArray<Issue> issues) {
$("#Issues tbody tr").remove
issues.each([addIssue(it as Issue)])
}
def addIssue(Issue issue) {
$("#Issues tbody")
.append($("<tr>")
.append($("<td>")
.append($("<a>").attr("href", issue.htmlUrl)
.attr("target", "_blank")
.text("#" + String::valueOf(issue.number))))
.append($("<td>").text(issue.title)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment