diff --git a/_examples/real-world-examples/server-sent-events-htmx/http.go b/_examples/real-world-examples/server-sent-events-htmx/http.go index dea23e7f9..bfe1e26ef 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/http.go +++ b/_examples/real-world-examples/server-sent-events-htmx/http.go @@ -39,7 +39,9 @@ func NewHandler(repo *Repository, eventBus *cqrs.EventBus, sseRouter watermillht counter := sseHandlersCounter{} - e.GET("/", h.AllPosts) + e.GET("/", h.Index) + e.GET("/posts", h.Posts) + e.GET("/idle", h.Idle) e.POST("/posts/:id/reactions", h.AddReaction) e.GET("/posts/:id/stats", func(c echo.Context) error { postID := c.Param("id") @@ -59,12 +61,34 @@ func NewHandler(repo *Repository, eventBus *cqrs.EventBus, sseRouter watermillht return e } -func (h Handler) AllPosts(c echo.Context) error { - posts, err := h.repo.AllPosts(c.Request().Context()) +func (h Handler) Index(c echo.Context) error { + posts, err := h.allPosts(c) + if err != nil { + return err + } + + return views.Index(posts).Render(c.Request().Context(), c.Response()) +} + +func (h Handler) Posts(c echo.Context) error { + posts, err := h.allPosts(c) if err != nil { return err } + return views.Posts(posts).Render(c.Request().Context(), c.Response()) +} + +func (h Handler) Idle(c echo.Context) error { + return views.Idle().Render(c.Request().Context(), c.Response()) +} + +func (h Handler) allPosts(c echo.Context) ([]views.Post, error) { + posts, err := h.repo.AllPosts(c.Request().Context()) + if err != nil { + return nil, err + } + for _, post := range posts { event := PostViewed{ PostID: post.ID, @@ -72,7 +96,7 @@ func (h Handler) AllPosts(c echo.Context) error { err = h.eventBus.Publish(c.Request().Context(), event) if err != nil { - return err + return nil, err } } @@ -81,7 +105,7 @@ func (h Handler) AllPosts(c echo.Context) error { postViews = append(postViews, newPostView(post)) } - return views.Index(postViews).Render(c.Request().Context(), c.Response()) + return postViews, nil } func (h Handler) AddReaction(c echo.Context) error { diff --git a/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ b/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ index 916d7fa7c..a1fa9e739 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ +++ b/_examples/real-world-examples/server-sent-events-htmx/views/pages.templ @@ -27,14 +27,41 @@ type PostViews struct { templ Index(posts []Post) { @base() { -
- for _, p := range posts { - @postView(p) - } -
+ @Posts(posts) } } +templ Posts(posts []Post) { +
+ for _, p := range posts { + @postView(p) + } + +
+
+} + +templ Idle() { +
+
+
+
Paused to save resources. 🫢
+ +
Still around?
+ + +
+
+
+} + templ postView(post Post) {
diff --git a/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go b/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go index 3d4e6fad5..bf86afd3a 100644 --- a/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go +++ b/_examples/real-world-examples/server-sent-events-htmx/views/pages_templ.go @@ -54,17 +54,7 @@ func Index(posts []Post) templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - for _, p := range posts { - templ_7745c5c3_Err = postView(p).Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + templ_7745c5c3_Err = Posts(posts).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -84,7 +74,7 @@ func Index(posts []Post) templ.Component { }) } -func postView(post Post) templ.Component { +func Posts(posts []Post) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -97,16 +87,74 @@ func postView(post Post) templ.Component { templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for _, p := range posts { + templ_7745c5c3_Err = postView(p).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} + +func Idle() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + if templ_7745c5c3_Var4 == nil { + templ_7745c5c3_Var4 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Paused to save resources. 🫢
Still around?
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} + +func postView(post Post) templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var5 := templ.GetChildren(ctx) + if templ_7745c5c3_Var5 == nil { + templ_7745c5c3_Var5 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var4 string - templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(post.Author) + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(post.Author) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 43, Col: 53} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 70, Col: 53} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -114,12 +162,12 @@ func postView(post Post) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var5 string - templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(post.Date) + var templ_7745c5c3_Var7 string + templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(post.Date) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 45, Col: 56} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 72, Col: 56} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -127,12 +175,12 @@ func postView(post Post) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var6 string - templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(post.Content) + var templ_7745c5c3_Var8 string + templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(post.Content) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 49, Col: 43} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 76, Col: 43} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -140,12 +188,12 @@ func postView(post Post) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var7 string - templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs("/posts/" + post.ID + "/stats") + var templ_7745c5c3_Var9 string + templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs("/posts/" + post.ID + "/stats") if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 50, Col: 70} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 77, Col: 70} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -168,13 +216,13 @@ func PostStatsView(stats PostStats) templ.Component { defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var8 := templ.GetChildren(ctx) - if templ_7745c5c3_Var8 == nil { - templ_7745c5c3_Var8 = templ.NopComponent + templ_7745c5c3_Var10 := templ.GetChildren(ctx) + if templ_7745c5c3_Var10 == nil { + templ_7745c5c3_Var10 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - var templ_7745c5c3_Var9 = []any{"d-flex", "align-items-center", templ.KV("animated", stats.Views.JustChanged)} - templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var9...) + var templ_7745c5c3_Var11 = []any{"d-flex", "align-items-center", templ.KV("animated", stats.Views.JustChanged)} + templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var11...) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -182,12 +230,12 @@ func PostStatsView(stats PostStats) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var10 string - templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var9).String()) + var templ_7745c5c3_Var12 string + templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var11).String()) if templ_7745c5c3_Err != nil { return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 1, Col: 0} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -195,12 +243,12 @@ func PostStatsView(stats PostStats) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var11 string - templ_7745c5c3_Var11, templ_7745c5c3_Err = templ.JoinStringErrs(stats.Views.Count + " views") + var templ_7745c5c3_Var13 string + templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinStringErrs(stats.Views.Count + " views") if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 58, Col: 64} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `views/pages.templ`, Line: 85, Col: 64} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var11)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var13)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -233,21 +281,21 @@ func reactionButton(postID string, reaction Reaction) templ.Component { defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var12 := templ.GetChildren(ctx) - if templ_7745c5c3_Var12 == nil { - templ_7745c5c3_Var12 = templ.NopComponent + templ_7745c5c3_Var14 := templ.GetChildren(ctx) + if templ_7745c5c3_Var14 == nil { + templ_7745c5c3_Var14 = templ.NopComponent } ctx = templ.ClearChildren(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("