mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-12 12:00:17 +08:00
feat: speed up sse output
This commit is contained in:
@@ -96,19 +96,13 @@ func ReturnFetchValue[T any](fetch func() T, rw http.ResponseWriter, r *http.Req
|
|||||||
sse := NewSSE(rw, r.Context())
|
sse := NewSSE(rw, r.Context())
|
||||||
tick := time.NewTicker(tickDur)
|
tick := time.NewTicker(tickDur)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
if isYaml {
|
writer := Conditoinal(isYaml, sse.WriteYAML, sse.WriteJSON)
|
||||||
|
writer(fetch())
|
||||||
for range tick.C {
|
for range tick.C {
|
||||||
if sse.WriteYAML(fetch()) != nil {
|
if writer(fetch()) != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
for range tick.C {
|
|
||||||
if sse.WriteJSON(fetch()) != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
data := fetch()
|
data := fetch()
|
||||||
rw.Header().Set("Content-Type", Conditoinal(isYaml, "text/yaml", "application/json"))
|
rw.Header().Set("Content-Type", Conditoinal(isYaml, "text/yaml", "application/json"))
|
||||||
|
Reference in New Issue
Block a user