summaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/a-h/templ/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/a-h/templ/runtime')
-rw-r--r--vendor/github.com/a-h/templ/runtime/buffer.go62
-rw-r--r--vendor/github.com/a-h/templ/runtime/bufferpool.go38
-rw-r--r--vendor/github.com/a-h/templ/runtime/builder.go8
-rw-r--r--vendor/github.com/a-h/templ/runtime/runtime.go21
4 files changed, 129 insertions, 0 deletions
diff --git a/vendor/github.com/a-h/templ/runtime/buffer.go b/vendor/github.com/a-h/templ/runtime/buffer.go
new file mode 100644
index 0000000..63e4acd
--- /dev/null
+++ b/vendor/github.com/a-h/templ/runtime/buffer.go
@@ -0,0 +1,62 @@
+package runtime
+
+import (
+ "bufio"
+ "io"
+ "net/http"
+)
+
+// DefaultBufferSize is the default size of buffers. It is set to 4KB by default, which is the
+// same as the default buffer size of bufio.Writer.
+var DefaultBufferSize = 4 * 1024 // 4KB
+
+// Buffer is a wrapper around bufio.Writer that enables flushing and closing of
+// the underlying writer.
+type Buffer struct {
+ Underlying io.Writer
+ b *bufio.Writer
+}
+
+// Write the contents of p into the buffer.
+func (b *Buffer) Write(p []byte) (n int, err error) {
+ return b.b.Write(p)
+}
+
+// Flush writes any buffered data to the underlying io.Writer and
+// calls the Flush method of the underlying http.Flusher if it implements it.
+func (b *Buffer) Flush() error {
+ if err := b.b.Flush(); err != nil {
+ return err
+ }
+ if f, ok := b.Underlying.(http.Flusher); ok {
+ f.Flush()
+ }
+ return nil
+}
+
+// Close closes the buffer and the underlying io.Writer if it implements io.Closer.
+func (b *Buffer) Close() error {
+ if c, ok := b.Underlying.(io.Closer); ok {
+ return c.Close()
+ }
+ return nil
+}
+
+// Reset sets the underlying io.Writer to w and resets the buffer.
+func (b *Buffer) Reset(w io.Writer) {
+ if b.b == nil {
+ b.b = bufio.NewWriterSize(b, DefaultBufferSize)
+ }
+ b.Underlying = w
+ b.b.Reset(w)
+}
+
+// Size returns the size of the underlying buffer in bytes.
+func (b *Buffer) Size() int {
+ return b.b.Size()
+}
+
+// WriteString writes the contents of s into the buffer.
+func (b *Buffer) WriteString(s string) (n int, err error) {
+ return b.b.WriteString(s)
+}
diff --git a/vendor/github.com/a-h/templ/runtime/bufferpool.go b/vendor/github.com/a-h/templ/runtime/bufferpool.go
new file mode 100644
index 0000000..ca2a131
--- /dev/null
+++ b/vendor/github.com/a-h/templ/runtime/bufferpool.go
@@ -0,0 +1,38 @@
+package runtime
+
+import (
+ "io"
+ "sync"
+)
+
+var bufferPool = sync.Pool{
+ New: func() any {
+ return new(Buffer)
+ },
+}
+
+// GetBuffer creates and returns a new buffer if the writer is not already a buffer,
+// or returns the existing buffer if it is.
+func GetBuffer(w io.Writer) (b *Buffer, existing bool) {
+ if w == nil {
+ return nil, false
+ }
+ b, ok := w.(*Buffer)
+ if ok {
+ return b, true
+ }
+ b = bufferPool.Get().(*Buffer)
+ b.Reset(w)
+ return b, false
+}
+
+// ReleaseBuffer flushes the buffer and returns it to the pool.
+func ReleaseBuffer(w io.Writer) (err error) {
+ b, ok := w.(*Buffer)
+ if !ok {
+ return nil
+ }
+ err = b.Flush()
+ bufferPool.Put(b)
+ return err
+}
diff --git a/vendor/github.com/a-h/templ/runtime/builder.go b/vendor/github.com/a-h/templ/runtime/builder.go
new file mode 100644
index 0000000..0f4c9d4
--- /dev/null
+++ b/vendor/github.com/a-h/templ/runtime/builder.go
@@ -0,0 +1,8 @@
+package runtime
+
+import "strings"
+
+// GetBuilder returns a strings.Builder.
+func GetBuilder() (sb strings.Builder) {
+ return sb
+}
diff --git a/vendor/github.com/a-h/templ/runtime/runtime.go b/vendor/github.com/a-h/templ/runtime/runtime.go
new file mode 100644
index 0000000..aaa4a2c
--- /dev/null
+++ b/vendor/github.com/a-h/templ/runtime/runtime.go
@@ -0,0 +1,21 @@
+package runtime
+
+import (
+ "context"
+ "io"
+
+ "github.com/a-h/templ"
+)
+
+// GeneratedComponentInput is used to avoid generated code needing to import the `context` and `io` packages.
+type GeneratedComponentInput struct {
+ Context context.Context
+ Writer io.Writer
+}
+
+// GeneratedTemplate is used to avoid generated code needing to import the `context` and `io` packages.
+func GeneratedTemplate(f func(GeneratedComponentInput) error) templ.Component {
+ return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
+ return f(GeneratedComponentInput{ctx, w})
+ })
+}