mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-01 10:56:31 +08:00
feature (cancellation): logic to cancel request in client and server
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
export function http_get(url, type = "json") {
|
||||
export function http_get(url, type = "json", params) {
|
||||
return new Promise((done, err) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", url, true);
|
||||
@ -30,6 +30,12 @@ export function http_get(url, type = "json") {
|
||||
xhr.onerror = function() {
|
||||
handle_error_response(xhr, err);
|
||||
};
|
||||
if (params && params.abort) {
|
||||
params.abort.signal.onabort = () => {
|
||||
xhr.abort();
|
||||
handle_error_response(xhr, err);
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -152,10 +158,17 @@ function handle_error_response(xhr, err) {
|
||||
if (navigator.onLine === false) {
|
||||
err({ message: "Connection Lost", code: "NO_INTERNET" });
|
||||
} else if (xhr.status === 0 && xhr.responseText === "") {
|
||||
switch(xhr.readyState) {
|
||||
case XMLHttpRequest.DONE:
|
||||
case XMLHttpRequest.UNSENT:
|
||||
err({ message: "aborted", code: "ABORTED" });
|
||||
break
|
||||
default:
|
||||
err({
|
||||
message: "Service unavailable, if the problem persist, contact your administrator",
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
});
|
||||
}
|
||||
} else if (xhr.status === 500) {
|
||||
err({
|
||||
message: message || "Oups something went wrong with our servers",
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Backend IBackend
|
||||
Body map[string]interface{}
|
||||
Session map[string]string
|
||||
Share Share
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ type ISearch interface {
|
||||
}
|
||||
|
||||
type IAuditPlugin interface {
|
||||
Query(searchParams map[string]string) (AuditQueryResult, error)
|
||||
Query(ctx *App, searchParams map[string]string) (AuditQueryResult, error)
|
||||
}
|
||||
type AuditQueryResult struct {
|
||||
Form *Form `json:"form"`
|
||||
|
||||
@ -147,7 +147,7 @@ func FetchAuditHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
searchParams[key] = element[0]
|
||||
}
|
||||
result, err := plg.Query(searchParams)
|
||||
result, err := plg.Query(ctx, searchParams)
|
||||
if err != nil {
|
||||
SendErrorResult(res, err)
|
||||
return
|
||||
|
||||
@ -12,13 +12,14 @@ import (
|
||||
type Middleware func(func(*App, http.ResponseWriter, *http.Request)) func(*App, http.ResponseWriter, *http.Request)
|
||||
|
||||
func NewMiddlewareChain(fn func(*App, http.ResponseWriter, *http.Request), m []Middleware, app App) http.HandlerFunc {
|
||||
|
||||
return func(res http.ResponseWriter, req *http.Request) {
|
||||
var resw ResponseWriter = NewResponseWriter(res)
|
||||
var f func(*App, http.ResponseWriter, *http.Request) = fn
|
||||
|
||||
for i := len(m) - 1; i >= 0; i-- {
|
||||
f = m[i](f)
|
||||
}
|
||||
app.Context = req.Context()
|
||||
f(&app, &resw, req)
|
||||
if req.Body != nil {
|
||||
req.Body.Close()
|
||||
|
||||
@ -57,7 +57,7 @@ var AuditForm Form = Form{
|
||||
|
||||
type SimpleAudit struct{}
|
||||
|
||||
func (this SimpleAudit) Query(searchParams map[string]string) (AuditQueryResult, error) {
|
||||
func (this SimpleAudit) Query(ctx *App, searchParams map[string]string) (AuditQueryResult, error) {
|
||||
return AuditQueryResult{
|
||||
Form: &AuditForm,
|
||||
RenderHTML: `<style>
|
||||
|
||||
Reference in New Issue
Block a user