From 46b16e57005d0e367456468fdf0344196e130c69 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 13 Jul 2023 22:51:20 +0200 Subject: [PATCH] templating: Remove unused lazy code --- .../dis/component/server/templating/base.go | 135 ++++-------------- .../dis/component/server/templating/flags.go | 2 +- .../dis/component/server/templating/parse.go | 2 +- .../component/server/templating/src/base.html | 1 - 4 files changed, 30 insertions(+), 110 deletions(-) diff --git a/internal/dis/component/server/templating/base.go b/internal/dis/component/server/templating/base.go index f646cd9..48efef4 100644 --- a/internal/dis/component/server/templating/base.go +++ b/internal/dis/component/server/templating/base.go @@ -3,7 +3,6 @@ package templating import ( "context" _ "embed" - "encoding/json" "fmt" "html/template" "net/http" @@ -14,7 +13,6 @@ import ( "github.com/gorilla/csrf" "github.com/rs/zerolog" "github.com/tkw1536/pkglib/httpx" - "github.com/tkw1536/pkglib/timex" ) //go:embed "src/base.html" @@ -32,17 +30,10 @@ func (tpl *Template[C]) Template() *template.Template { return baseTemplate } -// LazyContext is like the lazy context -func (tpl *Template[C]) LazyContext(r *http.Request, f func() (C, error), funcs ...FlagFunc) (ctx *tContext[C]) { - ctx = tpl.context(r, funcs...) - ctx.startLazy(f) - return ctx -} - // Context generates the context to pass to an instance of the template returned by Template. func (tpl *Template[C]) Context(r *http.Request, c C, funcs ...FlagFunc) (ctx *tContext[C]) { ctx = tpl.context(r, funcs...) - ctx.start(c, nil) // setup the request + ctx.cMain = c return ctx } @@ -93,27 +84,22 @@ func FormTemplateContext(tw *Template[FormContext]) func(ctx httpx.FormContext, } } -// Hander returns a function that returns a context for the given template +// HandlerWithFlags returns a function that, given a request, generates context and error to pass to the generated template. +// The worker implements the actual buisness logic, it takes a request, and returns the content for the main template, and any error. +// See also HandlerWithFlags. func (tw *Template[C]) Handler(f func(r *http.Request) (C, error)) func(r *http.Request) (any, error) { - // TODO: Should this one be removed? return tw.HandlerWithFlags(func(r *http.Request) (C, []FlagFunc, error) { c, err := f(r) return c, nil, err }) } -// HTMLHandler returns a new HTMLHandler for this request -func (tw *Template[C]) HTMLHandler(f func(r *http.Request) (C, error)) httpx.HTMLHandler[any] { - return httpx.HTMLHandler[any]{ - Handler: tw.Handler(f), - Template: tw.Template(), - } -} - -// HandlerWithFlags works like handler, but additionally receive funcs to generate flags -func (tw *Template[C]) HandlerWithFlags(f func(r *http.Request) (C, []FlagFunc, error)) func(r *http.Request) (any, error) { +// HandlerWithFlags returns a function that, given a request, generates context and error to pass to the generated template. +// The worker implements the actual buisness logic, it takes a request, and returns the content for the main template, flag functions and error. +// See also Handler. +func (tw *Template[C]) HandlerWithFlags(worker func(r *http.Request) (C, []FlagFunc, error)) func(r *http.Request) (any, error) { return func(r *http.Request) (any, error) { - c, funcs, err := f(r) + c, funcs, err := worker(r) if err != nil { return nil, err } @@ -122,9 +108,20 @@ func (tw *Template[C]) HandlerWithFlags(f func(r *http.Request) (C, []FlagFunc, } } -func (tw *Template[C]) HTMLHandlerWithFlags(f func(r *http.Request) (C, []FlagFunc, error)) httpx.HTMLHandler[any] { +// HTMLHandler creates a new httpx.HTMLHandler that calls tw.Handler(worker) and tw.Template. +// See also Handler. +func (tw *Template[C]) HTMLHandler(worker func(r *http.Request) (C, error)) httpx.HTMLHandler[any] { return httpx.HTMLHandler[any]{ - Handler: tw.HandlerWithFlags(f), + Handler: tw.Handler(worker), + Template: tw.Template(), + } +} + +// HTMLHandlerWithFlags creates a new httpx.HTMLHandler that calls tw.HandlerWithFlags(worker) and tw.Template. +// See also HandlerWithFlags. +func (tw *Template[C]) HTMLHandlerWithFlags(worker func(r *http.Request) (C, []FlagFunc, error)) httpx.HTMLHandler[any] { + return httpx.HTMLHandler[any]{ + Handler: tw.HandlerWithFlags(worker), Template: tw.Template(), } } @@ -140,74 +137,16 @@ type tContext[C any] struct { ctx context.Context // underlying context for render // the main template and context - eMain chan error // are we done? - cWaiting bool - tMain *template.Template - cMain C + tMain *template.Template + cMain C // the footer template and context tFooter *template.Template cFooter RuntimeFlags } -func (ctx *tContext[C]) start(c C, err error) { - ctx.cMain = c - ctx.eMain = make(chan error, 1) - ctx.eMain <- err -} - -func (ctx *tContext[C]) startLazy(f func() (C, error)) { - ctx.eMain = make(chan error, 1) - go func() { - defer close(ctx.eMain) - - // compute the result, storing the error - var err error - ctx.cMain, err = f() - ctx.eMain <- err - }() -} - -const mainDelay = time.Second - // Main renders the main template. func (ctx *tContext[C]) Main() (template.HTML, error) { - timer := timex.NewTimer() - defer timex.ReleaseTimer(timer) - - timer.Reset(mainDelay) - select { - - case err := <-ctx.eMain: - // we received the result within the given time - // so we can render it immediatly - ctx.cWaiting = false - return ctx.doMain(err) - - case <-timer.C: - // the template is taking longer than expected. - // we should display a spinner, and do something later - ctx.cWaiting = true - return timeWait, nil - } -} - -// Footer renders the footer template -func (ctx *tContext[C]) Footer() (template.HTML, error) { - return ctx.renderSafe("footer", ctx.tFooter, ctx.cFooter) -} - -const ( - timeWait = "Loading" - errUnknown = "An unknown error occured, see the server log for details. " -) - -func (ctx *tContext[C]) doMain(err error) (template.HTML, error) { - if err != nil { - zerolog.Ctx(ctx.ctx).Err(err).Msg("error lazy loading template") - return errUnknown, nil - } - // if the context has a runtime flags embed, then set the field properly if ctx.updateEmbedded { reflect.ValueOf(&ctx.cMain).Elem(). @@ -218,34 +157,16 @@ func (ctx *tContext[C]) doMain(err error) (template.HTML, error) { return ctx.renderSafe("main", ctx.tMain, ctx.cMain) } -func (ctx *tContext[C]) AfterBody() (template.HTML, error) { - // everything was done already - if !ctx.cWaiting { - return "", nil - } - - // wait for the result to appear - res, err := ctx.doMain(<-ctx.eMain) - if err != nil { - return "", err - } - - str, err := json.Marshal(string(res)) - if err != nil { - return "", err - } - - fix := "" - - // hook that is called after the body is complete - return template.HTML(fix), nil +// Footer renders the footer template +func (ctx *tContext[C]) Footer() (template.HTML, error) { + return ctx.renderSafe("footer", ctx.tFooter, ctx.cFooter) } const renderSafeError = "Error displaying page. See server log for details. " func (ctx *tContext[C]) renderSafe(name string, t *template.Template, c any) (template.HTML, error) { - // already done + // already done with context => return if err := ctx.ctx.Err(); err != nil { return "", err } diff --git a/internal/dis/component/server/templating/flags.go b/internal/dis/component/server/templating/flags.go index f8a75f5..0900ad5 100644 --- a/internal/dis/component/server/templating/flags.go +++ b/internal/dis/component/server/templating/flags.go @@ -40,7 +40,7 @@ type RuntimeFlags struct { CSRF template.HTML // csrf data (if any) } -var runtimeFlagsName = reflectx.TypeOf[RuntimeFlags]().Name() +var runtimeFlagsName = reflectx.MakeType[RuntimeFlags]().Name() // Clone clones this flags func (flags Flags) Clone() Flags { diff --git a/internal/dis/component/server/templating/parse.go b/internal/dis/component/server/templating/parse.go index 2f8a8c4..e875a3d 100644 --- a/internal/dis/component/server/templating/parse.go +++ b/internal/dis/component/server/templating/parse.go @@ -21,7 +21,7 @@ type Parsed[C any] struct { // If base is not nil, every template associated with the base template is copied into the given template. // Functions will be applied on creation time to represent the context for the given template. func Parse[C any](name string, source []byte, base *template.Template, funcs ...FlagFunc) Parsed[C] { - tp := reflectx.TypeOf[C]() + tp := reflectx.MakeType[C]() // determine if we have an embedded field in the struct var hasEmbed bool diff --git a/internal/dis/component/server/templating/src/base.html b/internal/dis/component/server/templating/src/base.html index bc735d3..26a8157 100644 --- a/internal/dis/component/server/templating/src/base.html +++ b/internal/dis/component/server/templating/src/base.html @@ -49,7 +49,6 @@ {{ .Footer }} - {{ .AfterBody }} {{ .Runtime.Flags.Assets.Scripts }}