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) => {
|
return new Promise((done, err) => {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
xhr.open("GET", url, true);
|
xhr.open("GET", url, true);
|
||||||
@ -30,6 +30,12 @@ export function http_get(url, type = "json") {
|
|||||||
xhr.onerror = function() {
|
xhr.onerror = function() {
|
||||||
handle_error_response(xhr, err);
|
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) {
|
if (navigator.onLine === false) {
|
||||||
err({ message: "Connection Lost", code: "NO_INTERNET" });
|
err({ message: "Connection Lost", code: "NO_INTERNET" });
|
||||||
} else if (xhr.status === 0 && xhr.responseText === "") {
|
} else if (xhr.status === 0 && xhr.responseText === "") {
|
||||||
err({
|
switch(xhr.readyState) {
|
||||||
message: "Service unavailable, if the problem persist, contact your administrator",
|
case XMLHttpRequest.DONE:
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
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) {
|
} else if (xhr.status === 500) {
|
||||||
err({
|
err({
|
||||||
message: message || "Oups something went wrong with our servers",
|
message: message || "Oups something went wrong with our servers",
|
||||||
|
|||||||
@ -1,8 +1,13 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
Backend IBackend
|
Backend IBackend
|
||||||
Body map[string]interface{}
|
Body map[string]interface{}
|
||||||
Session map[string]string
|
Session map[string]string
|
||||||
Share Share
|
Share Share
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,7 +46,7 @@ type ISearch interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IAuditPlugin interface {
|
type IAuditPlugin interface {
|
||||||
Query(searchParams map[string]string) (AuditQueryResult, error)
|
Query(ctx *App, searchParams map[string]string) (AuditQueryResult, error)
|
||||||
}
|
}
|
||||||
type AuditQueryResult struct {
|
type AuditQueryResult struct {
|
||||||
Form *Form `json:"form"`
|
Form *Form `json:"form"`
|
||||||
|
|||||||
@ -147,7 +147,7 @@ func FetchAuditHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
searchParams[key] = element[0]
|
searchParams[key] = element[0]
|
||||||
}
|
}
|
||||||
result, err := plg.Query(searchParams)
|
result, err := plg.Query(ctx, searchParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SendErrorResult(res, err)
|
SendErrorResult(res, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -12,13 +12,14 @@ import (
|
|||||||
type Middleware func(func(*App, http.ResponseWriter, *http.Request)) func(*App, http.ResponseWriter, *http.Request)
|
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 {
|
func NewMiddlewareChain(fn func(*App, http.ResponseWriter, *http.Request), m []Middleware, app App) http.HandlerFunc {
|
||||||
|
|
||||||
return func(res http.ResponseWriter, req *http.Request) {
|
return func(res http.ResponseWriter, req *http.Request) {
|
||||||
var resw ResponseWriter = NewResponseWriter(res)
|
var resw ResponseWriter = NewResponseWriter(res)
|
||||||
var f func(*App, http.ResponseWriter, *http.Request) = fn
|
var f func(*App, http.ResponseWriter, *http.Request) = fn
|
||||||
|
|
||||||
for i := len(m) - 1; i >= 0; i-- {
|
for i := len(m) - 1; i >= 0; i-- {
|
||||||
f = m[i](f)
|
f = m[i](f)
|
||||||
}
|
}
|
||||||
|
app.Context = req.Context()
|
||||||
f(&app, &resw, req)
|
f(&app, &resw, req)
|
||||||
if req.Body != nil {
|
if req.Body != nil {
|
||||||
req.Body.Close()
|
req.Body.Close()
|
||||||
|
|||||||
@ -57,7 +57,7 @@ var AuditForm Form = Form{
|
|||||||
|
|
||||||
type SimpleAudit struct{}
|
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{
|
return AuditQueryResult{
|
||||||
Form: &AuditForm,
|
Form: &AuditForm,
|
||||||
RenderHTML: `<style>
|
RenderHTML: `<style>
|
||||||
|
|||||||
Reference in New Issue
Block a user