Skip to content

Commit

Permalink
Send async data to template with deferrable
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed Mar 26, 2024
1 parent 5258b72 commit b6add16
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Enables asynchronous DOM updates with no Javascript needed!
## Test

```shell
go test ./...
go test -race ./...
```

## Run
Expand Down
7 changes: 6 additions & 1 deletion internal/app/index.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package app

import (
"github.com/initialcapacity/go-streaming/pkg/deferrable"
"github.com/initialcapacity/go-streaming/pkg/websupport"
"net/http"
"time"
)

type model struct {
Message deferrable.Defer[chan []string]
}

func Index(addArtificialDelay bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := make(chan []string)
Expand All @@ -22,6 +27,6 @@ func Index(addArtificialDelay bool) http.HandlerFunc {
close(data)
}()

_ = websupport.Render(w, Resources, "index", data)
_ = websupport.Render(w, Resources, "index", model{Message: deferrable.New(w, data)})
}
}
2 changes: 1 addition & 1 deletion internal/app/resources/templates/index.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</slot>
</section>
</template>
{{ range $items := . }}
{{ range $items := .Message.Value }}
<div slot="content">
<h2>
Success!
Expand Down
20 changes: 20 additions & 0 deletions pkg/deferrable/deferrable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package deferrable

import "net/http"

type Defer[T any] struct {
writer http.ResponseWriter
value T
}

func New[T any](writer http.ResponseWriter, value T) Defer[T] {
return Defer[T]{
writer: writer,
value: value,
}
}

func (d Defer[T]) Value() T {
_ = http.NewResponseController(d.writer).Flush()
return d.value
}
32 changes: 0 additions & 32 deletions pkg/websupport/add_flush.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/websupport/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,7 @@ func Render(writer http.ResponseWriter, resources fs.FS, templateName string, da

writer.Header().Set("Transfer-Encoding", "chunked")
temp := template.Must(template.New(fileName).
Funcs(template.FuncMap{
"flush": func(value any) any {
go func() {
// flush immediately after the value is returned
// TODO: address the data race
_ = flush(writer)
}()

return value
},
}).
ParseFS(resources, "resources/templates/template.gohtml", fmt.Sprintf("resources/templates/%s", fileName)))

AddFlush(temp)

return temp.Execute(writer, data)
}

func flush(writer http.ResponseWriter) error {
return http.NewResponseController(writer).Flush()
}

0 comments on commit b6add16

Please sign in to comment.